@echo off
chcp 65001 >nul
setlocal EnableDelayedExpansion

echo ==========================================
echo  Acoustic Core Library Test Build
echo ==========================================

set SRC_ROOT=%~dp0
set EIGEN=%SRC_ROOT%third_party\eigen-3.4.0
set ONNX_INC=%SRC_ROOT%third_party\onnxruntime\include

set INCLUDES=-I%SRC_ROOT%include -I%EIGEN%
set FLAGS=-std=c++17 -O2 -D_USE_MATH_DEFINES -Wa,-mbig-obj

if not exist build mkdir build

echo [1/3] Building test_core_lib.exe ...
g++ %FLAGS% %INCLUDES% ^
    tests\test_core_lib.cpp ^
    src\core\fft_utils.cpp ^
    src\core\audio_buffer.cpp ^
    src\core\feature_extractor.cpp ^
    src\core\distance_estimator.cpp ^
    src\core\gcc_phat_localizer.cpp ^
    src\core\threat_tracker.cpp ^
    src\io\wav_file_source.cpp ^
    -o build\test_core_lib.exe ^
    -D_stdcall=
if %ERRORLEVEL% NEQ 0 (
    echo [FAIL] test_core_lib build failed.
    exit /b 1
)

echo [2/3] Building extract_mel_cpp.exe ...
g++ %FLAGS% %INCLUDES% ^
    tests\extract_mel_cpp.cpp ^
    src\core\fft_utils.cpp ^
    src\core\feature_extractor.cpp ^
    src\io\wav_file_source.cpp ^
    -o build\extract_mel_cpp.exe ^
    -D_stdcall=
if %ERRORLEVEL% NEQ 0 (
    echo [FAIL] extract_mel_cpp build failed.
    exit /b 1
)

echo [3/3] Building test_classifier_cpp.exe ...
set ONNX_LIB=%SRC_ROOT%third_party\onnxruntime\lib\libonnxruntime.a
g++ %FLAGS% %INCLUDES% -I%ONNX_INC% ^
    tests\test_classifier_cpp.cpp ^
    src\core\fft_utils.cpp ^
    src\core\feature_extractor.cpp ^
    src\core\gunshot_classifier.cpp ^
    src\io\wav_file_source.cpp ^
    -o build\test_classifier_cpp.exe ^
    %ONNX_LIB% -D_stdcall=
if %ERRORLEVEL% NEQ 0 (
    echo [FAIL] test_classifier_cpp build failed.
    exit /b 1
)

echo.
echo [OK] All test executables built successfully.
echo.
echo Run tests:
echo   build\test_core_lib.exe
echo   build\extract_mel_cpp.exe dataset\binary\val\ambient\xxx.wav
echo   build\test_classifier_cpp.exe --model models\gunshot_classifier.onnx --wav dataset\binary\val\ambient\xxx.wav --label_map models\label_map.json
endlocal
