@echo off
chcp 65001 >nul
setlocal EnableDelayedExpansion

echo ==========================================
echo  Acoustic Core Library - Quick Build
echo ==========================================

set SRC_ROOT=%~dp0
set EIGEN=%SRC_ROOT%third_party\eigen-3.4.0
set KISSFFT=%SRC_ROOT%third_party\kiss_fft
set ONNX_INC=%SRC_ROOT%third_party\onnxruntime\include
set ONNX_LIB=%SRC_ROOT%third_party\onnxruntime\lib\onnxruntime.lib

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

if not exist build mkdir build

REM --- test_core_lib ---
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\gcc_phat_localizer.cpp ^
    src\core\distance_estimator.cpp ^
    src\core\threat_tracker.cpp ^
    -o build\test_core_lib.exe ^
    %LIBS% -lws2_32
if %ERRORLEVEL% NEQ 0 (
    echo [FAIL] test_core_lib build failed.
    exit /b 1
)

REM --- extract_mel_cpp ---
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 ^
    %LIBS%
if %ERRORLEVEL% NEQ 0 (
    echo [FAIL] extract_mel_cpp build failed.
    exit /b 1
)

REM --- test_classifier_cpp ---
echo [3/3] Building test_classifier_cpp.exe ...
g++ %FLAGS% %INCLUDES% ^
    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 ^
    %LIBS% -D_stdcall=
if %ERRORLEVEL% NEQ 0 (
    echo [FAIL] test_classifier_cpp build failed.
    exit /b 1
)

echo.
echo [OK] All core tests built successfully in build\
endlocal
