#!/usr/bin/make -f

export DH_VERBOSE = 1

include /usr/share/dpkg/architecture.mk

# Debian host arch -> Rust target triple, for the arches we ship.
RUST_TARGET_amd64 = x86_64-unknown-linux-gnu
RUST_TARGET_arm64 = aarch64-unknown-linux-gnu
RUST_TARGET_armhf = armv7-unknown-linux-gnueabihf
RUST_TARGET       = $(RUST_TARGET_$(DEB_HOST_ARCH))

# When the host arch differs from the build arch, drive cargo through the
# matching cross toolchain: build for the host triple, link with the host
# gcc, and resolve the -sys crates' system libraries (pam, xkbcommon,
# wayland) against the host architecture via its pkg-config wrapper.
ifneq ($(DEB_HOST_ARCH),$(DEB_BUILD_ARCH))
RUST_TARGET_ENV = $(shell echo $(RUST_TARGET) | tr 'a-z.-' 'A-Z__')
export CARGO_TARGET_$(RUST_TARGET_ENV)_LINKER = $(DEB_HOST_GNU_TYPE)-gcc
export CC_$(subst -,_,$(RUST_TARGET)) = $(DEB_HOST_GNU_TYPE)-gcc
export PKG_CONFIG = $(DEB_HOST_GNU_TYPE)-pkg-config
export PKG_CONFIG_ALLOW_CROSS = 1
CARGO_MAKE_ARGS = CARGO_TARGET=$(RUST_TARGET)
endif

%:
	dh $@

override_dh_auto_configure:

override_dh_auto_build:
	$(MAKE) $(CARGO_MAKE_ARGS)

override_dh_auto_install:
	$(MAKE) install $(CARGO_MAKE_ARGS) DESTDIR=$(CURDIR)/debian/eydos-loginmanager PREFIX=/usr
	$(MAKE) install-smartcast $(CARGO_MAKE_ARGS) DESTDIR=$(CURDIR)/debian/eydos-smartcast PREFIX=/usr

override_dh_auto_test:

override_dh_auto_clean:
	$(MAKE) clean || true

# A display manager that's mid-supervising a greeter (and possibly a live
# user session) must not be torn down mid-upgrade — both children share the
# daemon's systemd cgroup, so the default `dh_installsystemd` behaviour of
# `systemctl restart` on every upgrade kills the greeter and the user
# session along with the daemon. `--no-stop-on-upgrade` keeps prerm from
# stopping the unit; `--no-restart-after-upgrade` keeps postinst from
# restarting it. The new code only takes effect on the next reboot or
# manual `systemctl restart` — which is the right semantics for a DM.
override_dh_installsystemd:
	dh_installsystemd --no-stop-on-upgrade --no-restart-after-upgrade
