#! /usr/bin/make -f

#export DH_VERBOSE = 1
#export DEB_BUILD_OPTIONS = nostrip

export LC_ALL = C.UTF-8
export DEB_CFLAGS_MAINT_APPEND = -ffat-lto-objects

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

ifeq (,$(filter uos Uos UOS,$(shell lsb_release -i -s)))
    DEBUG_SHELL_FLAG = /usr/bin/bash
else
    DEBUG_SHELL_FLAG = "/sbin/agetty -o '-p -- \\\\\\\\u' --noclear - linux"
endif

CONFFLAGS = \
	-Db_lto=true \
	-Db_pie=true \
	-Dstatic-libsystemd=true \
	-Dmode=release \
	-Dversion-tag="$(DEB_VERSION)" \
	-Dquotaon-path=/usr/sbin/quotaon \
	-Dquotacheck-path=/usr/sbin/quotacheck \
	-Dkmod-path=/usr/bin/kmod \
	-Dkexec-path=/usr/sbin/kexec \
	-Dsulogin-path=/usr/sbin/sulogin \
	-Dmount-path=/usr/bin/mount \
	-Dumount-path=/usr/bin/umount \
	-Dloadkeys-path=/usr/bin/loadkeys \
	-Dsetfont-path=/usr/bin/setfont \
	-Dnologin-path=/usr/sbin/nologin \
	-Dtelinit-path=/usr/sbin/telinit \
	-Dsysvinit-path=/etc/init.d \
	-Dsysvrcnd-path=/etc \
	-Dlocalegen-path=/usr/sbin/locale-gen \
	-Ddebug-shell=$(DEBUG_SHELL_FLAG) \
	-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=/usr/lib/$(DEB_HOST_MULTIARCH)/security \
	-Drpmmacrosdir=no \
	-Dvconsole=false \
	-Dfirstboot=true \
	-Dxkbcommon=disabled \
	-Dwheel-group=false \
	-Dpwquality=disabled \
	-Doomd=true \
	-Durlify=true \
	-Dgnutls=disabled \
	-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=enabled \
	-Dfdisk=enabled \
	-Dsysext=true \
	-Dnscd=true \
	-Dselinux=enabled \
	-Dhwdb=true \
	-Dsysusers=true \
	-Dinstall-tests=$(if $(filter noinsttest,$(DEB_BUILD_PROFILES)),false,true) \
	-Dman=$(if $(filter nodoc,$(DEB_BUILD_PROFILES)),disabled,enabled) \
	-Dtranslations=true \
	-Dnss-myhostname=true \
	-Dnss-mymachines=enabled \
	-Dnss-resolve=enabled \
	-Dnss-systemd=true \
	-Dresolve=true \
	-Dstatus-unit-format-default=combined \
	-Dstandalone-binaries=true \
	-Dseccomp=$(if $(filter mips64el loong64 sw64,$(DEB_HOST_ARCH)),disabled,enabled)

ifeq (, $(filter stage1, $(DEB_BUILD_PROFILES)))
CONFFLAGS += \
	-Daudit=enabled \
	-Dlibcryptsetup=enabled \
	-Dlibcryptsetup-plugins=enabled \
	-Dcoredump=true \
	-Delfutils=enabled \
	-Dapparmor=enabled \
	-Dlibidn2=enabled \
	-Dlibiptc=enabled \
	-Dlibcurl=enabled \
	-Dimportd=enabled \
	-Dmicrohttpd=enabled \
	-Dopenssl=enabled \
	-Dcryptolib=openssl \
	-Ddns-over-tls=openssl \
	-Dlibfido2=enabled \
	-Dtpm2=enabled \
	-Dp11kit=enabled \
	-Dqrencode=enabled \
	-Dhomed=enabled \
	-Duserdb=true \
	-Dpcre2=enabled \
	-Dukify=enabled \
	-Dbootloader=$(if $(filter x32,$(DEB_HOST_ARCH)),disabled,auto) \
	-Dsysupdate=enabled
else
CONFFLAGS += \
	-Daudit=disabled \
	-Dlibcryptsetup=disabled \
	-Dlibcryptsetup-plugins=disabled \
	-Dcoredump=false \
	-Delfutils=disabled \
	-Dapparmor=disabled \
	-Dlibidn2=disabled \
	-Dlibiptc=disabled \
	-Dlibcurl=disabled \
	-Dimportd=disabled \
	-Dmicrohttpd=disabled \
	-Dopenssl=disabled \
	-Dlibfido2=disabled \
	-Dtpm2=disabled \
	-Dp11kit=disabled \
	-Dqrencode=disabled \
	-Dhomed=disabled \
	-Duserdb=false \
	-Dpcre2=disabled \
	-Dukify=disabled \
	-Dbootloader=disabled \
	-Dsysupdate=disabled
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_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 src/ukify/test/__pycache__/
	rm -rf src/ukify/__pycache__/
	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/usr/lib/*.d/README
	rm -f debian/tmp/usr/lib/udev/*.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/
	# remove etc.conf and usr/share/factory for now, we are not using them yet
	find debian/tmp/ -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
	# we don't want repart and shutdown standalone binaries for now
	rm -f debian/tmp/usr/bin/systemd-repart.standalone
	rm -f debian/tmp/usr/lib/systemd/systemd-shutdown.standalone
	# files shipped by cryptsetup
ifeq (, $(filter stage1, $(DEB_BUILD_PROFILES)))
	rm -f debian/tmp/usr/share/man/man5/crypttab.5
endif
	# print empty directories, makes it easier to keep *.install files up-to-date
	find debian/tmp -type d -empty -print

execute_after_dh_install-arch:
	# we don't want /tmp to be a tmpfs by default
	mv debian/tmp/usr/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/tmp/usr/lib/systemd/system/local-fs.target.wants/tmp.mount

	# 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/usr/lib/udev/rules.d/
	cp -a debian/extra/units-ubuntu/* debian/systemd/usr/lib/systemd/system/
endif

	#deepin specific files
	install --mode=644 debian/extra/rules-deepin/*.rules debian/udev/usr/lib/udev/rules.d/
	install -D --mode=755 debian/extra/units-deepin/deepin-sys-shutdown.sh debian/systemd/usr/libexec/deepin-sys-shutdown.sh
	install --mode=644 debian/extra/units-deepin/*.service debian/systemd/usr/lib/systemd/system/

	# Remove empty directories from /usr/lib.
	# Those are not strictly needed and can trigger piuparts errors due to
	# accidential directory removal by dpkg on merged-/usr systems.
	find debian/systemd/usr/lib -type d -empty -print -delete

override_dh_missing:
	dh_missing $(DH_MISSING)

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
	dh_installsystemd -pudev systemd-udevd.service

	#enable deepin specific service
	dh_installsystemd -psystemd --no-start deepin-sys-shutdown.service

override_dh_installsystemduser:

execute_after_dh_installsysusers:
	dh_installsysusers -pudev --name=debian-udev

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/usr/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
