#!/usr/bin/make -f
#
# SPDX-FileCopyrightText: 2022-2026 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

export DH_VERBOSE = 1
export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# read /usr/share/perl5/Debian/Debhelper/Buildsystem/cargo.pm and /usr/share/cargo/bin/cargo !
export CARGO_HOME=$(CURDIR)/debian/cargo_home
export PATH := /usr/share/cargo/bin:$(PATH)

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/rustc/architecture.mk
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE

override_dh_auto_build:
	cargo build --release --workspace

override_dh_auto_test:
	cargo test --release --workspace

override_dh_auto_install:
	# Cargo has no install mechanism for cdylib crates.
	# Install the built shared objects explicitly; the artifact directory
	# depends on whether cargo was invoked with an explicit --target.
	set -e; \
	sasl_so=$$(find target -path '*/release/liboauthbearer.so' | head -n1); \
	pam_so=$$(find target -path '*/release/libpam_oauthbearer.so' | head -n1); \
	install -d debian/libsasl2-modules-oauthbearer/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2; \
	install -m 0644 "$$sasl_so" debian/libsasl2-modules-oauthbearer/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2/liboauthbearer.so; \
	install -d debian/libpam-oauthbearer/usr/lib/$(DEB_HOST_MULTIARCH)/security; \
	install -m 0644 "$$pam_so" debian/libpam-oauthbearer/usr/lib/$(DEB_HOST_MULTIARCH)/security/pam_oauthbearer.so

	install -D -m 0644 sasl/sasl_oauthbearer.5 debian/tmp/usr/share/man/man5/sasl_oauthbearer.5
	install -D -m 0644 pam/pam_oauthbearer.5 debian/tmp/usr/share/man/man5/pam_oauthbearer.5

override_dh_auto_clean:
	cargo clean || true

%:
	dh $@ --with cargo --buildsystem=cargo
