#!/usr/bin/make -f
#
# Univention Policy
#  rules file for the debian package
#
# SPDX-FileCopyrightText: 2003-2026 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk
include /usr/share/rustc/architecture.mk

export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE

# Use Debian's cargo wrapper.
export CARGO_HOME=$(CURDIR)/debian/cargo_home
export PATH := /usr/share/cargo/bin:$(PATH)

CARGO := /usr/share/cargo/bin/cargo

# The Rust bindings link against libuniventionpolicy, which is built by
# Autotools as part of the same source package. During build/test the library
# has not been installed yet and lives in lib/.libs.
CARGO_ENV = \
	LIBRARY_PATH="$(CURDIR)/lib/.libs" \
	LD_LIBRARY_PATH="$(CURDIR)/lib/.libs"

# dh-cargo derives the crate installation version from debian/changelog.
# Do the same for the manually installed librust-*-dev package.
DEB_UPSTREAM_VERSION := $(shell dpkg-parsechangelog -SVersion | sed 's/-[^-]*$$//')
CRATE_NAME := univentionpolicy
CRATE_DIR := debian/librust-univentionpolicy-dev/usr/share/cargo/registry/$(CRATE_NAME)-$(DEB_UPSTREAM_VERSION)

export DEB_CARGO_CRATE=$(CRATE_NAME)_$(DEB_UPSTREAM_VERSION)


%:
	dh $@ --with python3


override_dh_auto_clean:
	dh_auto_clean

	$(CARGO) clean
	$(RM) -r debian/cargo_home
	$(RM) -r debian/cargo_registry
	$(RM) .cargo-checksum.json

	$(RM) Makefile.in lib/Makefile.in include/Makefile.in include/univention/Makefile.in tools/Makefile.in
	$(RM) configure
	$(RM) aclocal.m4 config.* install-sh ltmain.sh missing mkinstalldirs depcomp


override_dh_auto_configure:
	libtoolize
	aclocal
	autoconf
	automake --add-missing

	dh_auto_configure

	# This is what the dh-cargo buildsystem normally does during configure.
	# Keep using Debian's cargo registry instead of accessing crates.io.
	cp debian/cargo-checksum.json .cargo-checksum.json
	$(RM) Cargo.lock
	$(CARGO) prepare-debian debian/cargo_registry --link-from-system


override_dh_auto_build:
	# Build the C library first. The Rust crate links against it.
	dh_auto_build

	# LIBRARY_PATH makes lib/.libs/libuniventionpolicy.so available to the
	# linker while building the Rust crate.
	$(CARGO_ENV) $(CARGO) build


override_dh_auto_test-arch:
	dh_auto_test

	# LIBRARY_PATH is needed while linking the Rust test executable.
	# LD_LIBRARY_PATH is needed when cargo executes it.
	$(CARGO_ENV) $(CARGO) test


override_dh_auto_install:
	dh_auto_install

	# Install the Rust crate source.
	#
	# dh-cargo normally copies the source tree into
	# /usr/share/cargo/registry/<crate>-<version>. Reproduce that here
	# without invoking Debhelper's cargo buildsystem.
	mkdir -p $(CRATE_DIR)

	find . -mindepth 1 -maxdepth 1 \
		! -name .git \
		! -name .pc \
		! -name debian \
		! -name target \
		-exec cp -a --parents {} $(CRATE_DIR)/ \;

	cp debian/cargo-checksum.json $(CRATE_DIR)/.cargo-checksum.json
	touch -d@$(SOURCE_DATE_EPOCH) $(CRATE_DIR)/Cargo.toml

	# The source package contains C, Python and Rust code. Only retain the
	# files belonging to the Rust crate in librust-univentionpolicy-dev.
	$(RM) -r \
		$(CRATE_DIR)/.pybuild \
		$(CRATE_DIR)/.pytest_cache \
		$(CRATE_DIR)/autom4te.cache \
		$(CRATE_DIR)/doc \
		$(CRATE_DIR)/etc \
		$(CRATE_DIR)/include \
		$(CRATE_DIR)/lib \
		$(CRATE_DIR)/python \
		$(CRATE_DIR)/python-lib \
		$(CRATE_DIR)/scripts \
		$(CRATE_DIR)/tools \
		$(CRATE_DIR)/tests

	find $(CRATE_DIR) \
		-type f \( \
			-name 'Makefile*' -o \
			-name '*.swp' -o \
			-name '*.pyc' -o \
			-name 'aclocal.m4' -o \
			-name 'config*' -o \
			-name 'compile' -o \
			-name 'depcomp*' -o \
			-name 'missing' -o \
			-name 'install-sh' -o \
			-name 'libtool' -o \
			-name 'ltmain.sh' -o \
			-name '*.pm' -o \
			-name 'setup.py' -o \
			-name 'setup.cfg' -o \
			-name '.coverage' -o \
			-name 'pytest.xml' \
		\) -delete
