@echo off
REM Khimera Portable - Windows Launcher
REM Automatically launches the Windows executable with portable settings

title Khimera Portable v0.9 - Launcher

cls

echo.
echo ========================================================
echo.
echo          KHIMERA PORTABLE v0.9
echo       Privacy-First P2P Messenger
echo          Windows Launcher
echo.
echo ========================================================
echo.

REM Get script directory
set "SCRIPT_DIR=%~dp0"
cd /d "%SCRIPT_DIR%"

echo [System Detection]
echo --------------------------------------------------------
echo   OS:           Windows %PROCESSOR_ARCHITECTURE%
echo   Location:     %SCRIPT_DIR%
echo.

REM Check if running from removable drive
set "DRIVE_LETTER=%SCRIPT_DIR:~0,1%"
set "IS_REMOVABLE=0"

REM Check if drive letter is E: or higher (typical USB range)
if "%DRIVE_LETTER%" GEQ "E" (
    set "IS_REMOVABLE=1"
    echo   Status:       Running from removable drive
) else (
    echo   Status:       Running from local drive
)
echo.

REM Check if executable exists
if not exist "Windows\khimera.exe" (
    echo.
    echo ========================================================
    echo   ERROR: Windows executable not found!
    echo ========================================================
    echo.
    echo Expected location: Windows\khimera.exe
    echo.
    echo Please build the Windows executable:
    echo   make windows
    echo.
    echo Or run the build script:
    echo   build.sh
    echo.
    pause
    exit /b 1
)

REM Create data directories if they don't exist
if not exist "data" mkdir data
if not exist "tor" mkdir tor
if not exist "temp" mkdir temp

echo [Launch]
echo --------------------------------------------------------
echo.
echo Starting Khimera Portable...
echo.

REM Set environment variables for portable mode
set "KHIMERA_PORTABLE=1"
set "KHIMERA_DATA_DIR=%SCRIPT_DIR%data"
set "KHIMERA_TOR_DIR=%SCRIPT_DIR%tor"
set "KHIMERA_TEMP_DIR=%SCRIPT_DIR%temp"

REM Change to Windows directory and launch
cd /d "%SCRIPT_DIR%Windows"
start "" "khimera.exe"

REM Check if launch was successful
if errorlevel 1 (
    echo.
    echo ========================================================
    echo   ERROR: Failed to launch Khimera
    echo ========================================================
    echo.
    echo Possible issues:
    echo   - Antivirus blocking the executable
    echo   - Insufficient permissions
    echo   - USB drive write-protected
    echo   - Missing dependencies
    echo.
    echo Troubleshooting:
    echo   1. Add khimera.exe to antivirus exceptions
    echo   2. Run as Administrator (one time)
    echo   3. Check USB drive is not read-only
    echo   4. Verify Windows version compatibility
    echo.
    pause
    exit /b 1
)

REM Success
echo.
echo Khimera launched successfully!
echo.
echo The application window should open shortly...
echo You can close this window.
echo.

REM Wait a moment to ensure app started
timeout /t 3 /nobreak >nul

exit /b 0
