@echo off
REM =====================================================================
REM  install-counter-helper.cmd
REM
REM  Installs the Caresoft Counter Helper on a Windows billing counter.
REM  Run once per till, as Administrator, from the folder holding
REM  CounterHelper.exe.
REM
REM  =================================================================
REM  WHY THIS EXISTS
REM  =================================================================
REM
REM  The README documented the sc commands and left a partner to type
REM  them at every till. Six commands typed by hand at thirty counters is
REM  thirty chances to mistype a path, skip the firewall rule, or forget
REM  the recovery settings — and each mistake surfaces weeks later as a
REM  counter that stops printing after a reboot.
REM
REM  The Helper is Windows-native on purpose: raw printing goes through
REM  winspool.drv, which is how an old LX-300 on a parallel-to-USB
REM  adapter gets bytes without a driver dialog. That cannot be done from
REM  the Linux server the rest of the platform runs on.
REM =====================================================================

setlocal EnableDelayedExpansion
set SVC=CaresoftCounterHelper
set DISPLAY=Caresoft Counter Helper
set TARGET=C:\Program Files\Caresoft
set DATA=C:\ProgramData\CaresoftCounter

echo.
echo  Caresoft Counter Helper — install
echo  =================================
echo.

REM ---- 1. Administrator ------------------------------------------------
net session >nul 2>&1
if %errorlevel% neq 0 (
  echo  This must run as Administrator.
  echo  Right-click the file and choose "Run as administrator".
  echo.
  exit /b 1
)

REM ---- 2. The binary must be beside this script ------------------------
if not exist "%~dp0CounterHelper.exe" (
  echo  CounterHelper.exe is not in this folder.
  echo  Copy the whole release folder to the counter and run it from there.
  echo.
  exit /b 1
)

REM ---- 3. Stop and remove any previous install -------------------------
REM  Re-running this must be safe. A partner who is unsure whether the
REM  install worked will run it again, and that must not produce two
REM  services fighting over one port.
sc query %SVC% >nul 2>&1
if %errorlevel% equ 0 (
  echo  [1/6] Existing service found — stopping it
  sc stop %SVC% >nul 2>&1
  timeout /t 3 /nobreak >nul
  sc delete %SVC% >nul 2>&1
  timeout /t 2 /nobreak >nul
) else (
  echo  [1/6] No previous install
)

REM ---- 4. Copy the files -----------------------------------------------
echo  [2/6] Copying to %TARGET%
if not exist "%TARGET%" mkdir "%TARGET%"
copy /Y "%~dp0CounterHelper.exe" "%TARGET%\" >nul
if %errorlevel% neq 0 (
  echo  Could not copy CounterHelper.exe — is it running?
  exit /b 1
)
if exist "%~dp0*.dll" copy /Y "%~dp0*.dll" "%TARGET%\" >nul 2>&1

REM ---- 5. Data directory -----------------------------------------------
REM  The spool lives here. Jobs are files, so a power cut loses nothing —
REM  but only if the directory survives an uninstall, which is why it is
REM  under ProgramData and not Program Files.
echo  [3/6] Data directory %DATA%
if not exist "%DATA%"          mkdir "%DATA%"
if not exist "%DATA%\spool"     mkdir "%DATA%\spool"
if not exist "%DATA%\telemetry" mkdir "%DATA%\telemetry"

REM ---- 6. Register the service ----------------------------------------
echo  [4/6] Registering the service
sc create %SVC% binPath= "\"%TARGET%\CounterHelper.exe\"" start= auto ^
   DisplayName= "%DISPLAY%" obj= LocalSystem >nul
if %errorlevel% neq 0 (
  echo  Could not create the service.
  exit /b 1
)
sc description %SVC% "Raw printing and hardware for the billing counter" >nul

REM  RESTART ON FAILURE. A counter that stops printing until somebody
REM  notices is a counter that stops printing for the rest of the day.
REM  Restart after 5s, 5s, then every 60s; reset the counter daily.
sc failure %SVC% reset= 86400 actions= restart/5000/restart/5000/restart/60000 >nul

REM ---- 7. Firewall -----------------------------------------------------
REM  The helper listens on LOOPBACK only, so no inbound rule is needed —
REM  and adding one would open a port that has no business being open.
REM  The rule below explicitly BLOCKS remote access, so a misconfigured
REM  machine cannot expose the print API to the shop's wifi.
echo  [5/6] Firewall — blocking remote access to the helper port
netsh advfirewall firewall delete rule name="Caresoft Counter Helper (block remote)" >nul 2>&1
netsh advfirewall firewall add rule name="Caresoft Counter Helper (block remote)" ^
   dir=in action=block protocol=TCP localport=7331 remoteip=any >nul 2>&1

REM ---- 8. Start --------------------------------------------------------
echo  [6/6] Starting
sc start %SVC% >nul
timeout /t 4 /nobreak >nul

sc query %SVC% | find "RUNNING" >nul
if %errorlevel% neq 0 (
  echo.
  echo  The service did not start. Check %DATA%\helper.log
  echo.
  exit /b 1
)

echo.
echo  Running.
echo.
echo  Next, and it is not optional:
echo.
echo    1. Open %DATA%\helper.json and copy the "token" value.
echo    2. Paste it into the setup wizard on the counter's browser.
echo       Nothing prints until the node and the helper share that token.
echo    3. Print a test slip from the wizard BEFORE the shop opens.
echo.
echo  If the slip is blank or garbled, it is the printer profile, not the
echo  software — try the next profile in the list rather than reinstalling.
echo.
endlocal
exit /b 0
