@echo off
REM ===========================================================================
REM  D&D Docket - set this computer up as the one that serves the docket
REM
REM  Run this ONCE, on the ONE computer that will hold the register. Right-click
REM  it and choose "Run as administrator".
REM
REM  It does the four things that otherwise get forgotten:
REM    1. opens the firewall so colleagues can reach it
REM    2. starts the docket automatically when the computer starts, with no
REM       window to close by accident
REM    3. stops the computer going to sleep and taking the docket down with it
REM    4. schedules the daily backup
REM
REM  IMPORTANT: only ONE computer runs this. Everyone else opens the docket in
REM  a browser or as an installed app - see MAKE-DESKTOP-ICON.cmd.
REM ===========================================================================
setlocal EnableDelayedExpansion
cd /d "%~dp0"
title D^&D Docket - Server setup

net session >nul 2>nul
if errorlevel 1 (
  echo.
  echo   This has to run as administrator.
  echo.
  echo   Close this window, right-click SETUP-SERVER.cmd, and choose
  echo   "Run as administrator".
  echo.
  pause
  exit /b 1
)

echo.
echo   =====================================================================
echo    Setting this computer up to serve the D^&D Docket
echo   =====================================================================
echo.

if not exist "dist\server\index.js" (
  echo   The program has not been built on this computer yet.
  echo   Run SETUP.cmd first, then come back to this one.
  echo.
  pause
  exit /b 1
)

set "PORT=8787"
REM The invoice application runs from the same program on the next port.
set "BILLING_PORT=8788"
set "HERE=%~dp0"
if "%HERE:~-1%"=="\" set "HERE=%HERE:~0,-1%"

REM --- 1. firewall ---------------------------------------------------------
echo   [1/4] Opening the firewall on ports %PORT% and %BILLING_PORT% ...
powershell -NoProfile -Command ^
  "Remove-NetFirewallRule -DisplayName 'D&D Docket' -ErrorAction SilentlyContinue;" ^
  "New-NetFirewallRule -DisplayName 'D&D Docket' -Direction Inbound -Protocol TCP -LocalPort %PORT%,%BILLING_PORT% -Action Allow -Profile Private,Domain | Out-Null"
if errorlevel 1 (echo         could not add the rule - add it by hand, see docs\INSTALL.md) else (echo         done - private and domain networks)

REM --- 2. start with the computer, with no window ---------------------------
REM  "Run whether user is logged on or not" runs it in the background session,
REM  so there is no black window anybody can close. That single change is what
REM  makes this behave like installed software rather than something somebody
REM  has to remember to launch.
echo   [2/4] Starting the docket automatically at startup ...
schtasks /delete /tn "D&D Docket" /f >nul 2>nul
schtasks /create /tn "D&D Docket" ^
  /tr "\"%SystemRoot%\System32\cmd.exe\" /c cd /d \"%HERE%\" ^& npm start" ^
  /sc onstart /ru SYSTEM /rl highest /f >nul
if errorlevel 1 (
  echo         could not create the task - see docs\INSTALL.md
) else (
  echo         done - task "D^&D Docket", runs at startup with no window
)

REM --- 3. no sleeping ------------------------------------------------------
echo   [3/4] Stopping this computer going to sleep ...
powercfg /change standby-timeout-ac 0 >nul 2>nul
powercfg /change hibernate-timeout-ac 0 >nul 2>nul
powercfg /change monitor-timeout-ac 15 >nul 2>nul
echo         done - screen still sleeps after 15 minutes, the computer does not

REM --- 4. daily backup -----------------------------------------------------
echo   [4/4] Scheduling the daily backup ...
schtasks /delete /tn "D&D Docket backup" /f >nul 2>nul
schtasks /create /tn "D&D Docket backup" /tr "\"%HERE%\BACKUP.cmd\"" ^
  /sc daily /st 15:00 /ru SYSTEM /rl highest /f >nul
if errorlevel 1 (echo         could not schedule it - run BACKUP.cmd by hand) else (echo         done - every day at 15:00, into backups\)

REM --- addresses -----------------------------------------------------------
echo.
echo   =====================================================================
echo    Done. Hand these addresses to the others:
echo   =====================================================================
echo.
for /f "tokens=*" %%A in ('powershell -NoProfile -Command ^
  "Get-NetIPAddress -AddressFamily IPv4 ^| Where-Object { $_.IPAddress -notlike '127.*' -and $_.IPAddress -notlike '169.254.*' } ^| ForEach-Object { $_.IPAddress }"') do (
  echo        http://%%A:%PORT%
)
echo.
echo    On this computer:  http://localhost:%PORT%
echo.
echo   ---------------------------------------------------------------------
echo    Two things this cannot do for you:
echo.
echo    * Ask whoever runs your network for a FIXED address for this
echo      computer. If the address changes, everyone's shortcut breaks.
echo.
echo    * Copy backups\ somewhere off this computer every week. A backup on
echo      the same disk does not survive the disk failing.
echo   ---------------------------------------------------------------------
echo.
echo    Starting the docket now...
echo.
schtasks /run /tn "D&D Docket" >nul 2>nul
timeout /t 8 >nul
powershell -NoProfile -Command ^
  "try { $r = Invoke-WebRequest -UseBasicParsing -Uri 'http://localhost:%PORT%/api/health' -TimeoutSec 5; Write-Host '    The docket is running.' } catch { Write-Host '    Not responding yet - give it a minute, or run START-DOCKET.cmd to see why.' }"
echo.
pause
