PREFIX    ?= /usr
DESTDIR   ?=

# Daemon goes under libexec — not on $PATH; D-Bus / systemd activation
# is how the rest of the session reaches it. Matches the convention
# `xdg-desktop-portal-crustace` and upstream's `-gnome` / `-kde` follow.
LIBEXECDIR = $(DESTDIR)$(PREFIX)/libexec
DATADIR    = $(DESTDIR)$(PREFIX)/share
SYSTEMDDIR = $(DESTDIR)$(PREFIX)/lib/systemd/user

DBUS_DIR    = $(DATADIR)/dbus-1/services

# CARGO_TARGET selects the Rust target triple. Empty means a native
# build (binary in target/release); set it to cross-compile, e.g.
# `make all CARGO_TARGET=aarch64-unknown-linux-gnu`.
CARGO_TARGET ?=
ifeq ($(CARGO_TARGET),)
	CARGO_FLAGS =
	RELEASE_DIR = target/release
else
	CARGO_FLAGS = --target $(CARGO_TARGET)
	RELEASE_DIR = target/$(CARGO_TARGET)/release
endif

.PHONY: all start install uninstall clean distclean

all:
	cargo build --release $(CARGO_FLAGS)

arm64-deps:
	sudo apt-get install libstd-rust-dev:arm64 libstd-rust-1.85:arm64 libwayland-dev:arm64 libwayland-egl1:arm64 libxkbcommon-dev:arm64 libegl-dev:arm64 libgles2-mesa-dev:arm64 libdbus-1-dev:arm64

arm64:
	$(MAKE) all CARGO_TARGET=aarch64-unknown-linux-gnu

start: all
	./$(RELEASE_DIR)/crustace-notifier

install:
	install -Dm755 $(RELEASE_DIR)/crustace-notifier                       $(LIBEXECDIR)/crustace-notifier
	install -Dm644 data/org.freedesktop.Notifications.service          $(DBUS_DIR)/org.freedesktop.Notifications.service
	install -Dm644 data/crustace-notifier.service                         $(SYSTEMDDIR)/crustace-notifier.service

uninstall:
	rm -f $(LIBEXECDIR)/crustace-notifier
	rm -f $(DBUS_DIR)/org.freedesktop.Notifications.service
	rm -f $(SYSTEMDDIR)/crustace-notifier.service

clean:
	cargo clean

distclean: clean
	dh_clean
	rm -f Cargo.lock
