PREFIX    ?= /usr
DESTDIR   ?=

# Backend daemons live under libexec — not on $PATH, only D-Bus / systemd
# activation needs to reach them. Matches the upstream convention used by
# `xdg-desktop-portal-gnome`, `-kde`, `-hyprland`, etc.
LIBEXECDIR = $(DESTDIR)$(PREFIX)/libexec
DATADIR    = $(DESTDIR)$(PREFIX)/share
SYSTEMDDIR = $(DESTDIR)$(PREFIX)/lib/systemd/user

PORTAL_DIR  = $(DATADIR)/xdg-desktop-portal/portals
DBUS_DIR    = $(DATADIR)/dbus-1/services

# Empty CARGO_TARGET => native build under target/release.
# Set it to a Rust target triple (e.g. aarch64-unknown-linux-gnu) to
# cross-compile; the binary then lands under target/$(CARGO_TARGET)/release.
CARGO_TARGET ?=
ifeq ($(CARGO_TARGET),)
TARGET_FLAGS =
TARGET_DIR   = target/release
else
TARGET_FLAGS = --target $(CARGO_TARGET)
TARGET_DIR   = target/$(CARGO_TARGET)/release
endif

.PHONY: all start install uninstall clean distclean arm64 arm64-deps

all:
	cargo build --release $(TARGET_FLAGS)

arm64-deps:
	sudo dpkg --add-architecture arm64
	sudo apt-get update
	sudo apt-get install crossbuild-essential-arm64 libstd-rust-dev:arm64

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

start: all
	./target/release/xdg-desktop-portal-crustace

install:
	install -Dm755 $(TARGET_DIR)/xdg-desktop-portal-crustace                   $(LIBEXECDIR)/xdg-desktop-portal-crustace
	install -Dm644 data/crustace.portal                                       $(PORTAL_DIR)/crustace.portal
	install -Dm644 data/org.freedesktop.impl.portal.desktop.crustace.service  $(DBUS_DIR)/org.freedesktop.impl.portal.desktop.crustace.service
	install -Dm644 data/xdg-desktop-portal-crustace.service                      $(SYSTEMDDIR)/xdg-desktop-portal-crustace.service

uninstall:
	rm -f $(LIBEXECDIR)/xdg-desktop-portal-crustace
	rm -f $(PORTAL_DIR)/crustace.portal
	rm -f $(DBUS_DIR)/org.freedesktop.impl.portal.desktop.crustace.service
	rm -f $(SYSTEMDDIR)/xdg-desktop-portal-crustace.service

clean:
	cargo clean

distclean: clean
	dh_clean
	rm -f Cargo.lock
