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

if (!(Test-Path $PidFile)) {
  Write-Host "Agent status: stopped"
  exit 1
}

$PidValue = Get-Content $PidFile -ErrorAction SilentlyContinue
$Process = $null
if ($PidValue) {
  $Process = Get-Process -Id $PidValue -ErrorAction SilentlyContinue
}
if ($Process) {
  Write-Host "Agent status: running. PID=$PidValue"
  exit 0
}
Write-Host "Agent status: stopped. Stale PID=$PidValue"
exit 1
