@echo off
if "%1"=="clean" (
    call :CLEAN
    goto :eof
)

echo ----------------------------------------------------
echo:
echo                  BUILDING MINIUPNPC
echo:
echo ----------------------------------------------------

set MINIUPNPCVER=8ddbb71
echo miniupnpc version: %MINIUPNPCVER%

echo ----------------------------------------------------
echo                  System checking...
echo ----------------------------------------------------
echo Checking Git...
where /q git
if not %errorlevel% == 0 (
    echo ERROR: git is not found! Check the installation and/or PATH.
    goto FAIL
) else (
    echo ***** Done!
    set GIT=git
)

echo Checking CMake...
where /q cmake
if not %errorlevel% == 0 (
    echo ERROR: cmake is not found! Check the installation and/or PATH.
    goto FAIL
) else (
    echo ***** Done!
    set CMAKE=cmake
)
echo --------------- System checking done ---------------

call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x86
call :DEPS_MINIUPNPC win32 Win32
if not %errorlevel% == 0 goto FAIL

call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
call :DEPS_MINIUPNPC win64 x64
if not %errorlevel% == 0 goto FAIL

echo ----------------------------------------------------
echo           BUILDING MINIUPNPC IS COMPLETED
echo ----------------------------------------------------
echo Output:
echo * %DEPSDIR%\win32\miniupnpc
echo * %DEPSDIR%\win64\miniupnpc
echo ----------------------------------------------------
goto :eof


rem The code below defines functions
:CLEAN
echo Cleaning miniupnpc...
if exist %DEPSDIR%\win32\miniupnpc rmdir /s/q %DEPSDIR%\win32\miniupnpc
if exist %DEPSDIR%\win64\miniupnpc rmdir /s/q %DEPSDIR%\win64\miniupnpc
goto :eof


:DEPS_MINIUPNPC
set DISTARCH=%DEPSDIR%\%1
set ARCH=%2
echo:
echo +++++++++++++ Building miniupnpc for %ARCH%...

rmdir /S /Q %DISTARCH%\miniupnpc-dist
rmdir /S /Q %DISTARCH%\miniupnpc
mkdir %DISTARCH%\miniupnpc

if "x%URL_MINIUPNPC%" == "x" set URL_MINIUPNPC="https://github.com/EPC-MSU/miniupnpc.git"

"%GIT%" clone --recursive %URL_MINIUPNPC% %DISTARCH%\miniupnpc-dist
if not %errorlevel% == 0 goto FAIL
cd %DISTARCH%\miniupnpc-dist
"%GIT%" checkout %MINIUPNPCVER%
if not %errorlevel% == 0 goto FAIL

set GENERATOR=Visual Studio 17 2022
%CMAKE% -G "%GENERATOR%" -A %ARCH% -DUPNPC_BUILD_TESTS=OFF -DUPNPC_BUILD_SAMPLE=OFF -DUPNPC_BUILD_SHARED=OFF -DCMAKE_INSTALL_PREFIX=%DISTARCH%\miniupnpc .
if not %errorlevel% == 0 goto FAIL
msbuild ALL_BUILD.vcxproj
if not %errorlevel% == 0 goto FAIL
msbuild INSTALL.vcxproj
if not %errorlevel% == 0 goto FAIL
cd %BASEDIR%
goto :eof


:FAIL
echo FAIL
exit /B 1
