#!/usr/bin/make -f
# -*- makefile -*-

include /usr/share/dpkg/default.mk
include /usr/share/rustc/architecture.mk
export CARGO=/usr/share/cargo/bin/cargo
export CARGO_HOME=$(shell pwd)/debian/cargo_home
export CARGO_REGISTRY=$(shell pwd)/debian/cargo_registry
export DEB_CARGO_CRATE=$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM)
export DEB_HOST_RUST_TYPE

%:
	dh $@ --buildsystem=cargo

# generate cargo-checksum file
_mkchecksum = printf '{"package":"%s","files":{}}\n' \
 $$(sha256sum $(or $2,$(dir $1)Cargo.toml) | grep -Po '^\S+') > $1;

execute_before_dh_auto_configure:
	$(call _mkchecksum,debian/cargo-checksum.json,Cargo.toml)
	$(CARGO) prepare-debian $(CARGO_REGISTRY) --link-from-system
	rm -f Cargo.lock

override_dh_auto_build:
	$(CARGO) build --release
	make -C man all

# we ship the build result, skipping dh-cargo's test build
override_dh_auto_test:

override_dh_auto_install:
	dh_install target/$(DEB_HOST_RUST_TYPE)/release/greetd /usr/sbin/
	dh_install target/$(DEB_HOST_RUST_TYPE)/release/agreety /usr/sbin/

execute_after_dh_fixperms:
	chmod a-x debian/greetd/lib/systemd/system/greetd.service

override_dh_installpam:
	dh_installpam --name=greetd
	dh_installpam --name=greetd-greeter

override_dh_installsystemd:
	dh_installsystemd --no-stop-on-upgrade --no-start

# similarly to https://bugzilla.redhat.com/show_bug.cgi?id=1564880
# and other cases (see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1016563)
# I got:
#   dwz: debian/greetd/usr/sbin/agreety: Couldn't find DIE at [2c8a] referenced by DW_AT_abstract_origin from DIE at [1ba4d]
#   dwz: debian/greetd/usr/sbin/greetd: Couldn't find DIE at [1036] referenced by DW_AT_abstract_origin from DIE at [e12b]
# Therefore I decide to skip it.
override_dh_dwz:

execute_after_dh_auto_clean:
	$(CARGO) clean
	rm -rf $(CARGO_HOME)
	rm -rf $(CARGO_REGISTRY)
	make -C man clean
	rm -f debian/cargo-checksum.json
