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

echo ----------------------------------------------------
echo:
echo                  BUILDING XIWRAPPER
echo:
echo ----------------------------------------------------

for /f "skip=3" %%L in ( version ) do ( set XIWRAPPERVER=%%L & goto out )
:out
if "%XIWRAPPERVER%"=="" set XIWRAPPERVER=master
echo xiwrapper version: %XIWRAPPERVER%

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_XIWRAPPER 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_XIWRAPPER win64 x64
if not %errorlevel% == 0 goto FAIL

echo ----------------------------------------------------
echo           BUILDING XIWRAPPER IS COMPLETED
echo ----------------------------------------------------
echo Output:
echo * %DISTDIR%\ximc\win32\xiwrapper.dll
echo * %DISTDIR%\ximc\win64\xiwrapper.dll
echo ----------------------------------------------------
goto :eof


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


:DEPS_XIWRAPPER
set DISTARCH=%DEPSDIR%\%1
set ARCH=%2
echo:
echo +++++++++++++ Building xiwrapper for %ARCH%...

rmdir /S /Q %DISTARCH%\xiwrapper
mkdir %DISTARCH%\xiwrapper

if "x%URL_XIWRAPPER%" == "x" set URL_XIWRAPPER="https://github.com/Standa-Optomechanics/libxiwrapper.git"

"%GIT%" clone %URL_XIWRAPPER% %DISTARCH%\xiwrapper
if not %errorlevel% == 0 goto FAIL
cd %DISTARCH%\xiwrapper
%GIT% checkout %XIWRAPPERVER%
if not %errorlevel% == 0 goto FAIL
"%GIT%" --no-pager show --stat %XIWRAPPERVER%
if not %errorlevel% == 0 goto FAIL

set GENERATOR=Visual Studio 17 2022
%CMAKE% -G "%GENERATOR%" -A %ARCH% -DBINDY_PATH=%DISTARCH%\bindy .
if not %errorlevel% == 0 goto FAIL
msbuild xiwrapper.sln /p:Configuration=%CONFIGURATION% /p:Platform=%ARCH% /m
if not %errorlevel% == 0 goto FAIL
cd /d "%BASEDIR%"
if not exist %DISTARCH%\xiwrapper\%CONFIGURATION%\xiwrapper.dll goto FAIL

rem Copying xiwrapper to the dist
if not exist %DISTDIR%\ximc\%1 mkdir %DISTDIR%\ximc\%1
if not %errorlevel% == 0 goto FAIL
copy %DISTARCH%\xiwrapper\%CONFIGURATION%\xiwrapper.dll %DISTDIR%\ximc\%1
if not %errorlevel% == 0 goto FAIL

if not "%CONFIGURATION%"=="Debug" goto SKIP_PDB_COPY_XIWRAPPER
copy %DISTARCH%\xiwrapper\%CONFIGURATION%\xiwrapper.pdb %DISTDIR%\ximc\%1
if not %errorlevel% == 0 goto FAIL


:SKIP_PDB_COPY_XIWRAPPER
goto :eof


:FAIL
echo FAIL
exit /B 1
