.DEFAULT_GOAL := all
ZIRAN ?= ./scripts/ziran.sh
CC ?= cc
LOCK_FLAGS := $(if $(wildcard ziran.local.toml),,--locked)
ZIRAN_ROOT = $(if $(TOOLCHAIN_SOURCE),$(TOOLCHAIN_SOURCE),$(shell $(ZIRAN) pkg path ziran $(LOCK_FLAGS)))
KRYON_DIR = $(shell $(ZIRAN) pkg path kryon $(LOCK_FLAGS))
# The shared Apps controller is built from the locked Apps package. Arcade
# trusts only the reviewed digest committed in apps-controller.sha256.
ENTITY_DIR = $(shell $(ZIRAN) pkg path entity $(LOCK_FLAGS))
# Optional manifest of already exported game artifacts (never compiler inputs).
GAME_ARTIFACT_MANIFEST ?=
APPS_BUNDLE := build/apps-ui/apps.zx
APPS_SHA256 := $(shell cat apps-controller.sha256)
INBE_SOURCE ?= $(shell $(ZIRAN) pkg path inbe $(LOCK_FLAGS))
RUNTIME_SOURCES = $(wildcard $(ZIRAN_ROOT)/cmd/zir/*.[ch] $(ZIRAN_ROOT)/include/*.h $(ZIRAN_ROOT)/cmd/compiler*.zi)
PROJECT := --project $(LOCK_FLAGS)
BIN_DIR ?= $(HOME)/.local/bin
LIB_DIR ?= $(HOME)/.local/lib/arcade
DATA_DIR ?= $(if $(XDG_DATA_HOME),$(XDG_DATA_HOME),$(HOME)/.local/share)

.PHONY: all build/arcade build/shovey standalone-ui-test collection-ui-test scroll-ui-test navigation-ui-test games collection collection-test run test chess-engine-test chess-app-test ui-test inbe-ui-test compatibility-test install
all: collection build/entity.zx build/arcade build/shovey collection-check

games: build/zx-metadata
	python3 scripts/check-games.py --ziran '$(ZIRAN)' $(LOCK_FLAGS)
	python3 scripts/build-games.py --ziran '$(ZIRAN)' $(LOCK_FLAGS) $(if $(GAME_ARTIFACT_MANIFEST),--artifact-manifest '$(GAME_ARTIFACT_MANIFEST)')

$(APPS_BUNDLE): ziran.lock apps-controller.sha256 scripts/build-apps.py Makefile
	$(ZIRAN) version > /dev/null
	env -u DISPLAY -u WAYLAND_DISPLAY \
		PATH="$(if $(wildcard $(ZIRAN_ROOT)/build/bin/ziran),$(ZIRAN_ROOT)/build/bin,$(CURDIR)/build/toolchain/bin):$$PATH" \
		python3 scripts/build-apps.py --output '$(abspath $(dir $@))'

collection: games $(APPS_BUNDLE)
	python3 scripts/build-collection.py --ziran '$(ZIRAN)' $(LOCK_FLAGS) \
		--apps-bundle '$(APPS_BUNDLE)' --apps-sha256 '$(APPS_SHA256)'

# Reads a game export's entry and assets for build-games.py; nothing runs.
build/zx-metadata: scripts/zx_metadata.zi ziran.lock build/ziran/libziran.a
	rm -rf build/zx-metadata-c
	$(ZIRAN) build $(PROJECT) --target=c --root scripts --entry zx_metadata:main \
		-o build/zx-metadata-c scripts/zx_metadata.zi
	$(CC) -std=c11 -O1 -Wno-main -I'$(ZIRAN_ROOT)/include' -Ibuild/zx-metadata-c \
		build/zx-metadata-c/*.c build/ziran/libziran.a -lm -lpthread -o $@

.PHONY: game-artifacts-test
game-artifacts-test: build/zx-metadata
	python3 tests/game_artifacts_test.py
	python3 tests/wurm_export_test.py

collection-test:
	python3 tests/collection_test.py

.PHONY: collection-notifications-test
collection-notifications-test:
	python3 tests/game_host_compatibility_test.py
	python3 tests/collection_notifications_test.py

# The desktop binary opens the collection beside it and refuses one that a
# newer toolchain built (owner, 2026-11-15: Arcade closed right after opening
# from Harmony's Apps). Build and check the pair together, with private data.
.PHONY: collection-check
collection-check: collection build/arcade
	ARCADE_DATA='$(CURDIR)/build/collection-check' ./build/arcade --check

src/catalog.zi: games

build/entity.zx: ziran.lock $(wildcard $(ENTITY_DIR)/src/*.zi)
	python3 scripts/build-entity.py

build/ziran/libziran.a: ziran.lock $(RUNTIME_SOURCES)
	$(MAKE) -C '$(ZIRAN_ROOT)' BUILD_DIR='$(abspath build/ziran)' '$(abspath $@)'

build/arcade: build/entity.zx $(wildcard src/*.zi) src/catalog.zi ziran.toml ziran.lock Makefile scripts/build-desktop.py build/ziran/libziran.a
	python3 scripts/build-desktop.py --output '$@'

build/shovey: build/entity.zx $(wildcard src/*.zi) src/catalog.zi ziran.toml ziran.lock Makefile scripts/build-desktop.py build/ziran/libziran.a
	python3 scripts/build-desktop.py --standalone-shovey --output '$@'

run: all
	./build/arcade

test: all
	python3 tests/catalog_versions_test.py
	python3 tests/game_artifacts_test.py
	python3 tests/wurm_export_test.py
	python3 tests/game_host_compatibility_test.py
	python3 tests/collection_notifications_test.py
	python3 tests/game_resource_packing_test.py
	python3 tests/game_translation_keys_test.py
	python3 tests/android_catalog_test.py
	python3 tests/android_jni_contract_test.py
	python3 tests/game_updates_test.py
	$(ZIRAN) fmt --check src/*.zi apps/chess/*.zi tests/chess_engine_behavior.zi tests/chess_app_behavior.zi
	python3 tests/chess_locale_test.py
	INBE_SOURCE='$(INBE_SOURCE)' python3 tests/catalog_test.py
	python3 tests/publisher_test.py
	python3 tests/apps_controller_test.py
	python3 tests/startup_art_test.py
	python3 tests/web_library_art_test.py
	./build/arcade --list
	sh tests/chess_engine_zi_test.sh '$(ZIRAN)'
	python3 tests/sound_test.py
	sh tests/collection_loading_test.sh

chess-engine-test:
	sh tests/chess_engine_zi_test.sh '$(ZIRAN)'

chess-app-test: all
	sh tests/chess_app_test.sh '$(ZIRAN)'

ui-test: collection-ui-test

inbe-ui-test:
	@test -n "$(INBE_BINARY)" || { echo 'Set INBE_BINARY to the intended compatible host'; exit 2; }
	env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS \
		-u SESSION_MANAGER -u GDK_DISPLAY YUE_DESKTOP_RECOVERY=0 SDL_AUDIODRIVER=dummy APP_NO_TRAY=1 \
		timeout 240s xvfb-run -a -n 400 -s '-screen 0 1280x1000x24' python3 tests/chess_inbe_ui_test.py '$(INBE_BINARY)'

compatibility-test:
	@test -n "$(INBE_BINARY)" || { echo 'Set INBE_BINARY to the intended compatible host'; exit 2; }
	python3 scripts/verify-compatibility.py --inbe-binary '$(INBE_BINARY)' $(if $(OLDEST_INBE_BINARY),--oldest-inbe-binary '$(OLDEST_INBE_BINARY)',)

standalone-ui-test: all
	env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS \
		-u SESSION_MANAGER -u GDK_DISPLAY YUE_DESKTOP_RECOVERY=0 SDL_AUDIODRIVER=dummy \
		timeout 240s xvfb-run -a -n 400 -s '-screen 0 1280x1000x24' python3 tests/shovey_standalone_ui_test.py --binary '$(CURDIR)/build/shovey' --inbe-source '$(INBE_SOURCE)'

collection-ui-test: all
	env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS \
		-u SESSION_MANAGER -u GDK_DISPLAY YUE_DESKTOP_RECOVERY=0 SDL_AUDIODRIVER=dummy \
		timeout 240s xvfb-run -a -n 400 -s '-screen 0 1280x1000x24' python3 tests/collection_ui_test.py --binary '$(CURDIR)/build/arcade' --inbe-source '$(INBE_SOURCE)'

scroll-ui-test: all
	env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS \
		-u SESSION_MANAGER -u GDK_DISPLAY YUE_DESKTOP_RECOVERY=0 SDL_VIDEODRIVER=x11 SDL_AUDIODRIVER=dummy \
		timeout 120s xvfb-run -a -n 400 -s '-screen 0 1280x1000x24' python3 tests/shovey_scroll_ui_test.py \
		--binary '$(CURDIR)/build/arcade' --binary '$(CURDIR)/build/shovey' --inbe-source '$(INBE_SOURCE)'

navigation-ui-test: all
	env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS \
		-u SESSION_MANAGER -u GDK_DISPLAY YUE_DESKTOP_RECOVERY=0 SDL_VIDEODRIVER=x11 SDL_AUDIODRIVER=dummy \
		timeout 150s xvfb-run -a -n 400 -s '-screen 0 1280x1000x24' python3 tests/standalone_navigation_ui_test.py \
		--binary '$(CURDIR)/build/arcade' --binary '$(CURDIR)/build/shovey' --inbe-source '$(INBE_SOURCE)'

.PHONY: install-arcade
install-arcade: collection build/entity.zx build/arcade collection-check
	mkdir -p '$(BIN_DIR)' '$(LIB_DIR)' '$(DATA_DIR)/applications'
	install -m 0755 build/arcade '$(LIB_DIR)/arcade.new'
	mv -f '$(LIB_DIR)/arcade.new' '$(LIB_DIR)/arcade'
	install -m 0644 build/entity.zx build/arcade.zx build/arcade.json '$(LIB_DIR)/'
	install -m 0644 '$(APPS_BUNDLE)' '$(LIB_DIR)/apps.zx'
	mkdir -p '$(LIB_DIR)/assets/hud'
	install -m 0644 build/assets/hud/profile.webp build/assets/hud/dew.webp '$(LIB_DIR)/assets/hud/'
	ln -sfn '$(LIB_DIR)/arcade' '$(BIN_DIR)/arcade'
	install -m 0644 packaging/xyz.waozi.arcade.desktop '$(DATA_DIR)/applications/'
	ARCADE_DATA='$(CURDIR)/build/collection-check' '$(LIB_DIR)/arcade' --check

install: all install-arcade
	mkdir -p '$(BIN_DIR)' '$(LIB_DIR)/games' '$(DATA_DIR)/applications'
	mkdir -p '$(LIB_DIR)/startup/shovey'
	install -m 0644 build/startup/shovey/*.png '$(LIB_DIR)/startup/shovey/'
	install -m 0755 build/arcade '$(LIB_DIR)/arcade.new'
	mv -f '$(LIB_DIR)/arcade.new' '$(LIB_DIR)/arcade'
	install -m 0755 build/shovey '$(LIB_DIR)/shovey.new'
	mv -f '$(LIB_DIR)/shovey.new' '$(LIB_DIR)/shovey'
	install -m 0644 build/entity.zx build/arcade.zx build/arcade.json build/shovey.zx build/shovey.json '$(LIB_DIR)/'
	install -m 0644 '$(APPS_BUNDLE)' '$(LIB_DIR)/apps.zx'
	mkdir -p '$(LIB_DIR)/assets/hud'
	install -m 0644 build/assets/hud/profile.webp build/assets/hud/dew.webp '$(LIB_DIR)/assets/hud/'
	ln -sfn '$(LIB_DIR)/arcade' '$(BIN_DIR)/arcade'
	ln -sfn '$(LIB_DIR)/shovey' '$(BIN_DIR)/shovey'
	mkdir -p '$(DATA_DIR)/icons/hicolor/256x256/apps'
	install -m 0644 build/shovey-icon.png '$(DATA_DIR)/icons/hicolor/256x256/apps/xyz.waozi.shovey.png'
	install -m 0644 packaging/xyz.waozi.arcade.desktop packaging/xyz.waozi.shovey.desktop '$(DATA_DIR)/applications/'

# Arcade's APK is a thin catalog with on-demand games and art. Each builds an unsigned
# release APK in build/android/; only -gplay carries a Google library.
# scripts/android-release.py signs and checks them.
.PHONY: android android-fdroid android-gplay
android: collection
	python3 scripts/build-android.py --product arcade --release

android-fdroid: collection
	python3 scripts/build-android.py --product arcade --release --edition fdroid

android-gplay: collection
	python3 scripts/build-android.py --product arcade --release --edition gplay

# Test the Android catalog through the generic install/cache path privately.
.PHONY: android-offline-ui-test
android-offline-ui-test: build/arcade build/entity.zx
	env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS \
		-u SESSION_MANAGER -u GDK_DISPLAY YUE_DESKTOP_RECOVERY=0 SDL_AUDIODRIVER=dummy \
		timeout 240s xvfb-run -a -n 400 -s '-screen 0 1280x1000x24' python3 tests/android_offline_ui_test.py --binary '$(CURDIR)/build/arcade' --inbe-source '$(INBE_SOURCE)'

.PHONY: android-shovey android-shovey-release android-shovey-bundle
android-shovey: collection
	python3 scripts/build-android.py

android-shovey-release: collection
	python3 scripts/build-android.py --release

android-shovey-bundle: collection
	python3 scripts/build-android.py --bundle

.PHONY: shovey-linux-download shovey-windows-download
shovey-linux-download: collection build/entity.zx build/shovey
	python3 scripts/package-shovey.py linux

# Cross-builds with MinGW (x86_64-w64-mingw32-gcc, -windres and CMake).
shovey-windows-download: collection build/entity.zx
	python3 scripts/build-windows.py
	python3 scripts/package-shovey.py windows
