@echo off
:: Silent Node Startup - called by Windows Task Scheduler at logon.
:: Performs the same session sandbox setup as "Start Node.bat" but runs
:: completely silently (no monitor window, no console output).
cd /d "%~dp0"

:: Kill any leftover processes from a previous session
powershell -Command "Stop-Process -Name 'proxy-node', 'proxy-agent', 'frpc', 'gost' -Force -ErrorAction SilentlyContinue" >nul 2>&1

:: Check for updates silently before starting
powershell -ExecutionPolicy Bypass -File "bin\check-update.ps1" >nul 2>&1
if %ERRORLEVEL% EQU 1 (
    start "" "%~f0"
    exit /b
)

:: Sweep old session folders
if exist ".tmp\session_*" for /d %%D in (".tmp\session_*") do rmdir /s /q "%%D" >nul 2>&1
if exist ".tmp\active_session.txt" del /q ".tmp\active_session.txt" >nul 2>&1

:: Generate unique session ID
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set datetime=%%I
set SESSION_NAME=session_%datetime:~0,14%

if not exist ".tmp" mkdir ".tmp"
mkdir ".tmp\%SESSION_NAME%"

:: Copy bin contents to the fresh session folder
xcopy "bin\*" ".tmp\%SESSION_NAME%\" /E /Y /Q >nul

:: Save active session pointer
echo %SESSION_NAME% > ".tmp\active_session.txt"

:: Run the silent starter (no monitor)
cd /d ".tmp\%SESSION_NAME%"
call start_silent.bat
