$ErrorActionPreference = "Stop"
$AppDir = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
$PidFile = Join-Path $AppDir "run\agent.pid"

if (!(Test-Path $PidFile)) {
  Write-Host "Agent is not running."
  exit 0
}

$PidValue = Get-Content $PidFile -ErrorAction SilentlyContinue
if ($PidValue -and (Get-Process -Id $PidValue -ErrorAction SilentlyContinue)) {
  Stop-Process -Id $PidValue -Force
  Write-Host "Agent stopped. PID=$PidValue"
} else {
  Write-Host "Agent process is not running."
}
Remove-Item $PidFile -Force -ErrorAction SilentlyContinue
