param(
    [string]$ServiceName = "lk-lemmy",
    [string]$DisplayName = "Ledningsagarmodul",
    [string]$Description = "Ledningsagarmodul - v2",
    [string]$PublishPath = "C:\Apps\Lam",
    [string]$DllName = "LkLemmy.Lam.dll",
    [string]$StartupType = "Automatic",
    [string]$SettingsPath = "" # value only needed in case environment variables are not set, or multi lemmy support is needed
)

$dotnetPath = (Get-Command dotnet).Source
$dllPath = Join-Path $PublishPath $DllName

if (-not (Test-Path $dllPath)) {
    Write-Error "DLL not found: $dllPath"
    exit 1
}

$binaryPath = "`"$dotnetPath`" `"$dllPath`""
# in case you have multiple lemmys, you can create your service this way, passing the --settings path value
if ($SettingsPath) {
    $binaryPath = "`"$dotnetPath`" `"$dllPath`" --settings `"$SettingsPath`""
}

$existingService = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($existingService) {
    Write-Error "Service '$ServiceName' already exists."
    exit 1
}

New-Service `
    -Name $ServiceName `
    -DisplayName $DisplayName `
    -BinaryPathName $binaryPath `
    -Description $Description `
    -StartupType $StartupType

Start-Service -Name $ServiceName

Write-Host "Service '$ServiceName' created and started successfully."