@echo off
setlocal EnableExtensions
title BrickWorks Print Relay
rem Generated by the BrickWorks app (https://brickworks.app) - one-click print relay setup.
cd /d "%~dp0"

set "BASE=https://brickworks.app"
set "BAKEDKEY="
set "DIR=%LOCALAPPDATA%\BrickWorksRelay"
rem Node is downloaded THROUGH this app's server (a redirect to the pinned
rem build), so a Node version bump on the server fixes old setup files too.
set "NODEURL=%BASE%/api/print/setup/node?platform=win"

if exist "%DIR%\relay-config.json" goto UPDATE

echo.
echo   BrickWorks Print Relay - one-time setup
echo   ======================================
echo   This sets up the small program that sends your labels to the Brother
echo   label printer. It installs only into %DIR% - no admin rights needed.
echo.

if not exist "%DIR%\app\lib" mkdir "%DIR%\app\lib"

set "NODEEXE="
where node >nul 2>&1
if not errorlevel 1 set "NODEEXE=node"
if defined NODEEXE (
  "%NODEEXE%" -e "process.exit(+process.versions.node.split('.')[0]>=22?0:1)" >nul 2>&1
  if errorlevel 1 set "NODEEXE="
)
if not defined NODEEXE if exist "%DIR%\node\node.exe" set "NODEEXE=%DIR%\node\node.exe"
if defined NODEEXE goto FILES

echo   [1/3] Downloading Node.js (about 35 MB, one time only)...
curl.exe -L --fail --progress-bar -o "%DIR%\node.zip" "%NODEURL%"
if errorlevel 1 goto FAILNET
echo   Unpacking...
tar -xf "%DIR%\node.zip" -C "%DIR%"
if errorlevel 1 goto FAILNET
rem The archive unfolds as node-vX.Y.Z-win-x64 — match any version so a
rem server-side Node bump never breaks this file.
for /d %%D in ("%DIR%\node-v*-win-x64") do move /y "%%D" "%DIR%\node" >nul
if not exist "%DIR%\node\node.exe" goto FAILNET
del "%DIR%\node.zip" >nul 2>&1
set "NODEEXE=%DIR%\node\node.exe"

:FILES
echo   [2/3] Downloading the BrickWorks relay files...
call :GET "print-relay.js" "print-relay.js" || goto FAILNET
call :GET "relay-setup.js" "relay-setup.js" || goto FAILNET
call :GET "lib/print-key.js" "lib\print-key.js" || goto FAILNET
call :GET "lib/printer.js" "lib\printer.js" || goto FAILNET
call :GET "lib/brother-ql.js" "lib\brother-ql.js" || goto FAILNET
call :GET "lib/snmp.js" "lib\snmp.js" || goto FAILNET
call :GET "lib/discover.js" "lib\discover.js" || goto FAILNET
call :GET "lib/mdns.js" "lib\mdns.js" || goto FAILNET
if not exist "%DIR%\app\print-relay.js" goto FAILNET

echo   [3/3] Connecting to your printer...
"%NODEEXE%" "%DIR%\app\relay-setup.js" --base "%BASE%" --baked-key "%BAKEDKEY%" --dir "%DIR%"
exit /b %errorlevel%

:GET
curl.exe -L --fail -s -o "%DIR%\app\%~2" "%BASE%/api/print/relay/files/%~1"
exit /b %errorlevel%

rem Update variant: download to a temp name and swap in only on success, so a
rem dropped connection can never leave a truncated relay file behind.
:GETUP
curl.exe -L --fail -s -o "%DIR%\app\%~2.new" "%BASE%/api/print/relay/files/%~1"
if errorlevel 1 (echo   ^(offline - keeping the installed version^) & exit /b 0)
move /y "%DIR%\app\%~2.new" "%DIR%\app\%~2" >nul
exit /b 0

:FAILNET
echo.
echo   A download failed. Check this computer's internet connection, then
echo   run this file again. It picks up where it left off. If it keeps
echo   failing, download a fresh setup file from BrickWorks on your phone
echo   (Settings - Printer - New Printer) - this copy may be out of date.
echo.
pause
exit /b 1

rem An existing install: refresh the relay code from the server (tolerant —
rem a failed download keeps the installed version), then run. This is the
rem update path: re-running a freshly downloaded setup file upgrades.
:UPDATE
echo   Checking for relay updates…
if not exist "%DIR%\app\lib" mkdir "%DIR%\app\lib"
call :GETUP "print-relay.js" "print-relay.js"
call :GETUP "relay-setup.js" "relay-setup.js"
call :GETUP "lib/print-key.js" "lib\print-key.js"
call :GETUP "lib/printer.js" "lib\printer.js"
call :GETUP "lib/brother-ql.js" "lib\brother-ql.js"
call :GETUP "lib/snmp.js" "lib\snmp.js"
call :GETUP "lib/discover.js" "lib\discover.js"
call :GETUP "lib/mdns.js" "lib\mdns.js"

:RUN
if exist "%DIR%\node\node.exe" (set "NODEEXE=%DIR%\node\node.exe") else set "NODEEXE=node"
echo Starting the BrickWorks Print Relay - leave this window open.
"%NODEEXE%" "%DIR%\app\print-relay.js" --config "%DIR%\relay-config.json"
if errorlevel 1 pause
