#!/usr/bin/make -f

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# we need an apparmor profile for >= 4.0; this exists in Ubuntu >= 24.04, but not in Debian yet
PRE4AA = $(filter $(shell . /etc/os-release; echo $${VERSION_ID:-unstable}),22.04 11 12 unstable)

# riscv is an emulated architecture for now, and too slow to run expensive unit tests
# hppa's threading is absurdly slow (#981127)
SLOW_ARCHES = $(filter $(shell dpkg-architecture -qDEB_BUILD_ARCH),riscv64 hppa)
ifneq ($(SLOW_ARCHES),)
	export COCKPIT_SKIP_SLOW_TESTS=1
endif

export deb_systemdsystemunitdir = $(shell pkgconf --variable=systemdsystemunitdir systemd | sed s,^/,,)
# pam.pc doesn't yet have a libdir on older releases
export deb_pamlibdir = $(shell { pkgconf --variable=libdir pam || echo /lib/$(DEB_HOST_MULTIARCH); } | sed s,^/,,)

%:
	dh $@ --buildsystem=autoconf --with=python3

override_dh_auto_configure:
	dh_auto_configure -- \
		--disable-pcp \
		--with-pamdir=/$(deb_pamlibdir)/security \
		--libexecdir=/usr/lib/cockpit $(CONFIG_OPTIONS)

# HACK: Debian's pip breaks --prefix: https://bugs.debian.org/1035546 with
# default install layout
override_dh_auto_install:
	DEB_PYTHON_INSTALL_LAYOUT=deb dh_auto_install

# avoid trying to start cockpit-motd.service and cockpit-wsinstance-*.socket etc.
override_dh_installsystemd:
	dh_installsystemd -p cockpit-ws cockpit.socket

override_dh_install:
	# Debian/Ubuntu PAM config
	mkdir -p debian/tmp/etc/pam.d
	install -p -m 644 tools/cockpit.debian.pam debian/tmp/etc/pam.d/cockpit

	# don't ship broken branding symlinks
	for d in rhel fedora centos scientific opensuse; do rm -r debian/tmp/usr/share/cockpit/branding/$$d; done
	dpkg-vendor --derives-from ubuntu || rm -r debian/tmp/usr/share/cockpit/branding/ubuntu

	# handled by package maintainer scripts
	rm debian/tmp/etc/motd.d/cockpit debian/tmp/etc/issue.d/cockpit.issue

	# unpackaged modules
	rm -r debian/tmp/usr/share/cockpit/kdump
	rm debian/tmp/usr/share/metainfo/org.cockpit-project.cockpit-kdump.metainfo.xml
	rm -r debian/tmp/usr/share/cockpit/selinux
	rm debian/tmp/usr/share/metainfo/org.cockpit-project.cockpit-selinux.metainfo.xml

	dh_install -Xusr/src/debug
	# we don't need this, it contains full build paths and breaks reproducibility
	rm -r debian/tmp/usr/lib/python*/*-packages/*.dist-info

        # AppArmor profile
ifeq ($(PRE4AA),)
	mkdir -p debian/cockpit-ws/etc/apparmor.d/
	install -p -m 644 tools/apparmor.d/cockpit-desktop debian/cockpit-ws/etc/apparmor.d/
else
	# clean up in debian unstable after broken 317-1
	echo 'rm_conffile /etc/apparmor.d/cockpit-desktop 317-2~' > debian/cockpit-ws.maintscript
endif

	make install-tests DESTDIR=debian/cockpit-tests

execute_after_dh_install-indep:
	# avoid dh_missing failure
	rm -r debian/tmp/usr/lib/python*

# run pytests *after* installation, so that we can make sure that we installed the right files
execute_after_dh_install-arch:
ifeq (, $(findstring nocheck, $(DEB_BUILD_OPTIONS)))
ifeq ($(shell . /etc/os-release; echo $${VERSION_ID:-unstable}),22.04)
	PYTHONPATH=$$(ls -d debian/cockpit-bridge/usr/lib/python3*/dist-packages) python3 -m pytest -vv -k 'not linter and not test_descriptions'
else
	pytest -vv -k 'not linter and not test_descriptions' -opythonpath=$$(ls -d debian/cockpit-bridge/usr/lib/python3*/dist-packages)
endif
endif

# dh compat 14 does that automatically, remove when upgrading
execute_before_dh_installtmpfiles:
	dh_installsysusers
