@echo off
REM ===========================================================================
REM  D&D Invoices - put an icon on this person's desktop
REM
REM  Only for the people who bill. Everyone else has no reason to see it.
REM
REM  The invoice application runs from the same program as the docket, on the
REM  next port (8788), with the same logins. Like the docket icon, this opens
REM  it in its own window and installs nothing.
REM ===========================================================================
setlocal EnableDelayedExpansion
title D^&D Invoices - Desktop icon

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

set "ADDR=http://localhost:8788"
echo   Where is it running? Same computer as the docket, port 8788.
echo   It looks like  http://192.168.0.12:8788
echo.
set /p ADDR="   Address [%ADDR%]: "
if "%ADDR%"=="" set "ADDR=http://localhost:8788"

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%:8788"

echo.
echo   Checking %ADDR% ...
powershell -NoProfile -Command ^
  "try { Invoke-WebRequest -UseBasicParsing -Uri '%ADDR%/api/session' -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 running on that computer? Invoices start with it.
  echo     * is the address right - port 8788, not 8787?
  echo     * was SETUP-SERVER.cmd run again on that computer since invoicing
  echo       was added? It opens the firewall for 8788.
  echo.
) else (
  echo   Reached it.
  echo.
)

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
)

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 Invoices.lnk');" ^
  "$s.TargetPath = '%BROWSER%';" ^
  "$s.Arguments  = '--app=%ADDR%';" ^
  "$s.IconLocation = '%ICON%';" ^
  "$s.Description = 'Invoicing 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 Invoices" icon on the desktop.
echo   =====================================================================
echo.
echo    Sign in with the same username and password as the docket.
echo.
pause
