@echo off
REM ===========================================================================
REM  D&D Docket - first-time setup on a new computer
REM
REM  Double-click this once after copying the folder across. It installs what
REM  the app needs and loads the trademark register from MainDB.xlsx.
REM
REM  Safe to run again: it will not overwrite an existing register without
REM  asking.
REM ===========================================================================
setlocal
cd /d "%~dp0"
title D^&D Docket - Setup

echo.
echo   =====================================================================
echo    D^&D Docket - setting up on this computer
echo   =====================================================================
echo.

REM --- 1. Node -------------------------------------------------------------
where node >nul 2>nul
if errorlevel 1 (
  echo   Node.js is not installed on this computer.
  echo.
  echo   The docket needs it. Install the LTS version from:
  echo       https://nodejs.org
  echo.
  echo   Then close this window and double-click SETUP.cmd again.
  echo.
  pause
  exit /b 1
)

for /f "delims=" %%v in ('node --version') do set NODEVER=%%v
echo   Node.js %NODEVER% found.
echo.

REM --- 2. Dependencies -----------------------------------------------------
echo   Installing what the app needs. This takes a minute the first time.
echo   (It downloads from the internet - you only need a connection now,
echo    never again once this finishes.)
echo.
call npm install --no-audit --no-fund
if errorlevel 1 (
  echo.
  echo   Installation failed. The usual cause is no internet connection.
  echo   Check the connection and run SETUP.cmd again.
  echo.
  pause
  exit /b 1
)
echo.
echo   Installed.
echo.

REM --- 3. Build ------------------------------------------------------------
REM  The app runs from compiled output in dist\. That folder is deliberately
REM  NOT copied between machines, so it has to be built here. Skipping this is
REM  what makes a fresh copy fail at startup with "Cannot find module".
echo   Preparing the program...
call npm run build
if errorlevel 1 (
  echo.
  echo   The build failed. Nothing will run until this is sorted.
  echo.
  pause
  exit /b 1
)
echo   Ready.
echo.

REM --- 4. The register -----------------------------------------------------
if exist "data\docket.sqlite" (
  echo   There is already a register in this folder.
  echo   Setup will leave it alone.
  echo.
  goto :done
)

echo   Now loading the trademark register.
echo.
set "XLSX="
set "FOUND="
if exist "..\MainDB.xlsx" set "FOUND=..\MainDB.xlsx"
if exist "MainDB.xlsx" set "FOUND=MainDB.xlsx"

REM Asked, never assumed: a clean installation must be able to start empty
REM even when the spreadsheet happens to sit beside the folder.
if defined FOUND set /p "LOADIT=   %FOUND% found. Load it? Y = load every record, N = start empty [Y/N]: "
if defined FOUND if /i "%LOADIT%"=="Y" set "XLSX=%FOUND%"
if defined FOUND if /i not "%LOADIT%"=="Y" goto :empty

if "%XLSX%"=="" (
  echo   MainDB.xlsx was not found next to this folder.
  echo.
  set /p XLSX="   Type the full path to MainDB.xlsx (or press Enter to skip): "
)

if "%XLSX%"=="" goto :empty
goto :load

:empty
(
  echo.
  echo   Starting empty. You can load the spreadsheet later by running this here:
  echo       npm run import -- "C:\path\to\MainDB.xlsx"
  echo.
  goto :done
)

:load
call npm run import -- "%XLSX%"
if errorlevel 1 (
  echo.
  echo   The import did not finish. Check the path and try SETUP.cmd again.
  echo.
  pause
  exit /b 1
)

:done
echo.
echo   =====================================================================
echo    Setup finished.
echo   =====================================================================
echo.
echo    Next: double-click  START-DOCKET.cmd
echo.
echo    The first person to open it creates the first account, which is a
echo    partner (the administrator). Everyone else is added from inside the
echo    app, on the Users page.
echo.
echo    Read HOW-TO-USE.txt in this folder before you start.
echo.
pause
