@echo off
setlocal enabledelayedexpansion

:: Configuration
set INSTALL_PREFIX=C:/local/cxxLibrary
set BUILD_DEBUG_DIR=debug
set BUILD_RELEASE_DIR=release
set CMAKE_GENERATOR="MinGW Makefiles"

echo ========================================
echo cxxLibrary Build and Install Script
echo ========================================
echo.

:: Check CMake availability
where cmake >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo [ERROR] CMake not found in PATH!
    echo Please ensure CMake is installed and added to system PATH.
    pause
    exit /b 1
)

:: Check MinGW availability
where mingw32-make >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo [WARNING] mingw32-make not found in PATH!
    echo This may cause build failures if using MinGW generator.
    echo.
)

:: Build Debug configuration
echo [1/4] Configuring Debug build...
cmake -B%BUILD_DEBUG_DIR% -G %CMAKE_GENERATOR% -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=%INSTALL_PREFIX%
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Debug configuration failed!
    pause
    exit /b 1
)

echo [2/4] Building and installing Debug version...
cmake --build %BUILD_DEBUG_DIR% --target install
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Debug build/install failed!
    pause
    exit /b 1
)

:: Build Release configuration
echo [3/4] Configuring Release build...
cmake -B%BUILD_RELEASE_DIR% -G %CMAKE_GENERATOR% -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%INSTALL_PREFIX%
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Release configuration failed!
    pause
    exit /b 1
)

echo [4/4] Building and installing Release version...
cmake --build %BUILD_RELEASE_DIR% --target install
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Release build/install failed!
    pause
    exit /b 1
)

:: Verification
echo.
echo ========================================
echo Build completed successfully!
echo ========================================
echo Library installed to: %INSTALL_PREFIX%
echo Debug build directory: %BUILD_DEBUG_DIR%
echo Release build directory: %BUILD_RELEASE_DIR%
echo.
echo To use this library in other projects, add to CMake:
echo     list(APPEND CMAKE_PREFIX_PATH "%INSTALL_PREFIX%")
echo     find_package(cxxLibrary REQUIRED)
echo     target_link_libraries(your_target PRIVATE cxxLibrary::cxxLibrary)
echo ========================================
pause