CC = gcc

PROFILE ?= size

PKG_CFLAGS = $(shell pkg-config --cflags \
	librsvg-2.0 epoxy libpulse-simple fftw3 \
	libavformat libavcodec libavutil libswscale \
	libswresample libsodium)

PKG_LIBS = $(shell pkg-config --libs \
	librsvg-2.0 sdl3 glib-2.0 fontconfig epoxy \
	libpulse-simple fftw3 libavformat libavcodec \
	libavutil libswscale libswresample libsodium)

COMMON_CFLAGS = \
	-Wall \
	-Wextra \
	-DNDEBUG \
	-flto=auto \
	-ffunction-sections \
	-fdata-sections \
	$(PKG_CFLAGS)

COMMON_LDFLAGS = \
	-flto=auto \
	-Wl,--gc-sections \
	-Wl,--as-needed

ifeq ($(PROFILE),speed)
	OPT_CFLAGS = -O2
else ifeq ($(PROFILE),native)
	OPT_CFLAGS = -O3 -march=native -mtune=native
else
	OPT_CFLAGS = -Os
endif

CFLAGS = $(COMMON_CFLAGS) $(OPT_CFLAGS)

LDFLAGS = \
	$(COMMON_LDFLAGS) \
	$(PKG_LIBS) \
	-lSDL3_ttf \
	-lm

TARGET = hyprtile

SRC = \
	hyprtile.c \
	app_util.c \
	app_config.c \
	app_json.c \
	app_i18n.c \
	ui_theme.c \
	ui_overlay.c \
	ui_text.c \
	tile_font.c \
	ui_geometry.c \
	ui_icons.c \
	tile_shape.c \
	ui_tiles.c \
	ui_filter.c \
	ui_tile_editor.c \
	ui_file_picker.c \
	overlay_audio_recorder.c \
	overlay_calculator.c \
	overlay_music_player.c \
	overlay_video_player.c \
	overlay_hud.c \
	overlay_xmpp.c \
	overlay_notes.c \
	overlay_pwsafe.c \
	overlay_ai.c \
	overlay_bt.c \
	overlay_share_window.c \
	overlay_hyprland_config.c \
	child_lock.c \
	hyprtile_ai_client.c \
	background_manager.c \
	background_plugin_config.c \
	bg_none.c \
	audio/audio_analyzer.c \
	gl/scanline_overlay.c \
	gl/glass_overlay.c \
	gl/starfield.c \
	gl/starfield_audiophile.c \
	gl/videorender.c \
	gl/cosmic_dolphin_portal.c \
	gl/equalizer.c \
	gl/circular.c \
	gl/spectral_core.c \
	gl/oscilloscope.c \
	gl/live_scope.c \
	gl/oscilloscope2.c \
	gl/hero.c \
	gl/winamp_modern.c

UTILS = \
	hyprtile-setaudiovol \
	hyprtile-setbrightness \
	hyprtile-batterystat \
	hyprtile-bt \
	hyprtile-wallpaperd \
	hyprtile-notes/hyprtile-notes \
	hyprtile-pwsafe/hyprtile-pwsafe \
	hyprtile-ai

LOCAL_BINARIES = $(TARGET) $(UTILS)

SUBPROJECT_BINARIES = \
	hyprtile-remoteviewer-build/build/hyprtile-remoteviewer \
	hyprtile-xmppclient/hyprtile-xmppclient \
	hyprtile-videoplayer-build/hyprtile-videoplayer \
	hyprtile-tts-server-build/hyprtile-tts-server \
	hyprtile-tts-build/hyprtile-tts \
	hyprtile-stt-build/hyprtile-stt \
	hyprtile-screener-build/hyprtile-screener \
	hyprtile-audioplayer-build/hyprtile-audioplayer \
	hyprtile-shotter/hyprtile-shotter

BINARIES = $(LOCAL_BINARIES) $(SUBPROJECT_BINARIES)

.PHONY: \
	all local subprograms clean clean-local clean-subprograms run strip upx \
	release release-size release-speed release-native release-upx check-i18n

# Build the standalone helper programs first. hyprtile-wallpaperd reuses protocol
# files generated by hyprtile-screener-build. Making the local build a second
# recursive step preserves that ordering even when the user invokes make -j.
all: subprograms
	$(MAKE) local

local: $(LOCAL_BINARIES)

subprograms:
	$(MAKE) -C hyprtile-remoteviewer-build WAYLAND_WLR=1
	$(MAKE) -C hyprtile-xmppclient
	$(MAKE) -C hyprtile-videoplayer-build
	$(MAKE) -C hyprtile-tts-server-build
	$(MAKE) -C hyprtile-tts-build
	$(MAKE) -C hyprtile-stt-build
	$(MAKE) -C hyprtile-screener-build
	$(MAKE) -C hyprtile-audioplayer-build
	$(MAKE) -C hyprtile-shotter

check-i18n:
	python3 tools/check_i18n.py --strict-unused --report I18N_AUDIT.md

$(TARGET): $(SRC)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

hyprtile-setaudiovol: tools/hyprtile-setaudiovol.c app_i18n.c app_json.c json.h
	$(CC) $(CFLAGS) -I. \
		-o $@ tools/hyprtile-setaudiovol.c app_i18n.c app_json.c \
		$(COMMON_LDFLAGS) \
		$(shell pkg-config --libs libpulse)

hyprtile-setbrightness: tools/hyprtile-setbrightness.c app_i18n.c app_json.c json.h
	$(CC) $(CFLAGS) -I. -o $@ tools/hyprtile-setbrightness.c app_i18n.c app_json.c $(COMMON_LDFLAGS)

hyprtile-batterystat: tools/hyprtile-batterystat.c
	$(CC) $(CFLAGS) -o $@ $< $(COMMON_LDFLAGS)

hyprtile-bt: tools/hyprtile-bt.c
	$(CC) $(CFLAGS) -o $@ $< $(COMMON_LDFLAGS)

hyprtile-wallpaperd: \
	tools/hyprtile-wallpaperd.c \
	hyprtile-screener-build/build/wlr-layer-shell-unstable-v1-protocol.c \
	hyprtile-screener-build/build/wlr-layer-shell-unstable-v1-client-protocol.h \
	hyprtile-screener-build/build/xdg-shell-protocol.c \
	hyprtile-screener-build/build/xdg-shell-client-protocol.h
	$(CC) $(CFLAGS) \
		-I. \
		-Ihyprtile-screener-build/build \
		-o $@ \
		tools/hyprtile-wallpaperd.c \
		hyprtile-screener-build/build/wlr-layer-shell-unstable-v1-protocol.c \
		hyprtile-screener-build/build/xdg-shell-protocol.c \
		$(COMMON_LDFLAGS) \
		$(shell pkg-config --libs \
			wayland-client wayland-egl egl glesv2 \
			libavformat libavcodec libavutil libswscale) \
		-lm

hyprtile-notes/hyprtile-notes: \
	hyprtile-notes/hyprtile-notes.c \
	app_i18n.c \
	app_json.c \
	json.h
	$(CC) $(CFLAGS) -I. -o $@ hyprtile-notes/hyprtile-notes.c app_i18n.c app_json.c $(COMMON_LDFLAGS)

hyprtile-pwsafe/hyprtile-pwsafe: \
	hyprtile-pwsafe/hyprtile-pwsafe.c \
	json.h
	$(CC) $(CFLAGS) \
		-o $@ $< \
		$(COMMON_LDFLAGS) \
		$(shell pkg-config --libs libsodium)

hyprtile-ai: \
	hyprtile-ai-build/hyprtile-ai.c \
	hyprtile_ai_protocol.h \
	app_i18n.c \
	app_json.c \
	json.h
	$(CC) $(CFLAGS) -I. -o $@ hyprtile-ai-build/hyprtile-ai.c app_i18n.c app_json.c $(COMMON_LDFLAGS)

strip: all
	strip --strip-all -- $(BINARIES)

upx: strip
	@command -v upx >/dev/null 2>&1 || { \
		printf 'Error: UPX is not installed.\n' >&2; \
		exit 1; \
	}
	upx --best --lzma -- $(BINARIES)

# Default portable release: optimized for size, stripped, no UPX.
release: release-size

release-size:
	$(MAKE) clean
	$(MAKE) PROFILE=size strip

# Faster portable release, still compatible with other CPUs.
release-speed:
	$(MAKE) clean
	$(MAKE) PROFILE=speed strip

# Maximum speed for the current machine only.
release-native:
	$(MAKE) clean
	$(MAKE) PROFILE=native strip

release-upx:
	$(MAKE) clean
	$(MAKE) PROFILE=size upx

run: $(TARGET)
	./$(TARGET)

clean: clean-local clean-subprograms

clean-local:
	rm -f -- $(LOCAL_BINARIES)

clean-subprograms:
	$(MAKE) -C hyprtile-remoteviewer-build clean
	$(MAKE) -C hyprtile-xmppclient clean
	$(MAKE) -C hyprtile-videoplayer-build clean
	$(MAKE) -C hyprtile-tts-server-build clean
	$(MAKE) -C hyprtile-tts-build clean
	$(MAKE) -C hyprtile-stt-build clean
	$(MAKE) -C hyprtile-screener-build clean
	$(MAKE) -C hyprtile-audioplayer-build clean
	$(MAKE) -C hyprtile-shotter clean
