@echo off
setlocal enableextensions enabledelayedexpansion

echo ================================================================
echo   Compiling Pehredaar Qt Project using Offline Toolchain
echo ================================================================
echo.

:: 1. Locate Toolchain Directory
set "TOOLCHAIN_DIR=C:\dev_git\nflt\toolchain\offline_toolchain"
if not exist "%TOOLCHAIN_DIR%" (
    set "TOOLCHAIN_DIR=%~dp0..\..\..\toolchain\offline_toolchain"
)
if not exist "%TOOLCHAIN_DIR%" (
    set "TOOLCHAIN_DIR=%~dp0..\..\toolchain\offline_toolchain"
)

if not exist "%TOOLCHAIN_DIR%\msvc\bin\cl.exe" (
    echo [ERROR] Toolchain directory not found at: %TOOLCHAIN_DIR%
    echo Please make sure the toolchain directory exists.
    pause
    exit /b 1
)

echo Toolchain Location: %TOOLCHAIN_DIR%

:: 2. Project and Output Directories
set "PROJECT_DIR=%~dp0"
if "%PROJECT_DIR:~-1%"=="\" set "PROJECT_DIR=%PROJECT_DIR:~0,-1%"
set "BUILD_DIR=%PROJECT_DIR%\build"
set "BIN_DIR=%PROJECT_DIR%\bin"

echo Project Location:   %PROJECT_DIR%
echo Build Location:     %BUILD_DIR%
echo Bin Output Dir:     %BIN_DIR%
echo.

:: 3. Configure Toolchain Environment
set "PATH=%TOOLCHAIN_DIR%\msvc\bin;%TOOLCHAIN_DIR%\sdk\bin;%TOOLCHAIN_DIR%\cmake\bin;%TOOLCHAIN_DIR%\Qt5\bin;%PATH%"
set "INCLUDE=%TOOLCHAIN_DIR%\msvc\include;%TOOLCHAIN_DIR%\sdk\include\ucrt;%TOOLCHAIN_DIR%\sdk\include\um;%TOOLCHAIN_DIR%\sdk\include\shared"
set "LIB=%TOOLCHAIN_DIR%\msvc\lib;%TOOLCHAIN_DIR%\sdk\lib\ucrt;%TOOLCHAIN_DIR%\sdk\lib\um"

:: 4. Clean/recreate build and bin directories
if exist "%BUILD_DIR%" rmdir /s /q "%BUILD_DIR%"
if exist "%BIN_DIR%" rmdir /s /q "%BIN_DIR%"

mkdir "%BUILD_DIR%"
mkdir "%BIN_DIR%"

:: 5. Run CMake configuration
echo [1/3] Running CMake configuration...
cd /d "%BUILD_DIR%"
"%TOOLCHAIN_DIR%\cmake\bin\cmake.exe" -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="%TOOLCHAIN_DIR%\Qt5" "%PROJECT_DIR%"
if %ERRORLEVEL% neq 0 (
    echo [ERROR] CMake configuration failed!
    pause
    exit /b %ERRORLEVEL%
)

:: 6. Build project with NMake
echo.
echo [2/3] Compiling project with NMake...
nmake
if %ERRORLEVEL% neq 0 (
    echo [ERROR] NMake compilation failed!
    pause
    exit /b %ERRORLEVEL%
)

:: 7. Copy executable and deploy Qt dependencies to bin directory
echo.
echo [3/3] Deploying build output and Qt runtime dependencies to bin...

if exist "%BUILD_DIR%\qt_pehredaar.exe" (
    copy /y "%BUILD_DIR%\qt_pehredaar.exe" "%BIN_DIR%\"
) else (
    echo [ERROR] Compiled executable qt_pehredaar.exe was not found in %BUILD_DIR%!
    pause
    exit /b 1
)

:: Run windeployqt to copy required Qt DLLs & plugins to bin directory
"%TOOLCHAIN_DIR%\Qt5\bin\windeployqt.exe" --release --compiler-runtime "%BIN_DIR%\qt_pehredaar.exe"
if %ERRORLEVEL% neq 0 (
    echo [WARNING] windeployqt finished with warnings.
)

:: Copy project assets if present
if exist "%PROJECT_DIR%\webrtc.html" copy /y "%PROJECT_DIR%\webrtc.html" "%BIN_DIR%\"
if exist "%PROJECT_DIR%\placeholder*.jpg" copy /y "%PROJECT_DIR%\placeholder*.jpg" "%BIN_DIR%\"
if exist "%PROJECT_DIR%\asset" xcopy /y /s /i "%PROJECT_DIR%\asset" "%BIN_DIR%\asset"

echo.
echo ================================================================
echo   SUCCESS! Qt Pehredaar binary output compiled and deployed to:
echo   %BIN_DIR%\qt_pehredaar.exe
echo ================================================================
echo.

endlocal
