@echo off
REM Simple Production Build Script for Spacetris SDL3
REM This batch file builds and packages the game for distribution

setlocal EnableDelayedExpansion

echo ======================================
echo    Spacetris SDL3 Production Builder
echo ======================================
echo.

REM Check if we're in the right directory
if not exist "CMakeLists.txt" (
    echo Error: CMakeLists.txt not found. Please run this script from the project root directory.
    pause
    exit /b 1
)

REM Clean previous builds
echo Cleaning previous builds...
if exist "build-release" rmdir /s /q "build-release"
if exist "dist" rmdir /s /q "dist"

REM Create and configure build directory
echo Configuring CMake for Release build...
mkdir "build-release"
cd "build-release"
cmake .. -DCMAKE_BUILD_TYPE=Release
if !errorlevel! neq 0 (
    echo CMake configuration failed!
    pause
    exit /b 1
)

REM Build the project
echo Building Release version...
cmake --build . --config Release
if !errorlevel! neq 0 (
    echo Build failed!
    pause
    exit /b 1
)

cd ..

REM Create distribution directory
echo Creating distribution package...
mkdir "dist\SpacetrisGame"

REM Copy executable
if exist "build-release\Release\spacetris.exe" (
    copy "build-release\Release\spacetris.exe" "dist\SpacetrisGame\"
) else if exist "build-release\spacetris.exe" (
    copy "build-release\spacetris.exe" "dist\SpacetrisGame\"
) else (
    echo Error: Executable not found!
    pause
    exit /b 1
)

REM Copy assets
echo Copying game assets...
if exist "assets" xcopy "assets" "dist\SpacetrisGame\assets\" /E /I /Y
if exist "FreeSans.ttf" copy "FreeSans.ttf" "dist\SpacetrisGame\"

REM Copy SDL DLLs (if available) - SDL_image no longer needed
echo Copying dependencies...
set "PackageDir=dist\SpacetrisGame"
set "copiedDependencies=0"

call :CopyDependencyDir "build-release\vcpkg_installed\x64-windows\bin"
call :CopyDependencyDir "vcpkg_installed\x64-windows\bin"

if "%copiedDependencies%"=="0" (
    echo Warning: No dependency DLLs were copied. Ensure vcpkg release binaries exist.
)

REM Create launcher batch file
echo @echo off > "dist\SpacetrisGame\Launch-Spacetris.bat"
echo cd /d "%%~dp0" >> "dist\SpacetrisGame\Launch-Spacetris.bat"
echo spacetris.exe >> "dist\SpacetrisGame\Launch-Spacetris.bat"
echo pause >> "dist\SpacetrisGame\Launch-Spacetris.bat"

echo.
echo ======================================
echo    Build Completed Successfully!
echo ======================================
echo Package location: dist\SpacetrisGame
echo.
echo The game is ready for distribution!
echo Users can run spacetris.exe or Launch-Spacetris.bat
echo.
pause
goto :eof

:CopyDependencyDir
set "dllDir=%~1"
if not exist "%dllDir%" goto :eof
echo Scanning %dllDir% for DLLs...
for %%F in ("%dllDir%\*.dll") do (
    if exist "%%~fF" (
        if exist "%PackageDir%\%%~nxF" (
            copy /Y "%%~fF" "%PackageDir%\%%~nxF" >nul
        ) else (
            copy "%%~fF" "%PackageDir%\" >nul
        )
        echo Copied %%~nxF
        set "copiedDependencies=1"
    )
)
goto :eof
