@echo off
chcp 65001 >nul
setlocal EnableDelayedExpansion

echo ==========================================
echo  Acoustic Module - CMake MinGW Build
echo ==========================================

set SRC_ROOT=%~dp0
set TEMP_SRC=C:\temp\acoustic_src
set TEMP_BUILD=C:\temp\acoustic_build
set OUTPUT_DIR=%SRC_ROOT%build

REM --- Copy source to temp English path (workaround for Chinese path issues) ---
echo [INFO] Copying source to %TEMP_SRC% ...
if exist %TEMP_SRC% rmdir /s /q %TEMP_SRC%
if exist %TEMP_BUILD% rmdir /s /q %TEMP_BUILD%
mkdir %TEMP_SRC%
xcopy /s /e /i /q "%SRC_ROOT%*" "%TEMP_SRC%\" >nul

echo [INFO] Running CMake ...
mkdir %TEMP_BUILD%
cd /d %TEMP_BUILD%
cmake "%TEMP_SRC%" -G "MinGW Makefiles" ^
    -DBUILD_TESTS=ON ^
    -DBUILD_ONNX_TESTS=ON ^
    -DBUILD_ROS_WRAPPER=OFF ^
    -DCMAKE_BUILD_TYPE=Release
if %ERRORLEVEL% NEQ 0 (
    echo [FAIL] CMake configuration failed.
    exit /b 1
)

echo [INFO] Building with mingw32-make ...
mingw32-make -j4
if %ERRORLEVEL% NEQ 0 (
    echo [FAIL] Build failed.
    exit /b 1
)

REM --- Copy executables back ---
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"
for %%E in (test_core_lib.exe extract_mel_cpp.exe test_classifier_cpp.exe demo_offline.exe) do (
    if exist "%TEMP_BUILD%\%%E" (
        copy /y "%TEMP_BUILD%\%%E" "%OUTPUT_DIR%\" >nul
        echo [OK] Copied %%E
    )
)

echo.
echo [OK] CMake MinGW build complete. Executables in %OUTPUT_DIR%
endlocal
