@echo off
REM ===========================================================================
REM  D&D Docket - start the system
REM
REM  Double-click this to run the docket. Leave the black window open: closing
REM  it stops the system for everyone.
REM
REM  Colleagues reach it from their own computers at the address this window
REM  prints - nothing needs installing on theirs.
REM ===========================================================================
setlocal
cd /d "%~dp0"
title D^&D Docket - LEAVE THIS WINDOW OPEN

where node >nul 2>nul
if errorlevel 1 (
  echo.
  echo   Node.js is not installed. Run SETUP.cmd first.
  echo.
  pause
  exit /b 1
)

if not exist "node_modules" (
  echo.
  echo   This copy has not been set up yet. Running SETUP first...
  echo.
  call "%~dp0SETUP.cmd"
  if errorlevel 1 exit /b 1
)

REM dist\ is build output and is not copied between machines, so a freshly
REM transferred folder has none. Build it rather than failing with a module
REM error nobody outside programming can read.
if not exist "dist\server\index.js" (
  echo.
  echo   Preparing the program for this computer. One moment...
  call npm run build
  if errorlevel 1 (
    echo.
    echo   That did not work. Try running SETUP.cmd.
    echo.
    pause
    exit /b 1
  )
)

echo.
echo   Starting the docket. Give it a few seconds.
echo.
echo   ---------------------------------------------------------------------
echo    LEAVE THIS WINDOW OPEN while people are using the system.
echo    Closing it stops the docket for everyone.
echo   ---------------------------------------------------------------------
echo.

REM Open a browser shortly after the server comes up.
start "" /b cmd /c "timeout /t 6 >nul & start http://localhost:8787"

call npm start

echo.
echo   The docket has stopped.
echo.
pause
