@echo off
setlocal enabledelayedexpansion

title gmodteachgma build program

echo ==============================================================================
echo                      GMoD-TeachGMAD Build Script.!
echo ==============================================================================

set BUILD_TYPE=Release
set ARCH=x64
set ACTION=build
set RUN_AFTER=0

:: args parsings
:parse_args
if "%~1"=="" goto after_args
if /I "%~1"=="debug" (
    set BUILD_TYPE=Debug
) else if /I "%~1"=="release" (
    set BUILD_TYPE=Release
) else if /I "%~1"=="clean" (
    set ACTION=clean
) else if /I "%~1"=="run" (
    set RUN_AFTER=1
) else if /I "%~1"=="x86" (
    set ARCH=Win32
) else if /I "%~1"=="x64" (
    set ARCH=x64
)
shift
goto parse_args
:after_args

if "%ACTION%"=="clean" (
    echo [INFO] Cleaning build directory...
    if exist "build" rmdir /s /q "build"
    if exist "bin" rmdir /s /q "bin"
    echo [SUCCESS] Clean completed.
    goto :eof
)

echo [INFO] Configuration : %BUILD_TYPE%
echo [INFO] Architecture  : %ARCH%
echo.

:: checeo cmake
where cmake >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo [ERROR] CMake was not found in PATH. Please install CMake or add it to PATH.
    exit /b 1
)

:: mkdir build directory
if not exist "build" mkdir "build"

:: confg set
echo [INFO] Generating CMake project files...
cmake -B "build" -A %ARCH% -DCMAKE_BUILD_TYPE=%BUILD_TYPE%
if %ERRORLEVEL% neq 0 (
    echo [ERROR] CMake configuration failed.
    exit /b %ERRORLEVEL%
)

:: build
echo.
echo [INFO] Building GMoD-TeachGMAD (%BUILD_TYPE%)...
cmake --build "build" --config %BUILD_TYPE% --parallel
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Build compilation failed.
    exit /b %ERRORLEVEL%
)

:: final and copy
echo.
echo ==============================================================================
echo [SUCCESS] Build succeeded: build/%BUILD_TYPE%/GMoD-TeachGMAD.exe
echo ==============================================================================

if "%RUN_AFTER%"=="1" (
    echo [INFO] Launching GMoD-TeachGMAD.exe...
    start "" "build\%BUILD_TYPE%\GMoD-TeachGMAD.exe"
)

endlocal
