@echo off
setlocal

set script_dir_backslash=%~dp0
set script_dir=%script_dir_backslash:~0,-1%
set build_dir=%script_dir%\Build
set code_dir=%script_dir%
set tely_dir=%code_dir%\External\tely

REM ================================================================================================

mkdir %build_dir% 2>nul
pushd %build_dir%
mkdir %build_dir%\Data 2>nul

REM ================================================================================================

set robocopy_cmd=robocopy /MIR /NJH /NJS /NDL /NP %code_dir%\Data %build_dir%\Data
call powershell -Command "$duration = Measure-Command {%robocopy_cmd% | Out-Default}; Write-Host 'robocopy:' $duration.TotalSeconds 'seconds'"

REM ================================================================================================

REM TODO: Raylib seems to have problems shutting down fsanitize=address?
set common_compile_flags=/W4 /Z7 /MT /EHsc /nologo /I %tely_dir%
set common_link_flags=/link /incremental:no

REM raylib =========================================================================================

set raylib_dir=%tely_dir%\external\raylib
if not exist %build_dir%\rcore.obj (
    cl %common_compile_flags% ^
    /w ^
    /c ^
    /D _DEFAULT_SOURCE ^
    /D PLATFORM_DESKTOP ^
    /I %raylib_dir% ^
    /I %raylib_dir%\external\glfw\include ^
    /I %raylib_dir%\glfw\deps\mingw ^
    %raylib_dir%\rcore.c ^
    %raylib_dir%\utils.c ^
    %raylib_dir%\raudio.c ^
    %raylib_dir%\rmodels.c ^
    %raylib_dir%\rtext.c ^
    %raylib_dir%\rtextures.c ^
    %raylib_dir%\rshapes.c ^
    %raylib_dir%\rglfw.c
)

REM raylib flags =========================================================================================

set raylib_compile_flags=%common_compile_flags% ^
/Tp %tely_dir%\tely_platform_raylib_unity.h ^
/I "%raylib_dir%"

set raylib_link_flags=%common_link_flags% ^
%build_dir%\rcore.obj ^
%build_dir%\utils.obj ^
%build_dir%\raudio.obj ^
%build_dir%\rmodels.obj ^
%build_dir%\rtext.obj ^
%build_dir%\rtextures.obj ^
%build_dir%\rshapes.obj ^
%build_dir%\rglfw.obj ^
gdi32.lib opengl32.lib winmm.lib user32.lib shell32.lib

REM DLL flags ======================================================================================

set dll_compile_flags=%common_compile_flags% /LD %code_dir%\feely_pona_unity.cpp
set dll_link_flags=%common_link_flags%

REM MSVC commands ==================================================================================

set msvc_exe_name=feely_pona_msvc
set msvc_cmd=cl %raylib_compile_flags% /Fo%msvc_exe_name% /Fe%msvc_exe_name% %raylib_link_flags%
set msvc_dll_cmd=cl %dll_compile_flags% /Fotely_dll_msvc /Fetely_dll_msvc %dll_link_flags%

REM CLANG commands =================================================================================

set clang_exe_name_sdl=feely_pona_clang
set clang_cmd_sdl=clang-cl %compile_flags%     /Fo%clang_exe_name_sdl% /Fe%clang_exe_name_sdl% %link_flags%
set clang_dll_cmd=clang-cl %dll_compile_flags% /Fotely_dll_clang   /Fetely_dll_clang   %dll_link_flags%

REM MSVC build =====================================================================================

set msvc_build_platform=$platform_time = Measure-Command {%msvc_cmd% ^| Out-Default};
set msvc_build_dll=$dll_time = Measure-Command {%msvc_dll_cmd% ^| Out-Default};
set msvc_print_dll_time=Write-Host 'msvc dll:'$dll_time.TotalSeconds's'
set msvc_print_platform_and_dll_time=Write-Host 'msvc (platform+dll):'($platform_time.TotalSeconds + $dll_time.TotalSeconds)'s ('$platform_time.TotalSeconds + $dll_time.TotalSeconds')'
set msvc_check_if_exe_locked=$File = [System.IO.File]::Open('%build_dir%\%msvc_exe_name%.exe', 'Open', 'Write'); $File.Close(); $File.Dispose()

set msvc_exe_is_locked=0
if exist %build_dir%\%msvc_exe_name%.exe (
    powershell -Command "%msvc_check_if_exe_locked%" 2>nul && set msvc_exe_is_locked=0 || set msvc_exe_is_locked=1
)

echo foo> %msvc_exe_name%.lock
if %msvc_exe_is_locked% == 1 (
    powershell -Command "%msvc_build_dll% %msvc_print_dll_time%"
) else (
    powershell -Command "%msvc_build_platform% %msvc_build_dll% %msvc_print_platform_and_dll_time%"
)
del %msvc_exe_name%.lock

REM CLANG build ====================================================================================

popd
exit /B 1
