$ErrorActionPreference = "Stop"
$AppDir = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
$RunScript = Join-Path $AppDir "scripts\run-agent.ps1"
$ServiceName = "ControlCmdAgent"
$BinaryPath = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$RunScript`""

if (Get-Service -Name $ServiceName -ErrorAction SilentlyContinue) {
  Write-Host "Service already exists: $ServiceName"
  exit 0
}

New-Service -Name $ServiceName -BinaryPathName $BinaryPath -DisplayName "ControlCmd Agent" -StartupType Automatic
Start-Service -Name $ServiceName
Write-Host "Service installed and started: $ServiceName"
