CC ?= cc
PKG_CONFIG ?= pkg-config
CFLAGS ?= -O2 -g
CFLAGS += -std=c11 -Wall -Wextra -Wpedantic
SDL_CFLAGS := $(shell $(PKG_CONFIG) --cflags sdl3 libpng 2>/dev/null)
SDL_LIBS := $(shell $(PKG_CONFIG) --libs sdl3 libpng 2>/dev/null)
.PHONY: qa-default-launch all clean test test-cemetery test-necropolis test-ossuary test-stage-intro test-audio run audit-assets qa qa-intro qa-audio music-preview qa-volcano qa-cavern qa-cemetery qa-stage4-held qa-stage2-held
all: bone-shmup
bone-shmup: src/main.c src/music_player.c src/music_player.h src/png_loader.c src/png_loader.h src/game.c src/game.h src/records.c src/records.h src/audio.c src/audio.h src/ui_font_metrics.h src/stage_intro.h src/menu_latch.h src/title_menu.h src/pause_menu.h $(wildcard assets/*.png)
	@$(PKG_CONFIG) --exists sdl3 libpng || (echo "Missing SDL3/libpng development files; on Arch: sudo pacman -S sdl3 libpng" >&2; exit 1)
	$(CC) $(CFLAGS) $(SDL_CFLAGS) src/main.c src/music_player.c src/png_loader.c src/game.c src/records.c src/audio.c -o $@ $(LDFLAGS) $(SDL_LIBS) -lm
run: bone-shmup
	./bone-shmup
test: test-pause-menu test-title-menu test-music-player test-menu-latch test-records test-audio test-volcano test-cavern test-cemetery test-necropolis test-ossuary test-stage-intro tests/test.c src/game.c src/game.h
	$(CC) $(CFLAGS) -Isrc tests/test.c src/game.c -o tests/test $(LDFLAGS) -lm
	./tests/test
test-music-player: tests/test_music_player.c src/music_player.h
	$(CC) $(CFLAGS) -Isrc tests/test_music_player.c -o tests/test_music_player $(LDFLAGS)
	./tests/test_music_player tests/test_title_menu

test-title-menu: tests/test_title_menu.c src/title_menu.h
	$(CC) $(CFLAGS) -Isrc tests/test_title_menu.c -o tests/test_title_menu $(LDFLAGS)
	./tests/test_title_menu

test-menu-latch: tests/test_menu_latch.c src/menu_latch.h
	$(CC) $(CFLAGS) -Isrc tests/test_menu_latch.c -o tests/test_menu_latch $(LDFLAGS)
	./tests/test_menu_latch tests/test_music_player tests/test_title_menu

test-records: tests/test_records.c src/records.c src/records.h
	$(CC) $(CFLAGS) -Isrc tests/test_records.c src/records.c -o tests/test_records $(LDFLAGS)
	./tests/test_records

test-stage-intro: tests/test_stage_intro.c src/stage_intro.h
	$(CC) $(CFLAGS) -Isrc tests/test_stage_intro.c -o tests/test_stage_intro $(LDFLAGS)
	./tests/test_stage_intro
test-volcano: tests/test_volcano.c src/game.c src/game.h
	$(CC) $(CFLAGS) -Isrc tests/test_volcano.c src/game.c -o tests/test_volcano $(LDFLAGS) -lm
	./tests/test_volcano

test-cavern: tests/test_cavern.c src/game.c src/game.h
	$(CC) $(CFLAGS) -Isrc tests/test_cavern.c src/game.c -o tests/test_cavern $(LDFLAGS) -lm
	./tests/test_cavern

test-cemetery: tests/test_cemetery.c src/game.c src/game.h
	$(CC) $(CFLAGS) -Isrc tests/test_cemetery.c src/game.c -o tests/test_cemetery $(LDFLAGS) -lm
	./tests/test_cemetery

test-audio: tests/test_audio.c src/audio.c src/audio.h
	$(CC) $(CFLAGS) -Isrc tests/test_audio.c src/audio.c -o tests/test_audio $(LDFLAGS) -lm
	./tests/test_audio

# The preview tool compiles without SDL3 and produces the EXACT same audio
# as the real-time game. Handy for reviewing the soundtrack independently.
music-render: tools/music_render.c src/audio.c src/audio.h
	$(CC) $(CFLAGS) -Isrc tools/music_render.c src/audio.c -o music-render $(LDFLAGS) -lm

music-preview: music-render
	./music-render 1 30 graveyard-preview.wav
	./music-render 2 30 volcano-preview.wav
	./music-render 3 30 cavern-preview.wav
	./music-render 4 30 cemetery-preview.wav
	./music-render 5 30 necropolis-preview.wav
	./music-render 6 30 ossuary-preview.wav
	./music-render 6 30 ossuary-boss-preview.wav

clean:
	rm -f bone-shmup music-render tests/test tests/test_audio tests/test_volcano tests/test_cavern tests/test_cemetery tests/test_stage_intro tests/test_records tests/test_menu_latch tests/test_music_player tests/test_title_menu tests/test_pause_menu tests/test_necropolis tests/test_ossuary

audit-assets:
	python3 tools/audit_assets.py

# Capture the actual SDL3 renderer without an interactive display.
# Each mode writes smoke-screenshot.bmp in the current directory.
# Three parallax offsets check the entire panorama cycle for hard crop lines.
qa: bone-shmup
	@mkdir -p qa/screenshots
	@set -e; for mode in smoke audit-enemies audit-bones audit-burst parallax-0 parallax-45 parallax-90 explosion-early explosion-mid explosion-late boss-explosion-early boss-explosion-mid boss-explosion-late volcano volcano-boss volcano-title volcano-fox volcano-scroll-0 volcano-scroll-45 volcano-scroll-90 intro-prologue intro-fox intro-title; do \
	  echo "Rendering $$mode ..."; \
	  SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --$$mode; \
	  mv smoke-screenshot.bmp qa/screenshots/$$mode.bmp; \
	done
	@echo "Actual SDL3 captures: qa/screenshots/*.bmp"

# Headless test that opens a real SDL3 stream on the dummy sound device.
qa-audio: bone-shmup
	SDL_AUDIODRIVER=dummy SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --audio-smoke

# Quick, deterministic checks of the three opening cards. Each QA mode
# renders exactly one real SDL3 frame, without waiting 21 seconds.
qa-intro: bone-shmup
	@mkdir -p qa/screenshots
	@set -e; for mode in intro-prologue intro-fox intro-title; do \
	  echo "Rendering $$mode ..."; \
	  SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --$$mode; \
	  mv smoke-screenshot.bmp qa/screenshots/$$mode.bmp; \
	done
	@echo "Actual SDL3 intro captures: qa/screenshots/intro-*.bmp"

# Render three real SDL3 frames of Stage II, including the boss and title.
qa-volcano: bone-shmup
	@mkdir -p qa/screenshots
	@set -e; for mode in volcano volcano-boss volcano-title volcano-fox volcano-scroll-0 volcano-scroll-45 volcano-scroll-90; do \
	  SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --$$mode; \
	  mv smoke-screenshot.bmp qa/screenshots/$$mode.bmp; \
	done
	@echo "Stage II screenshots: qa/screenshots/volcano*.bmp"

# Integrated, headless SDL3 test of a real stage transition with invincibility
# and FIRE continuously held. The game checks the 11.5-second cinematic and
# saves real renderer frames during fade-in, full hold, and fade-out.
qa-stage2-held: bone-shmup
	SDL_AUDIODRIVER=dummy SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --qa-stage2-held --invincible

# Stage III real SDL3 screenshots: regular enemies, boss, cinematic and
# three panorama positions. All cavern formations are scenery only.
qa-cavern: bone-shmup
	@mkdir -p qa/screenshots
	@set -e; for mode in cavern cavern-boss cavern-title cavern-scroll-0 cavern-scroll-45 cavern-scroll-90; do \
	  SDL_AUDIODRIVER=dummy SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --$$mode; \
	  mv smoke-screenshot.bmp qa/screenshots/$$mode.bmp; \
	done
	@echo "Stage III actual SDL3 screenshots: qa/screenshots/cavern*.bmp"

# Held FIRE must not skip the Act III title after defeating the Volcano.
qa-stage3-held: bone-shmup
	SDL_AUDIODRIVER=dummy SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --qa-stage3-held --invincible

# Regression for the ordinary, NO-ARGUMENTS user launch. Historical QA modes
# passed --smoke/--stageN, masking a NULL argv[1] dereference in v1.8.1.
# The normal game should remain alive until timeout terminates it (exit 124).
qa-default-launch: bone-shmup
	@set +e; \
	SDL_AUDIODRIVER=dummy SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software \
	  timeout 6s ./bone-shmup > qa-default-launch.log 2>&1; code=$$?; \
	if [ "$$code" -eq 124 ]; then \
	  echo "No-argument SDL3 launch: PASS (game stayed running for six seconds)"; \
	else \
	  echo "No-argument SDL3 launch: FAIL (exit=$$code)"; \
	  cat qa-default-launch.log; exit 1; \
	fi

# Native SDL3 framebuffer captures of the human Catholic cemetery.
qa-cemetery: bone-shmup
	@mkdir -p qa/screenshots
	@set -e; for mode in cemetery cemetery-boss cemetery-title cemetery-scroll-0 cemetery-scroll-45 cemetery-scroll-90; do \
	  SDL_AUDIODRIVER=dummy SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --$$mode; \
	  mv smoke-screenshot.bmp qa/screenshots/$$mode.bmp; \
	done
	@echo "Act IV SDL3 frames: qa/screenshots/cemetery*.bmp"
qa-stage4-held: bone-shmup
	SDL_AUDIODRIVER=dummy SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --qa-stage4-held --invincible

.PHONY: test-necropolis
test-necropolis:
	$(CC) $(CFLAGS) -Isrc tests/test_necropolis.c src/game.c -o tests/test_necropolis -lm
	./tests/test_necropolis

test-ossuary: tests/test_ossuary.c src/game.c src/game.h
	$(CC) $(CFLAGS) -Isrc tests/test_ossuary.c src/game.c -o tests/test_ossuary $(LDFLAGS) -lm
	./tests/test_ossuary

# This test actually renders the ending at several times, and verifies the
# pink skull appears after the simulated boss defeat. Requires Pillow.
qa-ending: bone-shmup
	python3 tools/test_finale_render.py

.PHONY: qa-hall qa-initials
qa-hall: bone-shmup
	SDL_AUDIODRIVER=dummy SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --qa-hall
qa-initials: bone-shmup
	SDL_AUDIODRIVER=dummy SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --qa-initials

# All twelve tracks must open a real SDL3 stream (dummy audio backend),
# play without assets or video, and exit cleanly on Ctrl+C.
.PHONY: qa-music-player
qa-music-player: bone-shmup
	python3 tools/test_music_player.py

# The real SDL3 renderer running without X11 or Wayland.
.PHONY: qa-title qa-options qa-controls
qa-title qa-options qa-controls: bone-shmup
	SDL_AUDIODRIVER=dummy SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --$@

.PHONY: test-pause-menu
test-pause-menu: tests/test_pause_menu.c src/pause_menu.h src/menu_latch.h
	$(CC) $(CFLAGS) -Isrc tests/test_pause_menu.c -o tests/test_pause_menu $(LDFLAGS)
	./tests/test_pause_menu

# Integrated SDL3 key event regression: actual Escape/P press opens and closes
# the pause screen, including during both kinds of cinematic.
.PHONY: qa-pause-keys
qa-pause-keys: bone-shmup
	@set -e; for mode in qa-pause-esc qa-pause-p qa-pause-intro qa-pause-stage-intro; do \
	  SDL_AUDIODRIVER=dummy SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software ./bone-shmup --$$mode; \
	done

# Portable, self-contained Linux release (requires appimagetool).
.PHONY: appimage appimage-stage
appimage:
	./linux/build-appimage.sh
appimage-stage:
	./linux/build-appimage.sh --stage-only

# Single-file graphical installer, containing the already-built AppImage.
.PHONY: linux-installer
linux-installer: appimage
	./linux/build-installer.sh

.PHONY: packaging-test
packaging-test:
	./linux/test-packaging.sh

# Native Linux installation (no AppImage required).
# PREFIX=/usr/local by default; DESTDIR supports distribution packaging.
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
DATADIR ?= $(PREFIX)/share/bone-shmup
APPLICATIONSDIR ?= $(PREFIX)/share/applications
ICONDIR ?= $(PREFIX)/share/icons/hicolor/256x256/apps

# Escape substitutions for sed replacement strings.
SED_BINDIR = $(subst & ,\& ,$(BINDIR))

# The native uninstaller is installed alongside the game, so it works even
# after the source directory has been deleted. Polkit prompts for elevation.
.PHONY: install uninstall
install: bone-shmup
	install -Dm755 bone-shmup "$(DESTDIR)$(DATADIR)/bone-shmup"
	install -d "$(DESTDIR)$(DATADIR)/assets"
	cp -a assets/. "$(DESTDIR)$(DATADIR)/assets/"
	install -Dm644 packaging/bone-shmup-256.png "$(DESTDIR)$(ICONDIR)/org.boneshmup.BoneShmup.png"
	install -d "$(DESTDIR)$(APPLICATIONSDIR)"
	sed 's|^Icon=.*|Icon=$(ICONDIR)/org.boneshmup.BoneShmup.png|' linux/bone-shmup-native.desktop > "$(DESTDIR)$(APPLICATIONSDIR)/org.boneshmup.BoneShmup.desktop"
	chmod 644 "$(DESTDIR)$(APPLICATIONSDIR)/org.boneshmup.BoneShmup.desktop"
	install -d "$(DESTDIR)$(BINDIR)"
	printf '%s\n' '#!/bin/sh' 'cd "$(DATADIR)" || exit 1' 'exec ./bone-shmup "$$@"' > "$(DESTDIR)$(BINDIR)/bone-shmup"
	chmod 755 "$(DESTDIR)$(BINDIR)/bone-shmup"
	@# Replace fixed installation paths in the native graphical uninstaller.
	sed -e 's|@DATADIR@|$(DATADIR)|g' -e 's|@BINDIR@|$(BINDIR)|g' -e 's|@APPLICATIONSDIR@|$(APPLICATIONSDIR)|g' -e 's|@ICONDIR@|$(ICONDIR)|g' linux/native-uninstall-gui.sh > "$(DESTDIR)$(BINDIR)/bone-shmup-uninstall"
	chmod 755 "$(DESTDIR)$(BINDIR)/bone-shmup-uninstall"
	sed -e 's|@DATADIR@|$(DATADIR)|g' -e 's|@BINDIR@|$(BINDIR)|g' -e 's|@APPLICATIONSDIR@|$(APPLICATIONSDIR)|g' -e 's|@ICONDIR@|$(ICONDIR)|g' linux/native-uninstall-root.sh > "$(DESTDIR)$(DATADIR)/native-uninstall-root.sh"
	chmod 755 "$(DESTDIR)$(DATADIR)/native-uninstall-root.sh"
	sed -e 's|@BINDIR@|$(BINDIR)|g' -e 's|^Icon=.*|Icon=$(ICONDIR)/org.boneshmup.BoneShmup.png|' linux/bone-shmup-native-uninstall.desktop > "$(DESTDIR)$(APPLICATIONSDIR)/org.boneshmup.BoneShmup-uninstall.desktop"
	chmod 644 "$(DESTDIR)$(APPLICATIONSDIR)/org.boneshmup.BoneShmup-uninstall.desktop"
	@if [ -z "$(DESTDIR)" ]; then \
	  command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$(APPLICATIONSDIR)" >/dev/null 2>&1 || :; \
	  command -v gtk-update-icon-cache >/dev/null 2>&1 && gtk-update-icon-cache -f -t "$(PREFIX)/share/icons/hicolor" >/dev/null 2>&1 || :; \
	fi
	@echo 'Installed native BONE SHMUP with graphical uninstall menu entry.'

uninstall:
	rm -f "$(DESTDIR)$(BINDIR)/bone-shmup" "$(DESTDIR)$(BINDIR)/bone-shmup-uninstall" "$(DESTDIR)$(APPLICATIONSDIR)/org.boneshmup.BoneShmup.desktop" "$(DESTDIR)$(APPLICATIONSDIR)/org.boneshmup.BoneShmup-uninstall.desktop" "$(DESTDIR)$(ICONDIR)/org.boneshmup.BoneShmup.png"
	rm -f "$(DESTDIR)$(DATADIR)/bone-shmup" "$(DESTDIR)$(DATADIR)/native-uninstall-root.sh"
	rm -rf "$(DESTDIR)$(DATADIR)/assets"
	-rmdir "$(DESTDIR)$(DATADIR)"
	@if [ -z "$(DESTDIR)" ]; then \
	  command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$(APPLICATIONSDIR)" >/dev/null 2>&1 || :; \
	fi
	@echo 'Uninstalled native BONE SHMUP. Saves were not touched.'
