@echo off
REM ===========================================================================
REM  D&D Docket - make a clean copy to move to another computer
REM
REM  Double-click this. It builds a folder you can copy to a pen drive or over
REM  the network, containing the program and nothing it should not carry.
REM
REM  LEFT OUT, on purpose:
REM    node_modules  a program compiled for THIS computer's processor and Node
REM                  version. Copying it is the single most common way a
REM                  transfer fails, and the error looks like a broken
REM                  database rather than a wrong file.
REM    dist          build output; rebuilt on the new computer in seconds.
REM    backups       old copies of the register. They belong on a backup disk,
REM                  not inside the thing being backed up.
REM    *.log         start-up noise.
REM
REM  INCLUDED, if you choose:
REM    data          the register, the documents, the audit trail, the users.
REM                  Say YES when you are MOVING the firm's docket.
REM                  Say NO for a fresh installation that starts empty.
REM ===========================================================================
setlocal EnableDelayedExpansion
cd /d "%~dp0"
title D^&D Docket - Make a transfer copy

echo.
echo   =====================================================================
echo    D^&D Docket - clean copy for another computer
echo   =====================================================================
echo.

if not exist "package.json" (
  echo   This does not look like the docket folder. Run it from inside it.
  echo.
  pause
  exit /b 1
)

REM --- with or without the firm's data? ------------------------------------
set "WITHDATA="
if exist "data\docket.sqlite" (
  echo   This folder holds a register.
  echo.
  echo     Y  Move the firm's docket  - the register, documents and users
  echo                                  come with it. The new computer takes
  echo                                  over; stop using this one.
  echo.
  echo     N  Fresh installation      - program only. The new computer starts
  echo                                  empty and loads MainDB.xlsx itself.
  echo.
  set /p WITHDATA="   Include the register? [Y/N]: "
) else (
  echo   There is no register in this folder, so this will be a program-only copy.
  set "WITHDATA=N"
)

if /i "%WITHDATA%"=="Y" (set "COPYDATA=1") else (set "COPYDATA=0")

REM --- where to put it ------------------------------------------------------
for /f "delims=" %%I in ('powershell -NoProfile -Command "Get-Date -Format yyyy-MM-dd"') do set STAMP=%%I
set "DEST=%~dp0transfer\docket-%STAMP%"
if "%COPYDATA%"=="1" set "DEST=%~dp0transfer\docket-with-register-%STAMP%"

echo.
echo   Building the copy in:
echo       %DEST%
echo.

if exist "%DEST%" rd /s /q "%DEST%" 2>nul
mkdir "%DEST%" 2>nul

REM robocopy, not xcopy: it handles long paths, it is on every Windows, and
REM /XD takes a list of folders to leave behind.
set "SKIP=node_modules dist backups transfer .git"
if "%COPYDATA%"=="0" set "SKIP=%SKIP% data"

robocopy "%~dp0." "%DEST%" /E /XD %SKIP% /XF *.log /NFL /NDL /NJH /NJS /NC /NS >nul
if errorlevel 8 (
  echo   The copy failed. Is the disk full, or a file open in another program?
  echo.
  pause
  exit /b 1
)

REM --- a note for whoever opens it on the other side ------------------------
> "%DEST%\READ-ME-FIRST.txt" (
  echo D^&D Docket - transfer copy made %STAMP%
  echo.
  if "%COPYDATA%"=="1" (
    echo This copy INCLUDES the firm's register, documents, users and audit trail.
    echo The computer it came from should stop serving the docket once this one
    echo is running - two computers must never serve the same register.
  ) else (
    echo This copy is the PROGRAM ONLY. It starts with an empty register and
    echo will offer to load MainDB.xlsx when you set it up.
  )
  echo.
  echo On the new computer:
  echo.
  echo   1. Copy this whole folder somewhere sensible, e.g. C:\docket
  echo   2. Install Node.js ^(the LTS version^) from https://nodejs.org
  echo   3. Double-click SETUP.cmd          - installs what it needs
  echo   4. Right-click SETUP-SERVER.cmd    - "Run as administrator"
  echo   5. Write down the address it prints, and run MAKE-DESKTOP-ICON.cmd
  echo      on each person's computer with that address.
  echo.
  echo node_modules is deliberately absent. SETUP.cmd fetches the right one for
  echo that computer. Copying it from here is the usual reason a transfer fails.
  echo.
  echo The full guide, in English and Bangla, is GUIDE.html in this folder.
)

REM --- report ---------------------------------------------------------------
for /f %%A in ('dir /s /b "%DEST%" 2^>nul ^| find /c /v ""') do set FILES=%%A
echo   =====================================================================
echo    Done - %FILES% file^(s^)
echo   =====================================================================
echo.
echo    %DEST%
echo.
if "%COPYDATA%"=="1" (
  echo    This copy INCLUDES the register. Once the new computer is running,
  echo    stop the docket on this one - two computers must never serve the
  echo    same register.
) else (
  echo    Program only. The new computer will start with an empty register.
)
echo.
echo    Copy that folder across, then read READ-ME-FIRST.txt inside it.
echo.
pause
