@echo off
setlocal enabledelayedexpansion

:: parse arguments
for %%a in (%*) do set "%%a=1"
if not "%msvc%"=="1" if not "%clang%"=="1"      set clang=1
if not "%release%"=="1"                         set debug=1
if "%debug%"=="1"                               set release=0 && echo [debug mode]
if "%release%"=="1"                             set debug=0   && echo [release mode]
if "%clang%"=="1"                               set msvc=0    && echo [clang compiler]
if "%msvc%"=="1"                                set clang=0   && echo [msvc compiler]
if not "%directx%"=="1" if not "%vulkan%"=="1"  set vulkan=1  
if "%vulkan%"=="1"                              set directx=0 && echo [vulkan backend]
if "%directx%"=="1"                             set vulkan=0  && echo [directx backend]
if "%~1"==""                                    echo [default build, building debug] && set debug=1

if "%msvc%"=="1" (
  echo [error: msvc currently unsupported]
  exit /b 1
)

if "%vulkan%"=="1" set clang_renderer_flag=-DSTG_VULKAN_RENDERER

set vulkan_include=C:\VulkanSDK\1.4.304.1\Include 

set out_name=Gears.exe

:: clang
set clang_out=-o
set clang_compiler=clang
set clang_include=-I..\src\ -I..\external\ -I%vulkan_include%
set clang_src=..\src\entry_windows.c .\vma.o
set clang_link=-luser32 -lkernel32 -lGdi32
set clang_common=-fuse-ld=lld-link %clang_include% %clang_link% %clang_renderer_flag% -Wl --target=x86_64-pc-windows-msvc -ferror-limit=50 -std=c23 -DCOMPILER_CLANG -D_USE_MATH_DEFINES -Xclang -flto-visibility-public-std -Wno-unknown-warning-option -fdiagnostics-absolute-paths -Wall -Wno-missing-braces -Wno-unused-function -Wno-writable-strings -Wno-unused-value -Wno-unused-variable -Wno-unused-local-typedef -Wno-deprecated-register -Wno-deprecated-declarations -Wno-microsoft-enum-forward-reference -Wno-unused-but-set-variable -Wno-single-bit-bitfield-constant-conversion -Wno-compare-distinct-pointer-types -Wno-initializer-overrides -Wno-incompatible-pointer-types-discards-qualifiers -Wno-for-loop-analysis -Wno-nullability-completeness
set clang_debug=%clang_compiler% %clang_src% -g -O0 -DBUILD_DEBUG=1 %clang_common% -o %out_name%
set clang_release=%clang_compiler% %clang_src% -O2 %clang_common% -o %out_name%

:: clang - vma
set vma_obj=vma.obj
set clang_vma_compiler=clang++
set clang_vma_include=-I..\external\vma\
set clang_vma_common=%clang_vma_include% -Wl --target=x86_64-pc-windows-msvc -fuse-ld=lld-link -Wno-everything -D_USE_MATH_DEFINES -c -std=c++20 -I%vulkan_include%
set clang_vma_debug=%clang_vma_compiler% ..\external\vma\vma.cpp -g -O0 %clang_vma_common%
set clang_vma_release=%clang_vma_compiler% -O2 %clang_vma_common%

:: clang - packer
set packer_src=..\src\packer.c .\vma.o
set packer_include=-I..\src -I..\external
set packer_name=Packer.exe
set packer_debug=%clang_compiler% %packer_src% -g -O0 -DBUILD_DEBUG=1 %clang_common% -o %packer_name%

if "%clang%"=="1" set compile_debug=%clang_debug%
if "%clang%"=="1" set compile_release=%clang_release%
if "%clang%"=="1" set compile_vma_debug=%clang_vma_debug%
if "%clang%"=="1" set compile_vma_release=%clang_vma_release%
if "%clang%"=="1" set compile_packer_debug=%packer_debug%

if "%debug%"=="1"   set compile=%compile_debug%
if "%debug%"=="1"   set compile_vma=%compile_vma_debug%
if "%release%"=="1" set compile=%compile_release%
if "%release%"=="1" set compile_vma=%compile_vma_release%

set linker_flags=-incremental:no -opt:ref User32.lib kernel32.lib
set vma_src=..\external\vma\vma.cpp
set game_src=..\src\entry_windows.c
set vma_obj=.\vma.obj

if not exist "..\assets.sgp" (
  echo "">..\assets.sgp
)

pushd build
if not exist ".\%packer_name%" (
  %compile_packer_debug%
)

if "%vulkan%"=="1" (
  if not exist ".\shaders" mkdir shaders
  if not exist ".\shaders\glsl" mkdir glsl

  for %%i in ("..\src\shaders\glsl\*") do (
    set base_name = %%~ni
    set ext = %%~xbase_name
    set out = "-o.\shaders\glsl\%%~ni.spv"

    if "%%i" NEQ "..\src\shaders\glsl\structures.glsl" (
      echo glslc --target-spv=spv1.6 -std=460 -fshader-stage=%ext% %i 
    )
  )
)


::.\%packer_name%

::if not exist ".\build" mkdir build
::%compile_vma%
::%compile%

popd
