@echo off
title Build Release
cd /d "%~dp0"

if not exist "bin\version.txt" (
    echo Error: bin\version.txt not found!
    pause
    exit /b
)

:: Read the version into a variable (for /f is more reliable than set /p)
for /f "usebackq delims=" %%V in ("bin\version.txt") do set "NODE_VERSION=%%V"

set ZIP_NAME=node-win64.%NODE_VERSION%.zip

echo ==============================================
echo   Packaging Release: %ZIP_NAME%
echo ==============================================
echo.

echo [1/3] Cleaning old log and config files from bin...
if exist "bin\*.log" del /q "bin\*.log" 2>nul
if exist "bin\*.json" del /q "bin\*.json" 2>nul
if exist "bin\*.toml" del /q "bin\*.toml" 2>nul

if exist "%ZIP_NAME%" (
    echo Removing old %ZIP_NAME%...
    del /q "%ZIP_NAME%"
)

echo [2/3] Compressing files...
:: Compress the bin folder and all top-level .bat files
powershell -Command "Compress-Archive -Path 'bin', '*.bat' -DestinationPath '%ZIP_NAME%' -Force"
echo   [ok] Package: %ZIP_NAME%

echo [3/3] Uploading to 124.70.180.22...
scp "%ZIP_NAME%" node-win64_install.ps1 root@124.70.180.22:/srv/static/proxy-node-win64/
if %ERRORLEVEL% NEQ 0 (
    echo   ERROR: Upload failed!
    pause
    exit /b
)
echo   [ok] Uploaded to 124.70.180.22.

echo.
echo ==============================================
echo   Build Complete!  %ZIP_NAME%
echo ==============================================
pause

