#! /usr/bin/make -f

#export DH_VERBOSE = 1
#export DEB_BUILD_OPTIONS = nostrip

export LC_ALL = C.UTF-8

include /usr/share/dpkg/default.mk
include /usr/share/debhelper/dh_package_notes/package-notes.mk

ifeq ($(DEB_VENDOR),Ubuntu)
    DEFAULT_NTP_SERVERS = ntp.ubuntu.com
    SUPPORT_URL = http://www.ubuntu.com/support
    CONFFLAGS_DISTRO =
else
    DEFAULT_NTP_SERVERS = 0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
    SUPPORT_URL = https://www.debian.org/support
    CONFFLAGS_DISTRO =
endif

TEST_TIMEOUT_MULTIPLIER = "-t 10"

# fail on missing files and symbols changes on distro builds, but not if we
# build/test upstream master
ifeq ($(TEST_UPSTREAM),)
    DH_MISSING = --fail-missing
    GENSYMBOLS_LEVEL = 4
else
    DH_MISSING = --list-missing
    GENSYMBOLS_LEVEL = 1
endif

CONFFLAGS = \
	-Db_lto=true \
	-Db_pie=true \
	-Dmode=release \
	-Drootlibdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	-Dsplit-usr=true \
	-Dversion-tag="$(DEB_VERSION)" \
	-Dquotaon-path=/sbin/quotaon \
	-Dquotacheck-path=/sbin/quotacheck \
	-Dkmod-path=/bin/kmod \
	-Dkexec-path=/sbin/kexec \
	-Dsulogin-path=/sbin/sulogin \
	-Dmount-path=/bin/mount \
	-Dumount-path=/bin/umount \
	-Dloadkeys-path=/bin/loadkeys \
	-Dsetfont-path=/bin/setfont \
	-Dnologin-path=/usr/sbin/nologin \
	-Dtelinit-path=/lib/sysvinit/telinit \
	-Dsysvinit-path=/etc/init.d \
	-Dsysvrcnd-path=/etc \
	-Dlocalegen-path=/usr/sbin/locale-gen \
	-Ddebug-shell=/bin/bash \
	-Dzshcompletiondir=/usr/share/zsh/vendor-completions \
	-Ddbuspolicydir=/usr/share/dbus-1/system.d/ \
	-Dsupport-url=$(SUPPORT_URL) \
	-Ddefault-kill-user-processes=false \
	-Ddefault-hierarchy=unified \
	-Ddefault-dnssec=no \
	-Dpamconfdir=no \
	-Dpamlibdir=/lib/$(DEB_HOST_MULTIARCH)/security \
	-Drpmmacrosdir=no \
	-Dvconsole=false \
	-Dfirstboot=true \
	-Dsysupdate=false \
	-Dxkbcommon=false \
	-Dwheel-group=false \
	-Dpwquality=false \
	-Doomd=true \
	-Durlify=true \
	-Dgnutls=false \
	-Dlink-udev-shared=false \
	-Dlink-systemctl-shared=false \
	-Dbump-proc-sys-fs-nr-open=false \
	-Dntp-servers="$(DEFAULT_NTP_SERVERS)" \
	-Ddns-servers='' \
	-Dsystem-uid-max=999 \
	-Dsystem-gid-max=999 \
	-Dnobody-user=nobody \
	-Dnobody-group=nogroup \
	-Ddev-kvm-mode=0660 \
	-Dgroup-render-mode=0660 \
	-Drepart=true \
	-Dfdisk=true \
	-Dsysext=true \
	-Dnscd=true \
	-Dselinux=true \
	-Dhwdb=true \
	-Dsysusers=true \
	-Dinstall-tests=$(if $(filter noinsttest,$(DEB_BUILD_PROFILES)),false,true) \
	-Defi=true \
	-Dman=$(if $(filter nodoc,$(DEB_BUILD_PROFILES)),false,true) \
	-Dtranslations=true \
	-Dnss-myhostname=true \
	-Dnss-mymachines=true \
	-Dnss-resolve=true \
	-Dnss-systemd=true \
	-Dresolve=true \
	-Dstatus-unit-format-default=combined \
	-Dstandalone-binaries=true

ifeq (, $(filter stage1, $(DEB_BUILD_PROFILES)))
CONFFLAGS += \
	-Daudit=true \
	-Dlibcryptsetup=true \
	-Dlibcryptsetup-plugins=true \
	-Dcoredump=true \
	-Delfutils=true \
	-Dapparmor=true \
	-Dlibidn2=true \
	-Dlibiptc=true \
	-Dlibcurl=true \
	-Dimportd=true \
	-Dmicrohttpd=true \
	-Dopenssl=true \
	-Dcryptolib=openssl \
	-Ddns-over-tls=openssl \
	-Dlibfido2=true \
	-Dtpm2=true \
	-Dp11kit=true \
	-Dqrencode=true \
	-Dhomed=true \
	-Duserdb=true \
	-Dpcre2=true
else
CONFFLAGS += \
	-Daudit=false \
	-Dlibcryptsetup=false \
	-Dlibcryptsetup-plugins=false \
	-Dcoredump=false \
	-Delfutils=false \
	-Dapparmor=false \
	-Dlibidn2=false \
	-Dlibiptc=false \
	-Dlibcurl=false \
	-Dimportd=false \
	-Dmicrohttpd=false \
	-Dopenssl=false \
	-Dlibfido2=false \
	-Dtpm2=false \
	-Dp11kit=false \
	-Dqrencode=false \
	-Dhomed=false \
	-Duserdb=false \
	-Dpcre2=false
endif

override_dh_auto_configure:
	dh_auto_configure \
		-- $(CONFFLAGS) $(CONFFLAGS_DISTRO) $(CONFFLAGS_UPSTREAM)

execute_before_dh_auto_build:
	# blhc false positives: C++ fuzz test program, cc -E flags listing, PE-COFF EFI binaries
	@echo 'blhc: ignore-line-regexp: .* -o test-bus-vtable-cc.*'
	@echo 'blhc: ignore-line-regexp: .*cc -E.*'
	@echo 'blhc: ignore-line-regexp: .* -o src/boot/efi.*'

execute_after_dh_auto_build:
	# generate POT file for translators
	ninja -C obj-$(DEB_HOST_GNU_TYPE) systemd-pot

execute_after_dh_auto_install:
	# fix paths in manpages; manually check the remaining /usr occurrences
	# occasionally, with filtering out paths which are known to be in /usr:
	# grep -r /usr debian/tmp/usr/share/man/|egrep -v '/usr/local|os.*release|factory|zoneinfo|tmpfiles|kernel|foo|machines|sysctl|dbus|include|binfmt'
	if test -d debian/tmp/usr/share/man; then \
		find debian/tmp/usr/share/man/ -type f | xargs sed -ri 's_/usr(/lib/systemd/system|/lib/systemd/network|/lib/udev|/lib[^/]|/lib/[^a-z])_\1_g'; \
	fi

execute_before_dh_auto_clean:
ifneq (, $(TEST_UPSTREAM))
	debian/extra/checkout-upstream
endif

execute_after_dh_auto_clean:
	rm -f debian/shlibs.local
	# remove Python byte code files
	rm -rf tools/__pycache__/
	rm -rf tools/chromiumos/__pycache__/
	rm -f po/systemd.pot

execute_before_dh_install:
	# remove unnecessary / unused files
	rm -rf debian/tmp/usr/share/doc/systemd/LICENSES/
	rm -f debian/tmp/usr/share/doc/systemd/LICENSE.*
	rm -f debian/tmp/etc/init.d/README
	rm -f debian/tmp/lib/*.d/README
	rm -f debian/tmp/lib/udev/*.d/README
	rm -f debian/tmp/usr/lib/*.d/README
	rm -f debian/tmp/usr/lib/sysctl.d/50-default.conf
	rm -f debian/tmp/etc/X11/xinit/xinitrc.d/50-systemd-user.sh
	rmdir -p --ignore-fail-on-non-empty debian/tmp/etc/X11/xinit/xinitrc.d/
	# those files should probably be shipped in a -dev package, remove for now
	rm -rf debian/tmp/usr/share/dbus-1/interfaces/
	# remove files related to factory-reset feature
	find debian/tmp/ \( -name 'systemd-update-done*' -o \
		-name systemd-journal-catalog-update.service -o \
		-name systemd-hwdb-update.service -o \
		-name ldconfig.service -o \
		-name etc.conf \) -print -delete
	rm -rf debian/tmp/usr/share/factory/
	# replace upstream sysusers.d/basic.conf with proper users for Debian
	debian/extra/make-sysusers-basic > debian/tmp/usr/lib/sysusers.d/basic.conf
	# remove obsolete compat symlink
	rm -f debian/tmp/usr/bin/systemd-resolve

execute_after_dh_install:
	# install standalone binaries
	for pkg in sysusers tmpfiles; do \
		mkdir -p debian/systemd-standalone-$$pkg/bin; \
		mv debian/systemd/bin/systemd-$$pkg.standalone debian/systemd-standalone-$$pkg/bin/systemd-$$pkg; \
	done

	# we don't want /tmp to be a tmpfs by default
	mv debian/systemd/lib/systemd/system/tmp.mount debian/systemd/usr/share/systemd/
	printf '\n[Install]\nWantedBy=local-fs.target\n' >> debian/systemd/usr/share/systemd/tmp.mount
	rm debian/systemd/lib/systemd/system/local-fs.target.wants/tmp.mount

	# files shipped by cryptsetup
ifeq (, $(filter stage1, $(DEB_BUILD_PROFILES)))
	rm -f debian/tmp/usr/share/man/man5/crypttab.5
endif

	# files shipped by systemd
	rm debian/udev/lib/udev/rules.d/70-uaccess.rules
	rm debian/udev/lib/udev/rules.d/73-seat-late.rules
	rm debian/udev/lib/udev/rules.d/71-seat.rules
	rm debian/udev/lib/udev/rules.d/99-systemd.rules

	# Ubuntu specific files
ifeq ($(DEB_VENDOR),Ubuntu)
	install -D --mode=644 debian/extra/udev.py debian/udev/usr/share/apport/package-hooks/udev.py
	install -D --mode=644 debian/extra/systemd.py debian/systemd/usr/share/apport/package-hooks/systemd.py
	install --mode=644 debian/extra/rules-ubuntu/*.rules debian/udev/lib/udev/rules.d/
	cp -a debian/extra/units-ubuntu/* debian/systemd/lib/systemd/system/
endif

execute_after_dh_installman:
	# remove duplicate files shipped by systemd-*/udev
	# run after dh_installman, which runs after dh_install, to include manpages
	echo "Removing duplicate files in systemd package:"
	set -e; for pkg in $(shell dh_listpackages -Nudev-udeb -Nlibudev1-udeb -Nsystemd -Nsystemd-standalone-sysusers -Nsystemd-standalone-tmpfiles); do \
		echo "... from $$pkg..."; \
		(cd debian/$$pkg; find -type f -o -type l) | (cd debian/systemd; xargs rm -f --verbose); \
		(cd debian/$$pkg; find -mindepth 1 -type d | sort -r) | (cd debian/systemd; xargs rmdir --ignore-fail-on-non-empty --verbose || true); \
	done

	# dh_installman is affected by false positives and mangles manpages named as lib*.so.*
	# fixed in debhelper 13.7.2 by https://salsa.debian.org/debian/debhelper/-/merge_requests/69
	for pkg in libnss-myhostname libnss-mymachines libnss-resolve libnss-systemd; do \
		rm -rf debian/$$pkg/usr/share/man/so; \
	done
	# work around some more dh_installman issues
	# see https://salsa.debian.org/debian/debhelper/-/merge_requests/69#note_316102
	for pkg in libnss-myhostname libnss-mymachines libnss-resolve libnss-systemd; do \
		rm -rf debian/$$pkg/usr/share/man/man2/; \
	done

override_dh_missing:
	dh_missing $(DH_MISSING)

override_dh_installinit:
	dh_installinit --no-start

override_dh_installsystemd:
	dh_installsystemd -psystemd-timesyncd
	dh_installsystemd -psystemd-oomd systemd-oomd.service
	dh_installsystemd -psystemd-oomd --no-stop-on-upgrade systemd-oomd.socket
	dh_installsystemd -psystemd-container --no-stop-on-upgrade machines.target
	dh_installsystemd -psystemd-userdbd --no-stop-on-upgrade systemd-userdbd.socket
	dh_installsystemd -psystemd-homed --no-also systemd-homed.service systemd-homed-activate.service
	dh_installsystemd -psystemd-resolved

override_dh_installsystemduser:

PROJECT_VERSION ?= $(shell awk '/(PROJECT|PACKAGE)_VERSION/ {print $$3}' obj-$(DEB_HOST_GNU_TYPE)/config.h | tr -d \")

# The SysV compat tools (which are symlinks to systemctl) are
# quasi-essential, so add their dependencies to Pre-Depends
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=753589
override_dh_shlibdeps:
	dh_shlibdeps -psystemd -Llibsystemd-shared -- \
		-dPre-Depends -edebian/systemd/bin/systemctl \
		-dDepends
	dh_shlibdeps -plibsystemd-shared -lusr/lib/$(DEB_HOST_MULTIARCH)/systemd
	dh_shlibdeps --remaining-packages -Llibsystemd-shared

override_dh_makeshlibs:
	sed 's/SHARED_LIB_VERSION/$(PROJECT_VERSION)/' debian/shlibs.local.in > debian/shlibs.local
	dh_makeshlibs -plibudev1 --add-udeb=libudev1-udeb -- -c$(GENSYMBOLS_LEVEL)
	dh_makeshlibs -plibsystemd-shared -Xlibsystemd-shared -Xlibsystemd-core -- -c$(GENSYMBOLS_LEVEL)
	dh_makeshlibs --remaining-packages -- -c$(GENSYMBOLS_LEVEL)

override_dh_auto_test:
ifeq (, $(filter nocheck, $(DEB_BUILD_OPTIONS)))
	# some tests hang under fakeroot, so disable fakeroot
	env -u LD_PRELOAD meson test -C obj-$(DEB_HOST_GNU_TYPE) --print-errorlogs $(TEST_TIMEOUT_MULTIPLIER)
endif

%:
	dh $@ --without autoreconf --buildsystem=meson