@echo off
setlocal EnableExtensions
title Applotus Sentinel Agent - Installer

:: Re-launch elevated if not already running as Administrator -- creating a SYSTEM
:: scheduled task below requires it.
net session >nul 2>nul
if not "%errorlevel%"=="0" (
    echo Requesting Administrator privileges...
    powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)

echo ================================================
echo  Applotus Sentinel Agent - Windows Installer
echo ================================================
echo.

set "SERVER_URL=https://applotus.fly.dev"
set "TENANT_ID=tenant_default"
set "API_KEY="
set "INSTALL_DIR=%ProgramData%\Applotus"
set "AGENT_PATH=%INSTALL_DIR%\sentinel-agent.py"
set "TASK_NAME=ApplotusSentinelAgent"

echo [1/4] Checking prerequisites (Python)...
set "PYTHON_EXE="
for /f "delims=" %%P in ('where python.exe 2^>nul') do if not defined PYTHON_EXE set "PYTHON_EXE=%%P"
if defined PYTHON_EXE goto :python_ok
for /f "delims=" %%P in ('where py.exe 2^>nul') do if not defined PYTHON_EXE set "PYTHON_EXE=%%P"
if defined PYTHON_EXE goto :python_ok

echo   Python not found. Installing via winget...
where winget.exe >nul 2>nul
if not "%errorlevel%"=="0" (
    echo   ERROR: winget is not available on this machine.
    echo   Install "App Installer" from the Microsoft Store: https://aka.ms/getwinget
    echo   then re-run this installer.
    pause
    exit /b 1
)
winget install Python.Python.3.11 --scope machine --override "/quiet PrependPath=1" --accept-package-agreements --accept-source-agreements
:: winget updates the machine PATH in the registry; refresh this session PATH from it
for /f "skip=2 tokens=3*" %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do set "PATH=%%B;%PATH%"
for /f "delims=" %%P in ('where python.exe 2^>nul') do if not defined PYTHON_EXE set "PYTHON_EXE=%%P"
if not defined PYTHON_EXE (
    echo   ERROR: Python still not found after install. Open a new terminal and re-run this installer.
    pause
    exit /b 1
)

:python_ok
echo   Using %PYTHON_EXE%

echo [2/4] Downloading Sentinel agent...
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%"
powershell -NoProfile -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri '%SERVER_URL%/download/sentinel-agent.py' -OutFile '%AGENT_PATH%'"
if not exist "%AGENT_PATH%" (
    echo   ERROR: Download failed.
    pause
    exit /b 1
)

echo [3/4] Registering Scheduled Task (starts at every boot, before login, as SYSTEM)...
schtasks /query /tn "%TASK_NAME%" >nul 2>nul
if "%errorlevel%"=="0" schtasks /delete /tn "%TASK_NAME%" /f >nul 2>nul
schtasks /create /tn "%TASK_NAME%" /sc onstart /ru SYSTEM /rl highest /tr "\"%PYTHON_EXE%\" \"%AGENT_PATH%\" --server %SERVER_URL% --tenant %TENANT_ID%" /f
if not "%errorlevel%"=="0" (
    echo   ERROR: Could not create the scheduled task. Are you running as Administrator?
    pause
    exit /b 1
)

echo [4/4] Starting agent now...
schtasks /run /tn "%TASK_NAME%"

echo.
echo ================================================
echo  Done. The agent is running now and will start
echo  automatically on every boot, even before login.
echo.
echo  Stop:   schtasks /end /tn "%TASK_NAME%"
echo  Remove: schtasks /delete /tn "%TASK_NAME%" /f
echo ================================================
pause