@echo off
setlocal enabledelayedexpansion

:: ------------------------------------------------------------------------------
:: Unity Build Script (misc/build.bat)
:: Step 1: Generate build_plan.txt for all configurations
:: Step 2: Invoke build.exe to execute the plan
:: ------------------------------------------------------------------------------

set SCRIPT_DIR=%~dp0
cd /d "%SCRIPT_DIR%.."

set FBUILD=misc\fbuild.exe
set BUILD_TOOL=misc\build_system.exe
set BUILD_SRC=tools\build_system.c

set TARGET_JULIET=0
set TARGET_ROMEO=0
set TARGET_SHADER=0
set TARGET_GAME=0
set TARGET_SPECIFIED=0

set CONFIG_DEBUG=0
set CONFIG_RELEASE=0
set CONFIG_PROFILE=0
set CONFIG_SPECIFIED=0

:parse_args
if "%~1"=="" goto check_defaults
if /i "%~1"=="debug" set CONFIG_DEBUG=1& set CONFIG_SPECIFIED=1
if /i "%~1"=="release" set CONFIG_RELEASE=1& set CONFIG_SPECIFIED=1
if /i "%~1"=="profile" set CONFIG_PROFILE=1& set CONFIG_SPECIFIED=1
if /i "%~1"=="juliet" set TARGET_JULIET=1& set TARGET_SPECIFIED=1
if /i "%~1"=="game" set TARGET_GAME=1& set TARGET_SPECIFIED=1
if /i "%~1"=="romeo" set TARGET_ROMEO=1& set TARGET_SPECIFIED=1
if /i "%~1"=="shadercompiler" set TARGET_SHADER=1& set TARGET_SPECIFIED=1
shift
goto parse_args

:check_defaults
if !CONFIG_SPECIFIED! equ 0 set CONFIG_DEBUG=1& if !TARGET_SPECIFIED! equ 0 set CONFIG_RELEASE=1& set CONFIG_PROFILE=1
if !TARGET_SPECIFIED! equ 0 set TARGET_JULIET=1& set TARGET_GAME=1& set TARGET_ROMEO=1& set TARGET_SHADER=1

:start_build
if not exist "%FBUILD%" (
    echo [BUILD FAILED] Could not find %FBUILD%.
    exit /b 1
)

:: Find Clang / MSVC compiler
set COMPILER=clang-cl.exe
where %COMPILER% >nul 2>nul
if !ERRORLEVEL! neq 0 (
    set COMPILER=cl.exe
    where %COMPILER% >nul 2>nul
    if !ERRORLEVEL! neq 0 (
        echo [BUILD FAILED] Neither clang-cl.exe nor cl.exe was found in PATH.
        exit /b 1
    )
)

:: Check if build.exe needs to be compiled (missing, version changed, or source changed)
set BUILD_TOOL_VER=4
set NEED_RECOMPILE_BUILD_TOOL=0
if not exist "%BUILD_TOOL%" (
    set NEED_RECOMPILE_BUILD_TOOL=1
) else (
    set "CURR_VER="
    if exist "Intermediate\build_tool.ver" (
        for /f "usebackq delims=" %%I in ("Intermediate\build_tool.ver") do set "CURR_VER=%%I"
    )
    if not "!CURR_VER!"=="!BUILD_TOOL_VER!" set NEED_RECOMPILE_BUILD_TOOL=1
    set "SRC_TIME="
    for %%I in ("%BUILD_SRC%") do set "SRC_TIME=%%~tI"
    set "STAMP_TIME="
    if exist "Intermediate\build_tool.stamp" (
        for /f "usebackq delims=" %%I in ("Intermediate\build_tool.stamp") do set "STAMP_TIME=%%I"
    )
    if not "!SRC_TIME!"=="!STAMP_TIME!" set NEED_RECOMPILE_BUILD_TOOL=1
)

if !NEED_RECOMPILE_BUILD_TOOL! equ 1 (
    if exist "Intermediate\build_tool.stamp" del /f /q "Intermediate\build_tool.stamp"
    if exist "Intermediate\build_tool.ver" del /f /q "Intermediate\build_tool.ver"
    if exist "%BUILD_TOOL%" (
        del /f /q "%BUILD_TOOL%" >nul 2>&1
        if exist "%BUILD_TOOL%" (
            echo [BUILD FAILED] Could not overwrite %BUILD_TOOL%. It may be in use by another process.
            exit /b 1
        )
    )
    echo [BUILD] Compiling fast native build orchestrator [tools\build_system.c -^> misc\build.exe]...
    %COMPILER% /nologo /MT /O2 /W4 "%BUILD_SRC%" /Fe"%BUILD_TOOL%"
    if !ERRORLEVEL! neq 0 (
        echo [BUILD FAILED] Could not compile build orchestrator.
        exit /b 1
    )
    if not exist "%BUILD_TOOL%" (
        echo [BUILD FAILED] Compilation failed to produce %BUILD_TOOL%.
        exit /b 1
    )
    "%BUILD_TOOL%" check_alive >nul 2>&1
    if !ERRORLEVEL! neq 0 (
        echo [BUILD FAILED] %BUILD_TOOL% was built but failed to execute ^(missing DLL or AV block^).
        exit /b 1
    )
    if not exist "Intermediate" mkdir "Intermediate"
    (echo !BUILD_TOOL_VER!)>"Intermediate\build_tool.ver"
    for %%I in ("%BUILD_SRC%") do (echo %%~tI)>"Intermediate\build_tool.stamp"
)

echo [BUILD] Generating Build Plan...
set PLAN_FILE=Intermediate\build_plan.txt
if exist "!PLAN_FILE!" del /f /q "!PLAN_FILE!"

if !CONFIG_DEBUG! equ 1 call :emit_config Debug || exit /b !ERRORLEVEL!
if !CONFIG_PROFILE! equ 1 call :emit_config Profile || exit /b !ERRORLEVEL!
if !CONFIG_RELEASE! equ 1 call :emit_config Release || exit /b !ERRORLEVEL!

echo Compiler Used : %COMPILER%
"%BUILD_TOOL%" run_plan "!PLAN_FILE!"
exit /b !ERRORLEVEL!

:: ------------------------------------------------------------------------------
:: Build Configuration Plan Generator Subroutine
:: ------------------------------------------------------------------------------
:emit_config
set "CFG=%~1"

if not exist "bin\x64-!CFG!" mkdir "bin\x64-!CFG!"
if not exist "Intermediate\x64-!CFG!" mkdir "Intermediate\x64-!CFG!"

echo CONFIG !CFG!>>"!PLAN_FILE!"

set FASTBUILD_TARGETS=Juliet-Unity
if /i not "!CFG!"=="Release" set FASTBUILD_TARGETS=!FASTBUILD_TARGETS! ImGui-Unity
if !TARGET_JULIET! equ 1 set FASTBUILD_TARGETS=!FASTBUILD_TARGETS! JulietApp-Unity Game-Unity
if !TARGET_GAME! equ 1 set FASTBUILD_TARGETS=!FASTBUILD_TARGETS! Game-Unity
if !TARGET_ROMEO! equ 1 set FASTBUILD_TARGETS=!FASTBUILD_TARGETS! Romeo-Unity
if !TARGET_SHADER! equ 1 set FASTBUILD_TARGETS=!FASTBUILD_TARGETS! JulietShaderCompiler-Unity

echo FASTBUILD !FASTBUILD_TARGETS!>>"!PLAN_FILE!"

set COMMON_FLAGS=/nologo /std:c++20 /W4 /EHa- /utf-8 /DUNICODE /D_UNICODE /DWIN32_LEAN_AND_MEAN /D_CRT_SECURE_NO_WARNINGS /I"Juliet/include" /I"Juliet/src" /I"Game" /I"External/imgui" /I"External/imgui/backends" /DJULIET_WIN32 /wd5267 /wd4061 /wd4505 /wd4514 /wd4577 /wd4625 /wd4710 /wd4711 /wd4746 /wd4820 /wd5045 /wd5220 /wd5245 /wd4626 /wd5026 /wd5027 /wd4530

if /i "!CFG!"=="Debug" (
    set COMPILER_FLAGS=!COMMON_FLAGS! /MDd /Od /Zi /Gw /Gy /Zc:inline /DDEBUG /DPROFILING_ENABLED /DJULIET_ENABLE_IMGUI /DJULIET_DEBUG
    set IMGUI_UNITY_SRCS=Intermediate\External\imgui\*.cpp
) else if /i "!CFG!"=="Profile" (
    set COMPILER_FLAGS=!COMMON_FLAGS! /MD /O2 /Zi /Gw /Gy /Zc:inline /DRELEASE /DPROFILING_ENABLED /DJULIET_ENABLE_IMGUI
    set IMGUI_UNITY_SRCS=Intermediate\External\imgui\*.cpp
) else (
    set COMPILER_FLAGS=!COMMON_FLAGS! /MD /O2 /Gw /Gy /Zc:inline /DRELEASE
    set IMGUI_UNITY_SRCS=
)

set COMMON_LIBS=kernel32.lib user32.lib gdi32.lib dxguid.lib Ws2_32.lib dxgi.lib imm32.lib dwmapi.lib d3dcompiler.lib shell32.lib winhttp.lib delayimp.lib

if defined IMGUI_UNITY_SRCS (
    echo STEP Compiling ImGui Shared Objs [!CFG!]>>"!PLAN_FILE!"
    echo OUTPUT Intermediate\x64-!CFG!\ImGui_Unity1.obj>>"!PLAN_FILE!"
    echo DEPENDS External\imgui>>"!PLAN_FILE!"
    echo COMMAND %COMPILER% %COMPILER_FLAGS% /c !IMGUI_UNITY_SRCS! /Fo"Intermediate\x64-!CFG!\\">>"!PLAN_FILE!"
)

set IMGUI_OBJS=
if defined IMGUI_UNITY_SRCS set IMGUI_OBJS=Intermediate\x64-!CFG!\ImGui_Unity*.obj

set BUILD_GAME_DLL=0
if !TARGET_JULIET! equ 1 set BUILD_GAME_DLL=1
if !TARGET_GAME! equ 1 set BUILD_GAME_DLL=1

:: --- Juliet.dll ---
echo STEP Building Juliet.dll [!CFG!]>>"!PLAN_FILE!"
echo OUTPUT bin\x64-!CFG!\Juliet.dll>>"!PLAN_FILE!"
echo DEPENDS Juliet^|External\imgui>>"!PLAN_FILE!"
echo COMMAND %COMPILER% %COMPILER_FLAGS% /DJULIET_EXPORT /LD /Fe"bin\x64-!CFG!\Juliet.dll" Intermediate\Juliet\*.cpp %IMGUI_OBJS% /link %COMMON_LIBS% /INCREMENTAL /ILK:Intermediate\x64-!CFG!\Juliet.ilk /PDB:Intermediate\x64-!CFG!\Juliet.pdb>>"!PLAN_FILE!"

:: --- Game.dll ---
if !BUILD_GAME_DLL! equ 1 (
    echo STEP Building Game.dll [!CFG!]>>"!PLAN_FILE!"
    echo OUTPUT bin\x64-!CFG!\Game.dll>>"!PLAN_FILE!"
    echo DEPENDS Game^|Juliet^|External\imgui>>"!PLAN_FILE!"
    echo COMMAND %COMPILER% %COMPILER_FLAGS% /DJULIET_EXPORT /LD /Fe"bin\x64-!CFG!\Game.dll" Intermediate\Game\*.cpp %IMGUI_OBJS% /link %COMMON_LIBS% bin\x64-!CFG!\Juliet.lib /INCREMENTAL /ILK:Intermediate\x64-!CFG!\Game.ilk /PDB:Intermediate\x64-!CFG!\Game.pdb>>"!PLAN_FILE!"
)

:: --- JulietApp.exe ---
if !TARGET_JULIET! equ 1 (
    echo STEP Building JulietApp.exe [!CFG!]>>"!PLAN_FILE!"
    echo OUTPUT bin\x64-!CFG!\JulietApp.exe>>"!PLAN_FILE!"
    echo DEPENDS JulietApp^|Game^|Juliet^|External\imgui>>"!PLAN_FILE!"
    echo COMMAND %COMPILER% %COMPILER_FLAGS% /Fe"bin\x64-!CFG!\JulietApp.exe" Intermediate\JulietApp\*.cpp %IMGUI_OBJS% /link %COMMON_LIBS% bin\x64-!CFG!\Juliet.lib /SUBSYSTEM:CONSOLE /INCREMENTAL /ILK:Intermediate\x64-!CFG!\JulietApp.ilk /PDB:Intermediate\x64-!CFG!\JulietApp.pdb>>"!PLAN_FILE!"
)

:: --- Romeo.exe ---
if !TARGET_ROMEO! equ 1 (
    echo STEP Building Romeo.exe [!CFG!]>>"!PLAN_FILE!"
    echo OUTPUT bin\x64-!CFG!\Romeo.exe>>"!PLAN_FILE!"
    echo DEPENDS Romeo^|Juliet^|External\imgui>>"!PLAN_FILE!"
    echo COMMAND %COMPILER% %COMPILER_FLAGS% /Fe"bin\x64-!CFG!\Romeo.exe" Intermediate\Romeo\*.cpp %IMGUI_OBJS% /link %COMMON_LIBS% bin\x64-!CFG!\Juliet.lib /SUBSYSTEM:CONSOLE /INCREMENTAL /ILK:Intermediate\x64-!CFG!\Romeo.ilk /PDB:Intermediate\x64-!CFG!\Romeo.pdb>>"!PLAN_FILE!"
)

:: --- JulietShaderCompiler.exe ---
if !TARGET_SHADER! equ 1 (
    echo STEP Building JulietShaderCompiler.exe [!CFG!]>>"!PLAN_FILE!"
    echo OUTPUT bin\x64-!CFG!\JulietShaderCompiler.exe>>"!PLAN_FILE!"
    echo DEPENDS JulietShaderCompiler^|Juliet^|External\imgui>>"!PLAN_FILE!"
    echo COMMAND %COMPILER% %COMPILER_FLAGS% /IJulietShaderCompiler /Fe"bin\x64-!CFG!\JulietShaderCompiler.exe" Intermediate\JulietShaderCompiler\*.cpp %IMGUI_OBJS% /link %COMMON_LIBS% dxcompiler.lib /DELAYLOAD:dxcompiler.dll /SUBSYSTEM:CONSOLE /INCREMENTAL /ILK:Intermediate\x64-!CFG!\JulietShaderCompiler.ilk /PDB:Intermediate\x64-!CFG!\JulietShaderCompiler.pdb>>"!PLAN_FILE!"
)

exit /b 0
