@echo off
REM WakaTraffic Server Startup Script
REM This script kills all Node.js processes and starts the server fresh

echo.
echo ========================================
echo  WakaTraffic Server Manager
echo ========================================
echo.

REM Kill all Node.js processes
echo [1/3] Killing all Node.js processes...
taskkill /IM node.exe /F /T >nul 2>&1

REM Wait for processes to be killed
timeout /t 2 /nobreak

REM Verify no Node processes are running
tasklist | findstr /I "node.exe" >nul
if %errorlevel% equ 0 (
    echo [!] Node.js processes still running. Trying again...
    taskkill /IM node.exe /F /T
    timeout /t 2 /nobreak
) else (
    echo [OK] All Node.js processes killed
)

echo.
echo [2/3] Clearing any remaining connections on port 5000...
netstat -ano | findstr :5000 >nul
if %errorlevel% equ 0 (
    for /f "tokens=5" %%a in ('netstat -ano ^| findstr :5000') do (
        taskkill /PID %%a /F >nul 2>&1
    )
    echo [OK] Port 5000 cleared
)

echo.
echo [3/3] Starting WakaTraffic Server...
echo.

REM Navigate to project directory
cd /d C:\xampp\htdocs\waka_traffic

REM Start the server
node server.js

REM If server crashes, keep window open
pause
