@echo off
:: set project paths
set SOURCE_DIR=%~dp0
set BUILD_DIR=%~dp0build
set BOOST_DIR=%~dp0boost
set BOOST_SRC_DIR=%~dp0boost_src
set TOOLS_DIR=%~dp0tools
set file=%SOURCE_DIR%config.ini

for /f "tokens=1,* delims==" %%i in (%file%) do (
    if "%%i" == "C_COMPILER" (
        set CONF_C_COMPILER=%%j
    )
    if "%%i" == "CXX_COMPILER" (
        set CONF_CXX_COMPILER=%%j
    )
    if "%%i" == "OPENSSL_ROOT_DIR" (
        set CONF_OPENSSL_ROOT_DIR=%%j
    )
    if "%%i" == "OPENSSL_INC" (
        set CONF_OPENSSL_INC=%%j
    )
    if "%%i" == "OPENSSL_LIB" (
        set CONF_OPENSSL_LIB=%%j
    )
    if "%%i" == "GENERATOR" (
        set GENERATOR=%%j
    )
)
set C_COMPILER=%CONF_C_COMPILER%
set CXX_COMPILER=%CONF_CXX_COMPILER%

echo "###"%CONF_C_COMPILER%
echo "###"%CONF_CXX_COMPILER%
echo "###"%CONF_OPENSSL_INC%
echo "###"%CONF_OPENSSL_LIB%

if exist "%BOOST_DIR%" goto :have_boost

mkdir "%BOOST_DIR%"

if not exist "%BOOST_SRC_DIR%" (
    echo "Please dounload the Boost version you need and unzip it to then boost_src directory"
    goto :error_exit
)

if not exist "%BOOST_SRC_DIR%\\bootstrap.bat" (
    echo "Please make sure that the bootstrap.bat file is in the boost_src directory"
    goto :error_exit
)

cd %BOOST_SRC_DIR%

call .\bootstrap.bat || (
    echo "bootstrap.bat failed"
    goto :error_exit
)
call .\b2.exe   link=static,shared ^
                architecture=x86 ^
                address-model=64 ^
                stage ^
                --stagedir="%BOOST_DIR%" ^
                --build-dir="%BOOST_SRC_DIR%\\build_windows"

cd %SOURCE_DIR%

xcopy "%BOOST_SRC_DIR%\\boost" "%BOOST_DIR%\\boost\\" /s /e /y

:have_boost

:: delete old build dir
if exist "%BUILD_DIR%" rd /s /q "%BUILD_DIR%"

:: create build dir
if not exist "%BUILD_DIR%" mkdir "%BUILD_DIR%"

:: run cmake command
cmake -G %GENERATOR% -A x64 -DCMAKE_C_COMPILER=%C_COMPILER% ^
    -DCMAKE_CXX_COMPILER=%CXX_COMPILER% ^
    -DCMAKE_BUILD_TYPE=Release ^
    -DBOOST_ROOT=%BOOST_DIR% ^
    -DOPENSSL_ROOT_DIR=%CONF_OPENSSL_ROOT_DIR% ^
    -DBoost_LIBRARY_DIRS=%BOOST_DIR%\\lib ^
    -DOPENSSL_INC_DIR=%CONF_OPENSSL_INC% ^
    -DOPENSSL_LIB_DIR=%CONF_OPENSSL_LIB% ^
    --no-warn-unused-cli -S%SOURCE_DIR% -B%BUILD_DIR%

:: build
cmake --build %BUILD_DIR% --config Release
goto :finish

:error_exit
rmdir /s /q %BOOST_DIR% 

:finish
cd %SOURCE_DIR%