@echo off
title Siraya Printer - Stop Agent
setlocal EnableExtensions

net session >nul 2>&1
if %errorlevel% neq 0 (
  echo Requesting administrator privileges...
  powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  exit /b
)

cd /d "%~dp0"
echo.
echo  Stopping Siraya printer agent so install/update can replace files...
echo.

if exist "%~dp0stop-agent.ps1" (
  powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0stop-agent.ps1"
) else (
  powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ^
    "try { schtasks /End /TN SirayaPrinterAgent 2^>$null } catch {}; ^
     try { net stop SirayaPrinterService 2^>$null } catch {}; ^
     Start-Sleep -Seconds 2; ^
     Get-Process -Name node -ErrorAction SilentlyContinue ^| ForEach-Object { ^
       $c = (Get-CimInstance Win32_Process -Filter ('ProcessId=' + $_.Id)).CommandLine; ^
       if ($c -match 'Siraya[\\/]Printer') { Stop-Process -Id $_.Id -Force } ^
     }"
)

echo.
echo  Done. You can now run Install.cmd or Update.cmd.
echo.
pause
exit /b 0
