@echo off
setlocal EnableDelayedExpansion
title QR Printer Setup — 4BARCODE 4B-3044TG

:: Server URL (auto-filled by the web app)
set "SERVER_URL=http://13.232.65.18/node_modules/documents/qr_printer_driver"

:: ── 1. Request Administrator privileges ───────────────────────────
net session >nul 2>&1
if %errorLevel% NEQ 0 (
    powershell -Command "Start-Process '%~f0' -Verb RunAs"
    exit /b
)

cls
echo.
echo  =====================================================
echo   QR Printer Setup  ^|  4BARCODE 4B-3044TG
echo  =====================================================
echo.
echo  NOTE: Make sure BarTender and 4BARCODE printer
echo  drivers are already installed on this computer
echo  before continuing.
echo.
echo  Make sure the printer is POWERED ON and on the
echo  same network as this computer.
echo.
pause

:: ── 2. Set working folder ─────────────────────────────────────────
set "SCRIPT_DIR=%~dp0"
set "PS_SCRIPT=%SCRIPT_DIR%setup_printer.ps1"
set "DAT_FILE=%SCRIPT_DIR%printer_config.dat"

:: ── 3. Download printer_config.dat ────────────────────────────────
echo.
echo  [Step 1/3] Downloading printer_config.dat ...
powershell -NoProfile -Command "Invoke-WebRequest -Uri '%SERVER_URL%/printer_config.dat' -OutFile '%DAT_FILE%' -UseBasicParsing"
if not exist "%DAT_FILE%" (
    echo.
    echo  ERROR: Could not download printer_config.dat
    echo  Make sure this PC can reach: %SERVER_URL%
    pause
    exit /b 1
)
echo             [OK] printer_config.dat downloaded.

:: ── 4. Download setup_printer.ps1 ─────────────────────────────────
echo.
echo  [Step 2/3] Downloading setup_printer.ps1 ...
powershell -NoProfile -Command "Invoke-WebRequest -Uri '%SERVER_URL%/setup_printer.ps1' -OutFile '%PS_SCRIPT%' -UseBasicParsing"
if not exist "%PS_SCRIPT%" (
    echo.
    echo  ERROR: Could not download setup_printer.ps1
    echo  Make sure this PC can reach: %SERVER_URL%
    pause
    exit /b 1
)
echo             [OK] setup_printer.ps1 downloaded.

:: ── 5. Get printer IP ─────────────────────────────────────────────
echo.
echo  [Step 3/3] Network printer connection
echo.
echo  Enter the IP address shown on the printer's LCD panel.
echo  (Ask your IT admin if you are unsure.)
echo.
set /p PRINTER_IP="  Printer IP address [e.g. 192.168.1.100]: "

if "%PRINTER_IP%"=="" (
    echo  ERROR: No IP entered. Exiting.
    pause
    exit /b 1
)

echo.
echo             Testing connection to %PRINTER_IP% ...
ping -n 2 -w 1500 %PRINTER_IP% >nul 2>&1
if %errorLevel% NEQ 0 (
    echo.
    echo  WARNING: Could not reach %PRINTER_IP%
    echo  The printer may be off or on a different network.
    echo.
    choice /C YN /M "  Continue anyway?"
    if !errorLevel! EQU 2 exit /b 1
) else (
    echo             [OK] Printer is reachable.
)

:: ── 6. Run setup_printer.ps1 ──────────────────────────────────────
powershell -NoProfile -ExecutionPolicy Bypass -File "%PS_SCRIPT%" -PrinterIP "%PRINTER_IP%" -DatFile "%DAT_FILE%"
if %errorLevel% NEQ 0 (
    echo.
    echo  ERROR: Printer setup failed. See messages above.
    pause
    exit /b 1
)

:: ── 7. Done ───────────────────────────────────────────────────────
echo.
echo  =====================================================
echo   Setup Complete!
echo  =====================================================
echo.
echo   Printer : 4BARCODE 4B-3044TG
echo   IP      : %PRINTER_IP%
echo.
echo   You can now print QR labels from the web application.
echo.
pause
endlocal