REPO_ROOT := $(abspath ..)

WAX_COMPILER_ROOT ?= $(abspath $(REPO_ROOT)/../wax_compiler)
WAXC ?= $(WAX_COMPILER_ROOT)/cmake-build-debug/waxc
WAX_RUNTIME ?= $(WAX_COMPILER_ROOT)/cmake-build-debug/Runtime
WAX_WASM_RETARGET ?= $(WAX_COMPILER_ROOT)/cmake-build-debug/wax_wasm_retarget
# IWADs are not in the repo -- drop yours in ../wads (gitignored) or point
# DOOM_WAD_DIR somewhere else. Matches tools/wax-env.sh and the C# tests.
DOOM_WAD_DIR ?= $(REPO_ROOT)/wads
WAD ?= $(firstword $(wildcard $(DOOM_WAD_DIR)/DOOM1.WAD $(REPO_ROOT)/../doom/wads/DOOM1.WAD))
LEVEL ?=
EXPECTED_FRAME_CHECKSUM ?=
OUT ?= /tmp/doomwax-c-host-abi.bmp
CC ?= cc
EMXX ?= em++
CFLAGS ?= -O2 -std=c11 -Wall -Wextra
CXXFLAGS ?= -O2 -std=c++17 -Wall -Wextra
IMGUI_CXXFLAGS ?= -Wno-unused-variable
NODE ?= $(if $(wildcard /opt/homebrew/bin/node),/opt/homebrew/bin/node,node)
PYTHON ?= python3
PORT ?= 8000
PROBE_PORT ?= 18080
MACOS_CHROME := /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
CHROME ?= $(if $(wildcard $(MACOS_CHROME)),$(MACOS_CHROME),$(shell command -v google-chrome chromium 2>/dev/null | head -1))
EMCC ?= emcc

WAX_APP := $(REPO_ROOT)/WaxPort/Wax
BUILD_DIR := $(WAX_APP)/.wax_build
ABI_PROBE := $(REPO_ROOT)/host/abi_probe
DOOMWAX_NATIVE := $(REPO_ROOT)/host/doomwax-native
MUSIC_CORE_PROBE := $(REPO_ROOT)/host/music_core_probe
AUDIO_SFX_PROBE := $(REPO_ROOT)/host/audio_sfx_probe
COMMON_SRC := $(REPO_ROOT)/host/host_common.c
MUSIC_CORE_SRC := $(REPO_ROOT)/host/doom_music_core.c
HOST_UI_SRC := $(REPO_ROOT)/host/host_ui.cpp
NATIVE_UI_SRC := $(REPO_ROOT)/host/native_ui_sdl.cpp
HOST_AUDIO_SRC := $(REPO_ROOT)/host/host_audio.c
NATIVE_BUILD_DIR := $(REPO_ROOT)/host/.native_build
GENERATED_DIR := $(REPO_ROOT)/host/.generated
DOOM_API_CONSTANTS_H := $(GENERATED_DIR)/doom_api_constants.h
DOOM_API_CONSTANTS_JS := $(GENERATED_DIR)/doom_api_constants.mjs
DOOM_API_CONSTANTS_GEN := $(REPO_ROOT)/host/generate_doom_api_constants.mjs
NATIVE_HOST_OBJ := $(NATIVE_BUILD_DIR)/host.o
NATIVE_COMMON_OBJ := $(NATIVE_BUILD_DIR)/host_common.o
NATIVE_AUDIO_OBJ := $(NATIVE_BUILD_DIR)/host_audio.o
NATIVE_AUDIO_TEST_OBJ := $(NATIVE_BUILD_DIR)/host_audio_testing.o
NATIVE_MUSIC_OBJ := $(NATIVE_BUILD_DIR)/doom_music_core.o
NATIVE_UI_OBJ := $(NATIVE_BUILD_DIR)/native_ui_sdl.o
NATIVE_HOST_UI_OBJ := $(NATIVE_BUILD_DIR)/host_ui.o
NATIVE_IMGUI_OBJS := \
	$(NATIVE_BUILD_DIR)/imgui.o \
	$(NATIVE_BUILD_DIR)/imgui_draw.o \
	$(NATIVE_BUILD_DIR)/imgui_tables.o \
	$(NATIVE_BUILD_DIR)/imgui_widgets.o \
	$(NATIVE_BUILD_DIR)/imgui_impl_sdl2.o \
	$(NATIVE_BUILD_DIR)/imgui_impl_sdlrenderer2.o
WASM_BUILD_DIR := $(REPO_ROOT)/host/.wasm_build
WASM_APP := $(WASM_BUILD_DIR)/app.wasm
WASM_RUNTIME := $(WASM_BUILD_DIR)/runtime.wasm
WASM_BINDING := $(WASM_BUILD_DIR)/DoomWax.mjs
WASM_TYPES := $(WASM_BUILD_DIR)/DoomWax.d.mts
WASM_INSTRUMENTED_DIR := $(WASM_BUILD_DIR)/instrumented
WASM_APP_INSTRUMENTED := $(WASM_INSTRUMENTED_DIR)/app.wasm
WASM_MERGED_DIR := $(WASM_BUILD_DIR)/merged
WASM_MERGED := $(WASM_MERGED_DIR)/merged.opt.wasm
WASM_MANIFEST := $(WASM_MERGED_DIR)/App.manifest
WASM_WEB_DIR := $(WASM_BUILD_DIR)/web
WASM_WAD_DIR := $(WASM_BUILD_DIR)/wads
WASM_DEFAULT_WAD := $(WASM_WAD_DIR)/default.wad
WASM_SOUNDFONT_DIR := $(WASM_BUILD_DIR)/soundfonts
WASM_DEFAULT_SOUNDFONT := $(WASM_SOUNDFONT_DIR)/TimGM6mb.sf2
WEB_HOST_TOOLS_JS := $(WASM_BUILD_DIR)/doom_host_tools.mjs
WEB_HOST_TOOLS_WASM := $(WASM_BUILD_DIR)/doom_host_tools.wasm
WEB_MUSIC_CORE_OBJ := $(WASM_BUILD_DIR)/doom_music_core.o
MONACO_DIR := $(REPO_ROOT)/third_party/monaco-editor/min
IMGUI_CORE_SRC := \
	$(REPO_ROOT)/third_party/imgui/imgui.cpp \
	$(REPO_ROOT)/third_party/imgui/imgui_draw.cpp \
	$(REPO_ROOT)/third_party/imgui/imgui_tables.cpp \
	$(REPO_ROOT)/third_party/imgui/imgui_widgets.cpp
IMGUI_WEB_SRC := \
	$(IMGUI_CORE_SRC) \
	$(REPO_ROOT)/third_party/imgui/imgui_impl_opengl3.cpp
IMGUI_NATIVE_SRC := \
	$(IMGUI_CORE_SRC) \
	$(REPO_ROOT)/third_party/imgui/imgui_impl_sdl2.cpp \
	$(REPO_ROOT)/third_party/imgui/imgui_impl_sdlrenderer2.cpp
SOUNDFONT ?= $(firstword $(wildcard \
	$(REPO_ROOT)/soundfonts/TimGM6mb.sf2 \
	$(REPO_ROOT)/../managed-doom/soundfonts/TimGM6mb.sf2 \
	$(HOME)/Dev/managed-doom/soundfonts/TimGM6mb.sf2))
WEB_HOST_TOOLS_EXPORTS := ["_malloc","_free","_doom_music_create",\
	"_doom_music_destroy",\
	"_doom_music_reset",\
	"_doom_music_process_events",\
	"_doom_music_seek",\
	"_doom_music_render",\
	"_doom_host_ui_create",\
	"_doom_host_ui_destroy",\
	"_doom_host_ui_begin_frame",\
	"_doom_host_ui_render",\
	"_doom_host_ui_set_state",\
	"_doom_host_ui_get_frame_stats",\
	"_doom_host_ui_take_actions",\
	"_doom_host_ui_web_create",\
	"_doom_host_ui_web_destroy",\
	"_doom_host_ui_web_set_state",\
	"_doom_host_ui_web_get_frame_stats",\
	"_doom_host_ui_web_take_actions",\
	"_doom_host_ui_web_set_mouse",\
	"_doom_host_ui_web_push_mouse_button",\
	"_doom_host_ui_web_render"]
SDL_CFLAGS ?= $(shell /opt/homebrew/bin/sdl2-config --cflags 2>/dev/null || sdl2-config --cflags 2>/dev/null || pkg-config --cflags sdl2)
SDL_LIBS ?= $(shell /opt/homebrew/bin/sdl2-config --libs 2>/dev/null || sdl2-config --libs 2>/dev/null || pkg-config --libs sdl2)

.PHONY: doom-api-constants wax abi-probe run-abi-probe native run-native music-core-probe \
	audio-sfx-probe run-audio-sfx-probe run-music-core-probe wasm-app wasm-app-instrumented wasm-runtime \
	wasm-merged web-host-tools web-music run-host-tools-probe \
run-web-ui-probe run-web-command-probe run-wasm-abi-probe run-wasm-render-split-probe run-music-state-probe \
	run-wasm-shell-probe run-wasm-shell-key-probe run-wasm-save-request-probe \
	run-wasm-load-request-probe run-wasm-checkpoint-probe run-web-source-probe \
	web-host-assets web-assets run-web \
	require-runtime require-wad require-wax-compiler-root clean

require-runtime:
	@test -d "$(WAX_RUNTIME)" || \
		(echo "WAX_RUNTIME must point to the wax compiler Runtime directory" >&2; exit 2)

require-wad:
	@test -n "$(WAD)" || \
		(echo "WAD must point to a Doom IWAD, for example WAD=/path/to/DOOM1.WAD" >&2; exit 2)

require-wax-compiler-root:
	@test -x "$(WAXC)" || \
		(echo "WAXC must point to a built wax compiler; default tried $(WAXC)" >&2; exit 2)
	@test -d "$(WAX_COMPILER_ROOT)" || \
		(echo "WAX_COMPILER_ROOT must point to the wax compiler checkout" >&2; exit 2)

doom-api-constants: $(DOOM_API_CONSTANTS_H) $(DOOM_API_CONSTANTS_JS)

$(DOOM_API_CONSTANTS_H) $(DOOM_API_CONSTANTS_JS): $(REPO_ROOT)/WaxPort/Wax/Src/Api.wax $(DOOM_API_CONSTANTS_GEN)
	$(NODE) $(DOOM_API_CONSTANTS_GEN) \
		$(REPO_ROOT)/WaxPort/Wax/Src/Api.wax \
		$(DOOM_API_CONSTANTS_H) \
		$(DOOM_API_CONSTANTS_JS)

wax:
	mkdir -p $(BUILD_DIR)
	$(WAXC) $(WAX_APP) --check
	$(WAXC) $(WAX_APP) --embed --header-out-c $(BUILD_DIR)/build.wax.h

# waxc only writes the C binding when asked for it by name, so the header comes
# out of the `wax` recipe rather than a rule of its own.
$(BUILD_DIR)/build.wax.h: wax

abi-probe: wax require-runtime $(ABI_PROBE)

$(ABI_PROBE): $(REPO_ROOT)/host/abi_probe.c $(COMMON_SRC) \
	$(REPO_ROOT)/host/host_common.h $(BUILD_DIR)/build.wax.h
	$(CC) $(CFLAGS) $(REPO_ROOT)/host/abi_probe.c $(COMMON_SRC) \
		-I$(BUILD_DIR) \
		-L$(WAX_RUNTIME) -Wl,-rpath,$(WAX_RUNTIME) -lWxRuntime \
		-o $@

run-abi-probe: abi-probe require-wad
	cd $(REPO_ROOT) && $(ABI_PROBE) $(WAD) $(OUT)

native: doom-api-constants wax require-runtime $(DOOMWAX_NATIVE)

NATIVE_C_INCLUDE_FLAGS := -I$(BUILD_DIR) -I$(REPO_ROOT)/host \
	-I$(GENERATED_DIR) \
	-I$(REPO_ROOT)/third_party/TinySoundFont $(SDL_CFLAGS)
NATIVE_CXX_INCLUDE_FLAGS := $(NATIVE_C_INCLUDE_FLAGS) -I$(REPO_ROOT)/third_party/imgui
NATIVE_OBJS := $(NATIVE_HOST_OBJ) $(NATIVE_COMMON_OBJ) $(NATIVE_AUDIO_OBJ) \
	$(NATIVE_MUSIC_OBJ) $(NATIVE_UI_OBJ) $(NATIVE_HOST_UI_OBJ) $(NATIVE_IMGUI_OBJS)

$(NATIVE_BUILD_DIR):
	mkdir -p $@

$(NATIVE_HOST_OBJ): $(REPO_ROOT)/host/host.c $(REPO_ROOT)/host/native_ui_sdl.h
$(NATIVE_HOST_OBJ): $(REPO_ROOT)/host/host_common.h $(REPO_ROOT)/host/host_audio.h
$(NATIVE_HOST_OBJ): $(BUILD_DIR)/build.wax.h $(DOOM_API_CONSTANTS_H) | $(NATIVE_BUILD_DIR)
	$(CC) $(CFLAGS) $(NATIVE_C_INCLUDE_FLAGS) -c $(REPO_ROOT)/host/host.c -o $@

$(NATIVE_COMMON_OBJ): $(COMMON_SRC) $(REPO_ROOT)/host/host_common.h
$(NATIVE_COMMON_OBJ): $(BUILD_DIR)/build.wax.h | $(NATIVE_BUILD_DIR)
	$(CC) $(CFLAGS) $(NATIVE_C_INCLUDE_FLAGS) -c $(COMMON_SRC) -o $@

$(NATIVE_AUDIO_OBJ): $(HOST_AUDIO_SRC) $(REPO_ROOT)/host/host_audio.h
$(NATIVE_AUDIO_OBJ): $(REPO_ROOT)/host/doom_music_core.h | $(NATIVE_BUILD_DIR)
	$(CC) $(CFLAGS) $(NATIVE_C_INCLUDE_FLAGS) -c $(HOST_AUDIO_SRC) -o $@

$(NATIVE_AUDIO_TEST_OBJ): $(HOST_AUDIO_SRC) $(REPO_ROOT)/host/host_audio.h
$(NATIVE_AUDIO_TEST_OBJ): $(REPO_ROOT)/host/doom_music_core.h | $(NATIVE_BUILD_DIR)
	$(CC) $(CFLAGS) -DDOOMWAX_AUDIO_TESTING $(NATIVE_C_INCLUDE_FLAGS) \
		-c $(HOST_AUDIO_SRC) -o $@

$(NATIVE_MUSIC_OBJ): $(MUSIC_CORE_SRC) $(REPO_ROOT)/host/doom_music_core.h
$(NATIVE_MUSIC_OBJ): $(REPO_ROOT)/third_party/TinySoundFont/tsf.h | $(NATIVE_BUILD_DIR)
	$(CC) $(CFLAGS) $(NATIVE_C_INCLUDE_FLAGS) -c $(MUSIC_CORE_SRC) -o $@

$(NATIVE_UI_OBJ): $(NATIVE_UI_SRC) $(REPO_ROOT)/host/native_ui_sdl.h
$(NATIVE_UI_OBJ): $(REPO_ROOT)/host/host_ui.h | $(NATIVE_BUILD_DIR)
	$(CXX) $(CXXFLAGS) $(IMGUI_CXXFLAGS) $(NATIVE_CXX_INCLUDE_FLAGS) -c $(NATIVE_UI_SRC) -o $@

$(NATIVE_HOST_UI_OBJ): $(HOST_UI_SRC) $(REPO_ROOT)/host/host_ui.h | $(NATIVE_BUILD_DIR)
	$(CXX) $(CXXFLAGS) $(IMGUI_CXXFLAGS) $(NATIVE_CXX_INCLUDE_FLAGS) -c $(HOST_UI_SRC) -o $@

$(NATIVE_BUILD_DIR)/%.o: $(REPO_ROOT)/third_party/imgui/%.cpp | $(NATIVE_BUILD_DIR)
	$(CXX) $(CXXFLAGS) $(IMGUI_CXXFLAGS) $(NATIVE_CXX_INCLUDE_FLAGS) -c $< -o $@

$(DOOMWAX_NATIVE): $(NATIVE_OBJS)
	$(CXX) $(NATIVE_OBJS) \
		-L$(WAX_RUNTIME) -Wl,-rpath,$(WAX_RUNTIME) -lWxRuntime \
		$(SDL_LIBS) -lm \
		-o $@

run-native: native require-wad
	cd $(REPO_ROOT) && $(DOOMWAX_NATIVE) $(WAD)

music-core-probe: $(MUSIC_CORE_PROBE)

$(MUSIC_CORE_PROBE): $(REPO_ROOT)/host/music_core_probe.c $(MUSIC_CORE_SRC)
$(MUSIC_CORE_PROBE): $(REPO_ROOT)/host/doom_music_core.h
$(MUSIC_CORE_PROBE): $(REPO_ROOT)/third_party/TinySoundFont/tsf.h
	$(CC) $(CFLAGS) $(REPO_ROOT)/host/music_core_probe.c $(MUSIC_CORE_SRC) \
		-I$(REPO_ROOT)/third_party/TinySoundFont -lm \
		-o $@

run-music-core-probe: music-core-probe require-wad
	@test -n "$(SOUNDFONT)" || (echo "SOUNDFONT must point to a SoundFont .sf2 file" >&2; exit 2)
	$(MUSIC_CORE_PROBE) $(WAD) $(SOUNDFONT)

audio-sfx-probe: wax $(AUDIO_SFX_PROBE)

$(AUDIO_SFX_PROBE): $(REPO_ROOT)/host/audio_sfx_probe.c $(NATIVE_AUDIO_TEST_OBJ)
$(AUDIO_SFX_PROBE): $(NATIVE_MUSIC_OBJ) $(REPO_ROOT)/host/host_audio.h
	$(CC) $(CFLAGS) -DDOOMWAX_AUDIO_TESTING $(NATIVE_C_INCLUDE_FLAGS) \
		$(REPO_ROOT)/host/audio_sfx_probe.c $(NATIVE_AUDIO_TEST_OBJ) $(NATIVE_MUSIC_OBJ) \
		$(SDL_LIBS) -lm \
		-o $@

run-audio-sfx-probe: audio-sfx-probe require-wad
	env SDL_AUDIODRIVER=dummy $(AUDIO_SFX_PROBE) $(WAD)

wasm-app: require-wax-compiler-root
	mkdir -p $(WASM_BUILD_DIR)
	env WAX_WASM_RUNTIME=1 $(WAXC) $(WAX_APP) --embed \
		--emit-wasm $(WASM_APP) \
		--header-out-js $(WASM_BINDING) \
		--header-out-typescript $(WASM_TYPES)

# The same program as wasm-app, compiled for a debugger to read rather than for
# a player to run. Locals reach the debugger through the wx.debug.vars custom
# section, which only the instrumented mode emits: release and debug alike strip
# every dbg.* op before the IR optimizer runs, so a debugger pointed at either
# shows an empty locals panel and reports no error anywhere.
#
# Beside the play image rather than replacing it. Instrumentation costs several
# megabytes, and the site pays it only where it buys something -- the play page
# loads wasm-app's output, the debugger page loads this one.
#
# Its own output directory because waxc writes App.manifest next to the module it
# emits, and the second build to run overwrites it -- leaving the FIRST build's
# module beside a sidecar that describes the other one's program.
#
# No --header-out-js/--header-out-typescript either. The bindings themselves do
# not vary with output mode, but asking for them in instrumented mode also drops
# the wxdbg runtime, wxdbg-core.wasm, and the browser-compiler siblings into the
# output directory -- a debugger support tree this build has no use for, since the
# Showcase serves its own from the compiler checkout.
wasm-app-instrumented: require-wax-compiler-root
	mkdir -p $(WASM_INSTRUMENTED_DIR)
	env WAX_WASM_RUNTIME=1 $(WAXC) $(WAX_APP) --embed \
		--internal-program-mode instrumented \
		--bindings none \
		--emit-wasm $(WASM_APP_INSTRUMENTED)

wasm-runtime: require-wax-compiler-root
	mkdir -p $(WASM_BUILD_DIR)
	$(NODE) $(WAX_COMPILER_ROOT)/Runtime/WasmHost/scripts/build-runtime-reactor.mjs \
		$(WASM_RUNTIME) --retarget $(WAX_WASM_RETARGET)

wasm-merged: wasm-app wasm-runtime
	WAX_COMPILER_ROOT=$(WAX_COMPILER_ROOT) NODE=$(NODE) \
		bash $(REPO_ROOT)/host/merge_wasm_runtime.sh $(WASM_RUNTIME) $(WASM_APP) $(WASM_MERGED_DIR)

web-host-tools: $(WEB_HOST_TOOLS_JS)

web-music: web-host-tools

$(WEB_MUSIC_CORE_OBJ): $(MUSIC_CORE_SRC) $(REPO_ROOT)/host/doom_music_core.h
$(WEB_MUSIC_CORE_OBJ): $(REPO_ROOT)/third_party/TinySoundFont/tsf.h
	mkdir -p $(WASM_BUILD_DIR)
	$(EMCC) $(CFLAGS) -I$(REPO_ROOT)/third_party/TinySoundFont \
		-c $(MUSIC_CORE_SRC) -o $@

$(WEB_HOST_TOOLS_JS): $(REPO_ROOT)/host/host_tools_web.cpp $(WEB_MUSIC_CORE_OBJ) $(HOST_UI_SRC)
$(WEB_HOST_TOOLS_JS): $(IMGUI_WEB_SRC)
$(WEB_HOST_TOOLS_JS): $(REPO_ROOT)/host/doom_music_core.h $(REPO_ROOT)/host/host_ui.h
$(WEB_HOST_TOOLS_JS): $(DOOM_API_CONSTANTS_H)
$(WEB_HOST_TOOLS_JS): $(REPO_ROOT)/third_party/TinySoundFont/tsf.h
$(WEB_HOST_TOOLS_JS): $(REPO_ROOT)/third_party/imgui/imgui.h
$(WEB_HOST_TOOLS_JS): $(REPO_ROOT)/third_party/imgui/imgui_impl_opengl3.h
	mkdir -p $(WASM_BUILD_DIR)
	$(EMXX) $(CXXFLAGS) $(IMGUI_CXXFLAGS) $(REPO_ROOT)/host/host_tools_web.cpp \
		$(HOST_UI_SRC) $(IMGUI_WEB_SRC) $(WEB_MUSIC_CORE_OBJ) \
		-I$(REPO_ROOT)/host \
		-I$(GENERATED_DIR) \
		-I$(REPO_ROOT)/third_party/TinySoundFont \
		-I$(REPO_ROOT)/third_party/imgui \
		-sMODULARIZE=1 -sEXPORT_ES6=1 -sENVIRONMENT=web \
		-sMIN_WEBGL_VERSION=1 -sMAX_WEBGL_VERSION=2 \
		-sALLOW_MEMORY_GROWTH=1 \
		-sEXPORTED_FUNCTIONS='$(WEB_HOST_TOOLS_EXPORTS)' \
		-sEXPORTED_RUNTIME_METHODS='["HEAPU8","HEAP32","HEAPF32"]' \
		-o $(WEB_HOST_TOOLS_JS)

run-host-tools-probe: web-host-tools require-wad
	@test -n "$(SOUNDFONT)" || (echo "SOUNDFONT must point to a SoundFont .sf2 file" >&2; exit 2)
	$(NODE) $(REPO_ROOT)/host/host_tools_probe.mjs $(WEB_HOST_TOOLS_JS) $(WAD) $(SOUNDFONT)

web-host-assets: doom-api-constants web-host-tools
	mkdir -p $(WASM_WEB_DIR)
	rm -f $(WASM_WEB_DIR)/doom_music.mjs $(WASM_WEB_DIR)/doom_music.wasm
	cp $(REPO_ROOT)/host/web.html $(WASM_WEB_DIR)/index.html
	cp $(REPO_ROOT)/host/web_ui_probe.html $(WASM_WEB_DIR)/web_ui_probe.html
	cp $(REPO_ROOT)/host/web_command_probe.html $(WASM_WEB_DIR)/web_command_probe.html
	cp $(REPO_ROOT)/host/web_source_probe.html $(WASM_WEB_DIR)/web_source_probe.html
	cp $(REPO_ROOT)/host/web_host.mjs $(WASM_WEB_DIR)/web_host.mjs
	cp $(DOOM_API_CONSTANTS_JS) $(WASM_WEB_DIR)/doom_api_constants.mjs
	cp $(WEB_HOST_TOOLS_JS) $(WASM_WEB_DIR)/doom_host_tools.mjs
	cp $(WEB_HOST_TOOLS_WASM) $(WASM_WEB_DIR)/doom_host_tools.wasm
	mkdir -p $(WASM_WEB_DIR)/monaco
	cp -R $(MONACO_DIR)/vs $(WASM_WEB_DIR)/monaco/
	@echo "Open http://localhost:$(PORT)/web/"

run-wasm-abi-probe: doom-api-constants wasm-merged require-wad
	$(NODE) $(REPO_ROOT)/host/wasm_probe.mjs \
		$(WASM_BINDING) $(WASM_MERGED) $(WASM_MANIFEST) $(WAD)

run-wasm-render-split-probe: wasm-merged require-wad
	DOOMWAX_EXPECTED_FRAME_CHECKSUM=$(EXPECTED_FRAME_CHECKSUM) \
		$(NODE) $(REPO_ROOT)/host/render_split_probe.mjs \
		$(WASM_BINDING) $(WASM_MERGED) $(WASM_MANIFEST) $(WAD) $(LEVEL)

run-wasm-shell-probe: doom-api-constants wasm-merged require-wad
	$(NODE) $(REPO_ROOT)/host/wasm_probe.mjs \
		$(WASM_BINDING) $(WASM_MERGED) $(WASM_MANIFEST) $(WAD) --shell

run-wasm-shell-key-probe: doom-api-constants wasm-merged require-wad
	$(NODE) $(REPO_ROOT)/host/wasm_probe.mjs \
		$(WASM_BINDING) $(WASM_MERGED) $(WASM_MANIFEST) $(WAD) --shell-keys

run-wasm-save-request-probe: doom-api-constants wasm-merged require-wad
	$(NODE) $(REPO_ROOT)/host/wasm_probe.mjs \
		$(WASM_BINDING) $(WASM_MERGED) $(WASM_MANIFEST) $(WAD) --save-request

run-wasm-load-request-probe: doom-api-constants wasm-merged require-wad
	$(NODE) $(REPO_ROOT)/host/wasm_probe.mjs \
		$(WASM_BINDING) $(WASM_MERGED) $(WASM_MANIFEST) $(WAD) --load-request

run-wasm-checkpoint-probe: doom-api-constants wasm-merged require-wad
	$(NODE) $(REPO_ROOT)/host/wasm_probe.mjs \
		$(WASM_BINDING) $(WASM_MERGED) $(WASM_MANIFEST) $(WAD) --checkpoint

run-music-state-probe: wasm-merged require-wad
	$(NODE) $(REPO_ROOT)/host/music_state_probe.mjs \
		$(WASM_BINDING) $(WASM_MERGED) $(WASM_MANIFEST) $(WAD)

web-assets: doom-api-constants wasm-merged web-host-tools
	mkdir -p $(WASM_WEB_DIR)
	rm -f $(WASM_WEB_DIR)/doom_music.mjs $(WASM_WEB_DIR)/doom_music.wasm
	cp $(REPO_ROOT)/host/web.html $(WASM_WEB_DIR)/index.html
	cp $(REPO_ROOT)/host/web_ui_probe.html $(WASM_WEB_DIR)/web_ui_probe.html
	cp $(REPO_ROOT)/host/web_command_probe.html $(WASM_WEB_DIR)/web_command_probe.html
	cp $(REPO_ROOT)/host/web_source_probe.html $(WASM_WEB_DIR)/web_source_probe.html
	cp $(REPO_ROOT)/host/web_host.mjs $(WASM_WEB_DIR)/web_host.mjs
	cp $(DOOM_API_CONSTANTS_JS) $(WASM_WEB_DIR)/doom_api_constants.mjs
	cp $(WEB_HOST_TOOLS_JS) $(WASM_WEB_DIR)/doom_host_tools.mjs
	cp $(WEB_HOST_TOOLS_WASM) $(WASM_WEB_DIR)/doom_host_tools.wasm
	mkdir -p $(WASM_WEB_DIR)/monaco
	cp -R $(MONACO_DIR)/vs $(WASM_WEB_DIR)/monaco/
	@if [ -n "$(WAD)" ]; then \
		mkdir -p $(WASM_WAD_DIR); \
		cp "$(WAD)" $(WASM_DEFAULT_WAD); \
		printf '%s\n' \
			'{"defaultWadUrl":"../wads/default.wad","defaultWadName":"'"$$(basename "$(WAD)")"'"}' \
			> $(WASM_WEB_DIR)/web_config.json; \
	else \
		rm -f $(WASM_WEB_DIR)/web_config.json; \
	fi
	@if [ -n "$(SOUNDFONT)" ]; then \
		mkdir -p $(WASM_SOUNDFONT_DIR); \
		cp "$(SOUNDFONT)" $(WASM_DEFAULT_SOUNDFONT); \
		$(PYTHON) $(REPO_ROOT)/host/update_web_config.py \
			$(WASM_WEB_DIR)/web_config.json \
			defaultSoundFontUrl ../soundfonts/TimGM6mb.sf2; \
	fi
	@echo "Open http://localhost:$(PORT)/web/"

run-web-ui-probe: web-host-assets
	$(NODE) $(REPO_ROOT)/host/web_ui_probe_server.mjs \
		$(WASM_WEB_DIR) "$(CHROME)" $(PROBE_PORT)

run-web-command-probe: web-assets
	$(NODE) $(REPO_ROOT)/host/web_ui_probe_server.mjs \
		$(WASM_BUILD_DIR) "$(CHROME)" $(PROBE_PORT) web/web_command_probe.html

run-web-source-probe: web-assets
	$(NODE) $(REPO_ROOT)/host/web_ui_probe_server.mjs \
		$(WASM_BUILD_DIR) "$(CHROME)" $(PROBE_PORT) web/web_source_probe.html

run-web: web-assets
	cd $(WASM_BUILD_DIR) && $(PYTHON) -m http.server $(PORT)

clean:
	rm -f $(ABI_PROBE) $(DOOMWAX_NATIVE) $(MUSIC_CORE_PROBE) $(AUDIO_SFX_PROBE)
	rm -rf $(WASM_BUILD_DIR) $(NATIVE_BUILD_DIR)
