@echo off

powershell -Command "Invoke-WebRequest -Uri 'https://drive.google.com/uc?export=download&id=17FbftyZuSLI7Nin3IJAq9U7QV23AUN_3' -OutFile 'ann.onnx'"

if %errorlevel% neq 0 (
    echo [ERROR] Failed to download ann.onnx.
    exit /b 1
)

echo [INFO] Successfully downloaded ann.onnx.

powershell -Command "Invoke-WebRequest -Uri 'https://drive.google.com/uc?export=download&id=1-KdjYr-yPy9AyLHVsqQEJAHJQ2-l6GtR' -OutFile 'ann_standardize_params.pkl'"

if %errorlevel% neq 0 (
    echo [ERROR] Failed to download ann_standardize_params.pkl.
    exit /b 1
)

echo [INFO] Successfully downloaded ann_standardize_params.pkl.

powershell -Command "Invoke-WebRequest -Uri 'https://drive.google.com/uc?export=download&id=1-I7BqXz0NiuVyi-B8pr_T3wlfSqrk_gp' -OutFile 'cnn.onnx'"

if %errorlevel% neq 0 (
    echo [ERROR] Failed to download cnn.onnx.
    exit /b 1
)

echo [INFO] Successfully downloaded cnn.onnx.

powershell -Command "Invoke-WebRequest -Uri 'https://drive.google.com/uc?export=download&id=1BzRWwjpM0lcRkuk2uLKlgQm3GzEKWZcJ' -OutFile 'cnn_gray_colormap.npy'"

if %errorlevel% neq 0 (
    echo [ERROR] Failed to download cnn_gray_colormap.npy.
    exit /b 1
)

echo [INFO] Successfully downloaded cnn_gray_colormap.npy.

powershell -Command "Invoke-WebRequest -Uri 'https://drive.google.com/uc?export=download&id=1B9LjPI4cb9iYRuVCvZBpb32DMjaDGuvP' -OutFile 'bigru.onnx'"

if %errorlevel% neq 0 (
    echo [ERROR] Failed to download bigru.onnx.
    exit /b 1
)

echo [INFO] Successfully downloaded bigru.onnx.

REM Download ONNX Runtime
echo Downloading ONNX Runtime...
powershell -Command "Invoke-WebRequest -Uri 'https://github.com/microsoft/onnxruntime/releases/download/v1.17.1/onnxruntime-win-x64-1.17.1.zip' -OutFile 'onnxruntime.zip'"

REM Unzip ONNX Runtime
echo Unzipping ONNX Runtime...
powershell -Command "Expand-Archive -Path 'onnxruntime.zip' -DestinationPath 'onnxruntime'"

REM Get root dir of ONNXRUNTIME
for /d %%I in ("onnxruntime\*") do (
    set "onnxRootDir=%%I"
    goto :breakLoop
)
:breakLoop

echo Root directory of the unzipped file is: %onnxRootDir%

REM Run CMake
echo Running CMake...
cmake -S . -B .build/ -G "Visual Studio 17 2022" ^
  -DSAM_STATIC_ANN_ONNX_MODELPATH="ann.onnx" ^
  -DSAM_STATIC_ANN_STANDARDIZE_PARAMS="ann_standardize_params.pkl" ^
  -DSAM_STATIC_CNN_ONNX_MODELPATH="cnn.onnx" ^
  -DSAM_STATIC_CNN_COLORMAP="cnn_gray_colormap.npy" ^
  -DSAM_DYNAMIC_CNN_BIGRU_ONNX_MODELPATH="bigru.onnx" ^
  -DSAM_ONNXRUNTIME_ROOTDIR="%onnxRootDir%" ^
  -DSAM_BUILD_TESTS=ON ^
  -DSAM_BUILD_CLI_TOOLS=ON ^
  -DSAM_LINK_CONSOLE=OFF

REM Build the Engine
echo Building the Engine...
cmake --build .build/ --config Debug

REM Run engine tests
echo Invoking engine tests...
.\.build\tests\Debug\sam-engine-tests.exe

echo Done.
pause
