@echo off
setlocal enabledelayedexpansion
REM MinGW Setup Script for SecondVoice
REM This installs a lightweight compiler instead of Visual Studio

echo ========================================
echo SecondVoice - MinGW Setup
echo ========================================
echo.
echo This script will install:
echo   - MinGW-w64 - GCC compiler for Windows
echo   - CMake - build system
echo   - Ninja - build tool
echo   - Git if not installed
echo.
echo Total size: ~500MB vs 10GB+ for Visual Studio
echo.
pause

REM Check if running as admin
net session >nul 2>&1
if !errorlevel! neq 0 (
    echo [WARNING] Not running as administrator
    echo Some installations might fail. Recommended to run as admin.
    echo.
    pause
)

REM Install chocolatey if not present
where choco >nul 2>&1
if !errorlevel! neq 0 (
    echo [INFO] Installing Chocolatey package manager...
    echo.

    powershell -Command "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))"

    REM Check if Chocolatey is now in PATH
    set "PATH=%PATH%;C:\ProgramData\chocolatey\bin"

    where choco >nul 2>&1
    if !errorlevel! neq 0 (
        echo.
        echo ========================================
        echo [IMPORTANT] Chocolatey installed successfully!
        echo ========================================
        echo.
        echo Please CLOSE this window and reopen PowerShell as ADMIN
        echo Then run this script again: .\setup_mingw.bat
        echo.
        pause
        exit /b 0
    )

    echo [SUCCESS] Chocolatey installed and ready!
    echo.
)

echo [INFO] Chocolatey found
echo.

REM Install MinGW-w64
where gcc >nul 2>&1
if !errorlevel! neq 0 (
    echo [INFO] Installing MinGW-w64...
    choco install mingw -y

    if !errorlevel! neq 0 (
        echo [ERROR] Failed to install MinGW
        pause
        exit /b 1
    )

    echo [SUCCESS] MinGW installed!
    echo.
) else (
    echo [INFO] GCC already installed
    gcc --version
    echo.
)

REM Install CMake
where cmake >nul 2>&1
if !errorlevel! neq 0 (
    echo [INFO] Installing CMake...
    choco install cmake -y --installargs 'ADD_CMAKE_TO_PATH=System'

    if !errorlevel! neq 0 (
        echo [ERROR] Failed to install CMake
        pause
        exit /b 1
    )

    echo [SUCCESS] CMake installed!
    echo.
) else (
    echo [INFO] CMake already installed
    cmake --version
    echo.
)

REM Install Ninja
where ninja >nul 2>&1
if !errorlevel! neq 0 (
    echo [INFO] Installing Ninja...
    choco install ninja -y

    if !errorlevel! neq 0 (
        echo [ERROR] Failed to install Ninja
        pause
        exit /b 1
    )

    echo [SUCCESS] Ninja installed!
    echo.
) else (
    echo [INFO] Ninja already installed
    ninja --version
    echo.
)

REM Install Git (if not present)
where git >nul 2>&1
if !errorlevel! neq 0 (
    echo [INFO] Installing Git...
    choco install git -y

    if !errorlevel! neq 0 (
        echo [ERROR] Failed to install Git
        pause
        exit /b 1
    )

    echo [SUCCESS] Git installed!
    echo.
) else (
    echo [INFO] Git already installed
    git --version
    echo.
)

REM Setup vcpkg if not present
if not defined VCPKG_ROOT (
    if not exist "C:\vcpkg" (
        echo [INFO] Installing vcpkg...
        cd C:\
        git clone https://github.com/microsoft/vcpkg.git
        cd vcpkg
        call bootstrap-vcpkg.bat

        if !errorlevel! neq 0 (
            echo [ERROR] Failed to bootstrap vcpkg
            pause
            exit /b 1
        )

        echo [SUCCESS] vcpkg installed!
        echo.
    )

    echo [INFO] Setting VCPKG_ROOT environment variable...
    setx VCPKG_ROOT "C:\vcpkg"
    set VCPKG_ROOT=C:\vcpkg
    echo [SUCCESS] VCPKG_ROOT set to C:\vcpkg
    echo.
) else (
    echo [INFO] vcpkg already configured at: !VCPKG_ROOT!
    echo.
)

echo ========================================
echo [SUCCESS] Setup Complete!
echo ========================================
echo.
echo Installed tools:
where gcc
where cmake
where ninja
where git
echo VCPKG_ROOT=!VCPKG_ROOT!
echo.
echo ========================================
echo Next Steps:
echo ========================================
echo 1. Close and reopen this terminal to reload PATH
echo 2. Run: build_mingw.bat --release
echo 3. Your .exe will be in: build\mingw-release\SecondVoice.exe
echo.
echo Total installation size: ~500MB
echo vs 10GB+ for Visual Studio!
echo.
pause
