@echo on
setlocal
net session >nul 2>&1
if %errorlevel% neq 0 (
  echo [ERROR] Must run as Administrator.
  pause
  exit /b 1
)
cd /d "%~dp0"

set SVC=SurfGuardDns
set EXE=%CD%\SurfGuardDns.exe

echo ================================================
echo   Install SurfGuardDns as a Windows service
echo   binPath = %EXE% --service
echo ================================================

if not exist "%EXE%" (
  echo [FATAL] SurfGuardDns.exe not found in this folder.
  pause
  exit /b 2
)

echo Removing any existing service...
sc stop %SVC% >nul 2>&1
sc delete %SVC% >nul 2>&1

echo Creating service...
sc create %SVC% binPath= "\"%EXE%\" --service" start= auto DisplayName= "SurfGuard DNS Blocker"
if %errorlevel% neq 0 ( echo [FATAL] sc create failed. & pause & exit /b 3 )

echo Setting description...
sc description %SVC% "Local DNS proxy that blocks malicious and unwanted domains. Part of the TreeIQ security suite."

echo Setting failure recovery (auto-restart on crash)...
sc failure %SVC% reset= 86400 actions= restart/5000/restart/10000/restart/30000

echo Starting service...
sc start %SVC%
if %errorlevel% neq 0 ( echo [WARN] Start failed. Check %ProgramData%\SurfGuardDns\service.log & pause & exit /b 4 )

echo.
echo Service state:
sc query %SVC%
echo.
echo ================================================
echo   Service installed and running.
echo   Log:  %ProgramData%\SurfGuardDns\service.log
echo.
echo   NEXT: run 2-set-dns-loopback.bat  (as admin)
echo   so Windows sends queries to the service.
echo ================================================
pause
