@echo off
setlocal
cd /d "%~dp0"

if not exist vendor\imgui\imgui.cpp (
    echo Initializing vendor dependencies...
    git submodule update --init --recursive || exit /b 1
)

where ninja >nul 2>nul
if %errorlevel%==0 (
    set "GENERATOR=-G Ninja"
) else (
    set "GENERATOR="
)

if not exist build\CMakeCache.txt (
    cmake -S . -B build %GENERATOR% -DCMAKE_BUILD_TYPE=Release || exit /b 1
)
cmake --build build --config Release --parallel || exit /b 1

if exist build\bin\gitree.exe (
    start "" build\bin\gitree.exe %*
) else if exist build\bin\Release\gitree.exe (
    start "" build\bin\Release\gitree.exe %*
) else (
    echo Build succeeded, but gitree.exe was not found.
    exit /b 1
)

