@echo off
REM ===========================================================================
REM  D&D Docket - back up the register and the documents
REM
REM  Double-click this any time. It also runs by itself every day at 15:00,
REM  scheduled by SETUP-SERVER.cmd.
REM
REM  Safe while people are working: the register is read as a consistent
REM  snapshot through SQLite's own backup, not copied file by file. Copying
REM  docket.sqlite by hand while the docket is running gives you a file that
REM  opens cleanly and is quietly out of date, which is the worst way for a
REM  backup to fail.
REM
REM  What it cannot do for you: get the copy OFF this computer. A backup on
REM  the same disk does not survive the disk failing.
REM ===========================================================================
setlocal
cd /d "%~dp0"
title D^&D Docket - Backup

where node >nul 2>nul
if errorlevel 1 (
  echo.
  echo   Node.js is not installed, so the backup cannot run.
  echo   Install it from https://nodejs.org and run INSTALL.cmd again.
  echo.
  if not "%1"=="/quiet" pause
  exit /b 1
)

if not exist "node_modules" (
  echo.
  echo   This folder has not been set up yet - run INSTALL.cmd first.
  echo.
  if not "%1"=="/quiet" pause
  exit /b 1
)

echo.
echo   Backing up...
call npm run --silent backup
if errorlevel 1 (
  echo.
  echo   The backup did NOT finish. Check the disk is not full and that the
  echo   backup folder in Settings still exists.
  echo.
  if not "%1"=="/quiet" pause
  exit /b 1
)

echo.
if not "%1"=="/quiet" pause
exit /b 0
