#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# OBS has no network at build time, so cargo builds from the vendor/ directory
# the orig tarball carries and never from crates.io (debian/README.source).
# CARGO_HOME has to be inside the tree as well: the build user on an OBS worker
# has no writable home.
export CARGO_HOME = $(CURDIR)/debian/cargo_home
export CARGO_NET_OFFLINE = true

# Leave stripping to dh_strip, so the -dbgsym package gets the line tables.
export CARGO_PROFILE_RELEASE_STRIP = false
export CARGO_PROFILE_RELEASE_DEBUG = line-tables-only

# Upstream needs rustc 1.89. trixie's is 1.85, so there it is rustc from
# trixie-backports; an Ubuntu whose plain rustc is older gets the versioned
# cargo-1.89. Build with the one that got installed.
CARGO := $(or $(wildcard /usr/bin/cargo-1.89),cargo)

%:
	dh $@

override_dh_auto_build:
	$(CARGO) build --release --locked --offline

# Upstream's tests talk to a live compositor and session bus.
override_dh_auto_test:

override_dh_auto_install:
	install -Dm755 target/release/xdg-desktop-portal-generic \
		debian/xdg-desktop-portal-generic/usr/libexec/xdg-desktop-portal-generic
	install -Dm644 data/generic.portal \
		debian/xdg-desktop-portal-generic/usr/share/xdg-desktop-portal/portals/generic.portal
	install -Dm644 data/org.freedesktop.impl.portal.desktop.generic.service \
		debian/xdg-desktop-portal-generic/usr/share/dbus-1/services/org.freedesktop.impl.portal.desktop.generic.service
	install -Dm644 data/xdg-desktop-portal-generic.service \
		debian/xdg-desktop-portal-generic/usr/lib/systemd/user/xdg-desktop-portal-generic.service

# D-Bus activation starts it when a portals.conf routes to it. Enabled under
# graphical-session.target it would also run in every GNOME and KDE session.
override_dh_installsystemduser:
	dh_installsystemduser --no-enable

override_dh_auto_clean:
	$(CARGO) clean --offline
	$(RM) -r $(CARGO_HOME)

# dh_clean deletes *.orig as patch leftovers, and `cargo vendor` writes a
# Cargo.toml.orig in every crate it vendors.
override_dh_clean:
	dh_clean -XCargo.toml.orig
