@echo off
chcp 65001 >nul
cd /d "%~dp0"

set PATH=%~dp0Ruby32-x64\bin;%PATH%
set GEM_HOME=%~dp03.2.0
set GEM_PATH=%~dp03.2.0
set RACK_ENV=production
set DATABASE_PATH=%~dp0data\client.db

if not exist "%~dp0Ruby32-x64\bin\ruby.exe" (
    echo [ERROR] Ruby not found: %~dp0Ruby32-x64\bin\ruby.exe
    pause
    exit /b 1
)

if not exist "%~dp0data" mkdir "%~dp0data"

set SECRET_FILE=%~dp0data\session_secret
if not exist "%SECRET_FILE%" (
    echo Generating session secret...
    ruby -rsecurerandom -e "puts SecureRandom.base64(48)" > "%SECRET_FILE%"
)

set /p SESSION_SECRET=<"%SECRET_FILE%"

cd /d "%~dp0src"

if not exist "app.rb" (
    echo [ERROR] app.rb not found
    pause
    exit /b 1
)

echo Starting NetCat Client...
ruby app.rb

if errorlevel 1 (
    echo.
    echo Startup failed
    pause
)
