@echo off
REM ===========================================================================
REM  D&D Docket - put an icon on this person's desktop
REM
REM  Run this on EACH person's computer. It asks nothing except where the
REM  docket is running, and leaves a "D&D Docket" icon on the desktop that
REM  opens it in its own window - no address bar, no tabs, its own taskbar
REM  button. It behaves like any other program on the machine.
REM
REM  Nothing is installed. It uses the browser that is already on Windows.
REM
REM  This does NOT put the register on this computer. The register lives on
REM  one machine and this is a window onto it, which is the only safe shape
REM  when five people share one docket.
REM ===========================================================================
setlocal EnableDelayedExpansion
title D^&D Docket - Desktop icon

echo.
echo   =====================================================================
echo    D^&D Docket - desktop icon
echo   =====================================================================
echo.

REM Default to this machine, which is right when run on the server itself.
set "ADDR=http://localhost:8787"
echo   Where is the docket running?
echo.
echo   The computer that serves it prints its address when it starts, and
echo   SETUP-SERVER.cmd prints it too. It looks like  http://192.168.0.12:8787
echo.
set /p ADDR="   Address [%ADDR%]: "
if "%ADDR%"=="" set "ADDR=http://localhost:8787"

REM Tolerate someone pasting just the IP, or leaving the port off.
echo %ADDR% | findstr /i "^http" >nul || set "ADDR=http://%ADDR%"
echo %ADDR% | findstr ":[0-9]" >nul || set "ADDR=%ADDR%:8787"

echo.
echo   Checking %ADDR% ...
powershell -NoProfile -Command ^
  "try { Invoke-WebRequest -UseBasicParsing -Uri '%ADDR%/api/health' -TimeoutSec 6 | Out-Null; exit 0 } catch { exit 1 }"
if errorlevel 1 (
  echo.
  echo   Could not reach it. The icon will still be made, but check:
  echo     * is the docket computer switched on, and the docket running?
  echo     * is the address right?
  echo     * did SETUP-SERVER.cmd run on that computer ^(firewall^)?
  echo.
) else (
  echo   Reached it.
  echo.
)

REM Find a Chromium browser. Edge is on every Windows 10 and 11 machine, so
REM this effectively always succeeds; Chrome is accepted if someone prefers it.
set "BROWSER="
for %%P in (
  "%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"
  "%ProgramFiles%\Microsoft\Edge\Application\msedge.exe"
  "%ProgramFiles%\Google\Chrome\Application\chrome.exe"
  "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"
) do if not defined BROWSER if exist %%P set "BROWSER=%%~P"

if not defined BROWSER (
  echo   No Edge or Chrome found on this computer, which is unusual.
  echo   Open %ADDR% in whatever browser it has instead.
  echo.
  pause
  exit /b 1
)

REM The icon: use the one served by the docket if this folder is present,
REM otherwise fall back to the browser's own.
set "ICON=%~dp0web\docket.ico"
if not exist "%ICON%" set "ICON=%BROWSER%,0"

powershell -NoProfile -Command ^
  "$s = (New-Object -ComObject WScript.Shell).CreateShortcut([Environment]::GetFolderPath('Desktop') + '\D&D Docket.lnk');" ^
  "$s.TargetPath = '%BROWSER%';" ^
  "$s.Arguments  = '--app=%ADDR%';" ^
  "$s.IconLocation = '%ICON%';" ^
  "$s.Description = 'Trademark docket for Doulah & Doulah';" ^
  "$s.Save()"

if errorlevel 1 (
  echo   Could not create the shortcut.
  echo.
  pause
  exit /b 1
)

echo   =====================================================================
echo    Done. There is now a "D^&D Docket" icon on the desktop.
echo   =====================================================================
echo.
echo    Double-click it and the docket opens in its own window.
echo.
echo    To put it on the taskbar as well: open it once, then right-click its
echo    taskbar button and choose "Pin to taskbar".
echo.
echo    If the address of the docket computer ever changes, run this again.
echo.
pause
