@echo off
setlocal

REM Build tentone/rubix-solver as standalone executable
REM Requires OpenCV. If OpenCV is not installed, only the solver logic
REM (cube.cpp, solver.cpp, solution.cpp) can be compiled without vision.

set SRCDIR=..\..\.external\rubix-solver
set OUTDIR=..\..\UnrealHyperTwist\Binaries\Win64\Solvers

if not exist %OUTDIR% mkdir %OUTDIR%

REM Try to find OpenCV
set OPENCV_DIR=
if exist "C:\opencv\build" set OPENCV_DIR=C:\opencv\build
if exist "C:\tools\opencv\build" set OPENCV_DIR=C:\tools\opencv\build

if "%OPENCV_DIR%"=="" (
    echo WARNING: OpenCV not found. Building solver-only executable without vision.
    echo Install OpenCV and set OPENCV_DIR to enable computer vision features.
    
    call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
    if errorlevel 1 exit /b 1
    
    cl /nologo /W4 /O2 /Fe%OUTDIR%\tentone-solver.exe ^
        %SRCDIR%\cube.cpp ^
        %SRCDIR%\solver.cpp ^
        %SRCDIR%\solution.cpp ^
        %SRCDIR%\utils.cpp ^
        %SRCDIR%\config.cpp ^
        %SRCDIR%\main.cpp
) else (
    echo Found OpenCV at %OPENCV_DIR%
    call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
    if errorlevel 1 exit /b 1
    
    cl /nologo /W4 /O2 /I%OPENCV_DIR%\include /Fe%OUTDIR%\tentone-solver.exe ^
        %SRCDIR%\cube.cpp ^
        %SRCDIR%\solver.cpp ^
        %SRCDIR%\solution.cpp ^
        %SRCDIR%\utils.cpp ^
        %SRCDIR%\config.cpp ^
        %SRCDIR%\vision.cpp ^
        %SRCDIR%\main.cpp ^
        /link /LIBPATH:%OPENCV_DIR%\x64\vc16\lib opencv_world490.lib
)

if errorlevel 1 (
    echo Build failed
    exit /b 1
)

echo tentone-solver built successfully at %OUTDIR%\tentone-solver.exe
