@ECHO OFF
SETLOCAL

PUSHD "%~dp0" > NUL

IF NOT EXIST "build" ( MKDIR "build" )

PUSHD "build"

SET deps=0
SET release=0
SET assets=0

FOR %%A in (%*) DO (
	SET %%A=1
)

IF NOT EXIST "%VULKAN_SDK%" (
	ECHO [Error: Vulkan SDK required to build]
	EXIT /b 1
)

IF NOT EXIST "deps\SDL3" (
	SET deps=1
)

IF %deps% equ 1 (
	ECHO [Building dependencies]

	RMDIR /Q /S "deps"

	RMDIR /Q /S "..\thirdparty\SDL3\build" 2> NUL
	MKDIR       "..\thirdparty\SDL3\build"

	PUSHD "..\thirdparty\SDL3\build"

	cmake -DCMAKE_INSTALL_PREFIX="..\..\..\build\deps\SDL3" ..

	IF %release% equ 1 (
		msbuild -p:Configuration=Release -p:Platform=x64 SDL3.sln
		msbuild -p:Configuration=Release -p:Platform=x64 INSTALL.vcxproj
	) ELSE (
		msbuild -p:Configuration=Debug -p:Platform=x64 SDL3.sln
		msbuild -p:Configuration=Debug -p:Platform=x64 INSTALL.vcxproj
	)

	POPD

	COPY "deps\SDL3\bin\SDL3.dll" "."

	MKDIR "deps\stb"
	COPY "..\thirdparty\stb\*.h" "deps\stb" > NUL
)

IF %assets% equ 1 (
	ECHO [Copying assets]

	xcopy /Y /Q "..\assets" "assets"
)

ECHO [Building shaders]

IF NOT EXIST "assets\shaders" ( MKDIR "assets\shaders" )

glslangValidator -o "assets\shaders\basic.vert.spv" --target-env vulkan1.3 "..\code\vulkan\shaders\basic.vert"
glslangValidator -o "assets\shaders\basic.frag.spv" --target-env vulkan1.3 "..\code\vulkan\shaders\basic.frag"

ECHO [Building source]

SET COMPILER_OPTS=-nologo -W4 -I"deps\SDL3\include" -I"deps\stb" -I"%VULKAN_SDK%\Include" -I"..\code"
SET LINKER_OPTS=-LIBPATH:"deps\SDL3\lib" SDL3.lib Ole32.lib Shell32.lib

IF %release% equ 1 (
	ECHO [Release build]
	cl -O2 -WX %COMPILER_OPTS% -DLD_RELEASE=1 "%~dp0code\first.c" -link %LINKER_OPTS%
) ELSE (
	ECHO [Debug build]
	cl -Od -Zi %COMPILER_OPTS% "%~dp0code\first.c" -link %LINKER_OPTS%
)

POPD
