@echo off
title Build Release (node-win64)
cd /d "%~dp0"

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

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

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

echo ==============================================
echo   Building Release: %ZIP_NAME%
echo ==============================================
echo.

:: Step 1: Build proxy-node.exe for Windows
echo [1/5] Building proxy-node for Windows (x86_64)...

set "PROXY_NODE_DIR=%~dp0..\proxy-node"
if not exist "%PROXY_NODE_DIR%\Cargo.toml" (
    echo ERROR: proxy-node project not found at %PROXY_NODE_DIR%
    pause
    exit /b 1
)

pushd "%PROXY_NODE_DIR%"
cargo build --release
if errorlevel 1 (
    echo.
    echo ERROR: Build failed!
    popd
    pause
    exit /b 1
)
popd
echo   [ok] Build succeeded.

:: Step 2: Copy proxy-node binary to bin
echo [2/5] Copying proxy-node.exe to bin...
set "BUILT_BINARY=%PROXY_NODE_DIR%\target\release\proxy-node.exe"
if not exist "%BUILT_BINARY%" (
    echo ERROR: Built binary not found at %BUILT_BINARY%
    pause
    exit /b 1
)
copy /Y "%BUILT_BINARY%" "bin\proxy-node.exe" >nul
for %%F in ("bin\proxy-node.exe") do echo   [ok] proxy-node.exe: %%~zF bytes

:: Step 3: Check frpc binary
echo [3/5] Checking frpc binary...
if not exist "bin\frpc.exe" (
    echo   [warn] frpc.exe not found in bin/
    echo   Please download frpc for Windows and place it in bin\frpc.exe
    pause
    exit /b 1
)
for %%F in ("bin\frpc.exe") do echo   [ok] frpc.exe: %%~zF bytes

:: Step 4: Clean old artifacts and legacy binaries
echo [4/5] Cleaning old artifacts from bin...
del /q "bin\*.log" 2>nul
del /q "bin\*.json" 2>nul
del /q "bin\*.toml" 2>nul
:: Remove legacy binaries (replaced by proxy-node.exe)
if exist "bin\proxy-agent.exe" del /q "bin\proxy-agent.exe"
if exist "bin\gost.exe" del /q "bin\gost.exe"
if exist "%ZIP_NAME%" del /q "%ZIP_NAME%"
echo   [ok] Clean.

:: Step 5: Compress + upload
echo [5/5] Compressing files...
powershell -Command "Compress-Archive -Path 'bin', '*.bat' -DestinationPath '%ZIP_NAME%' -Force"
echo   [ok] Package: %ZIP_NAME%

echo [6/6] 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 1
)
echo   [ok] Uploaded to 124.70.180.22.

echo.
echo ==============================================
echo   Build Complete!  %ZIP_NAME%
echo ==============================================
echo.
echo Package contents:
echo   bin/proxy-node.exe   Rust proxy (HTTP + SOCKS5 + heartbeat + FRPS selection)
echo   bin/frpc.exe         FRP client for port forwarding (managed by proxy-node)
echo   bin/start-node.ps1   Main startup script
echo   bin/stop-node.ps1    Stop script
echo   bin/monitor-node.ps1 Live status monitor
echo   bin/check-update.ps1 Auto-update checker
echo   bin/version.txt      Version identifier
echo   *.bat                User-facing wrappers
echo.
pause
