#! /usr/bin/make -f

#export DH_VERBOSE = 1
#export DEB_BUILD_OPTIONS = nostrip

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
VERSION = $(shell dpkg-parsechangelog | sed -n -e '/^Version:/s/.*: //p')

ifeq ($(shell dpkg-vendor --query vendor),Ubuntu)
    DEFAULT_NTP_SERVERS = ntp.ubuntu.com
else
    DEFAULT_NTP_SERVERS = 0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
endif

ifneq (,$(findstring noudeb,$(DEB_BUILD_OPTIONS)))
export DEB_BUILD_PROFILES += noudeb
endif

# blocked by MIR LP #1488341 and wily FF
export DH_OPTIONS += -Nsystemd-journal-remote

CONFFLAGS = \
	--with-rootprefix= \
	--with-rootlibdir=/lib/$(DEB_HOST_MULTIARCH) \
	--with-zshcompletiondir=/usr/share/zsh/vendor-completions \
	--with-sysvinit-path=/etc/init.d \
	--with-sysvrcnd-path=/etc \
	--with-debug-shell=/bin/bash \
	--with-telinit=/lib/sysvinit/telinit \
	--enable-split-usr \
	--disable-qrencode \
	--disable-libidn \
	--disable-elfutils \
	--disable-vconsole \
	--disable-sysusers \
	--disable-firstboot \
	--disable-coredump \
	--disable-xkbcommon \
	--disable-libiptc \
	--disable-silent-rules \
	--with-ntp-servers="$(DEFAULT_NTP_SERVERS)"  \
	--with-system-uid-max=999 \
	--with-system-gid-max=999

CONFFLAGS_deb = \
	--enable-selinux \
	--enable-compat-libs \
	--enable-hwdb \
	--enable-efi \
	PYTHON=python3

ifeq (,$(findstring stage1,$(DEB_BUILD_PROFILES)))
CONFFLAGS_deb += \
	--enable-audit \
	--enable-libcryptsetup \
	--enable-apparmor \
	--enable-libcurl \
	--enable-importd \
	--disable-microhttpd \
	--enable-gnutls
else
CONFFLAGS_deb += \
	--disable-audit \
	--disable-libcryptsetup \
	--disable-apparmor \
	--disable-libcurl \
	--disable-importd \
	--disable-microhttpd \
	--disable-gnutls
endif

CONFFLAGS_udeb = \
	--disable-libcryptsetup \
	--disable-pam \
	--disable-audit \
	--disable-selinux \
	--disable-apparmor \
	--disable-smack \
	--disable-ima \
	--disable-binfmt \
	--disable-bootchart \
	--disable-quotacheck \
	--disable-tmpfiles \
	--disable-randomseed \
	--disable-backlight \
	--disable-logind \
	--disable-machined \
	--disable-libcurl \
	--disable-importd \
	--disable-microhttpd \
	--disable-gnutls \
	--disable-hostnamed \
	--disable-timedated \
	--disable-networkd \
	--disable-timesyncd \
	--disable-localed \
	--disable-myhostname \
	--disable-resolved \
	--disable-polkit \
	--disable-acl \
	--disable-gcrypt \
	--disable-rfkill \
	--disable-hwdb \
	--disable-manpages \
	--disable-efi \
	--disable-gnuefi \
	--disable-seccomp \
	--without-python

override_dh_auto_configure:
	dh_auto_configure --builddirectory=build-deb \
		-- $(CONFFLAGS) $(CONFFLAGS_deb)
ifeq (, $(filter noudeb, $(DEB_BUILD_PROFILES)))
	dh_auto_configure --builddirectory=build-udeb \
		-- $(CONFFLAGS) $(CONFFLAGS_udeb)
endif

override_dh_auto_build:
	dh_auto_build --builddirectory=build-deb
ifeq (, $(filter noudeb, $(DEB_BUILD_PROFILES)))
	dh_auto_build --builddirectory=build-udeb
endif
	# generate POT file for translators
	cd build-deb/po; srcdir=../../po intltool-update --pot --verbose

override_dh_auto_install:
	dh_auto_install --builddirectory=build-deb \
		--destdir=debian/install/deb
ifeq (, $(filter noudeb, $(DEB_BUILD_PROFILES)))
	dh_auto_install --builddirectory=build-udeb \
		--destdir=debian/install/udeb
endif
	# 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/install/deb/usr/share/man/|egrep -v '/usr/local|os.*release|factory|zoneinfo|tmpfiles|kernel|foo|machines|sysctl|dbus|include|binfmt'
	find debian/install/deb/usr/share/man/ -type f | xargs sed -ri 's_/usr(/lib/systemd|/lib/udev|/lib/modules-load.d|/lib/modules\\-load|/lib[^/]|/lib/[^a-z])_\1_g'

override_dh_auto_clean:
	dh_auto_clean --builddirectory=build-deb
ifeq (, $(filter noudeb, $(DEB_BUILD_PROFILES)))
	dh_auto_clean --builddirectory=build-udeb
endif
	rm -rf debian/install/
	# remove Python byte code files
	rm -rf tools/__pycache__/

override_dh_install:
	# remove unnecessary / unused files
	rm -f debian/install/*/usr/share/doc/systemd/LICENSE.*
	rm -f debian/install/*/var/log/README
	rm -f debian/install/*/etc/init.d/README
	rm -f debian/install/*/usr/share/man/man8/kernel-install.8
	rm -f debian/install/*/usr/bin/kernel-install
	rm -f debian/install/*/usr/share/zsh/vendor-completions/_kernel-install
	rm -f debian/install/*/usr/share/bash-completion/completions/kernel-install
	rm -rf debian/install/*/usr/lib/kernel/install.d
	rm -rf debian/install/*/etc/rpm/
	rm -rf debian/install/*/usr/lib/rpm/
	rm -rf debian/install/*/etc/kernel/
	rm -f debian/install/*/usr/lib/sysctl.d/50-default.conf
	find debian/install/ -name '*.la' -delete
	# remove files related to factory-reset feature
	find debian/install/ \( -name 'systemd-update-done*' -o \
		-name systemd-journal-catalog-update.service -o \
		-name systemd-udev-hwdb-update.service -o \
		-name ldconfig.service -o \
		-name etc.conf \) -delete
	rm -rf debian/install/*/usr/share/factory/
	# remove deprecated compatibility libraries
	rm -f debian/install/*/usr/lib/*/pkgconfig/libsystemd-journal.pc
	rm -f debian/install/*/usr/lib/*/pkgconfig/libsystemd-id128.pc
	rm -f debian/install/*/usr/lib/*/libsystemd-*.so
	rm -f debian/install/*/lib/*/libsystemd-*.so.*
	# remove symlinks enabling default-on services
	rm -rf debian/install/*/etc/systemd/system/*.target.wants/
	# blocked by MIR/FF in Ubuntu
	rm -rf debian/install/*/lib/systemd/systemd-journal-upload
	rm -rf debian/install/*/lib/systemd/system/systemd-journal-upload*
	rm -rf debian/install/*/etc/systemd/journal-upload.conf
ifeq (, $(filter noudeb, $(DEB_BUILD_PROFILES)))
	dh_install -pudev-udeb -plibudev1-udeb --sourcedir=debian/install/udeb
endif
	# still needs to mature a bit, and packaging
	rm -f debian/install/*/usr/lib/*/libnss_resolve*

	# fix location of NSS modules
	cd debian/install/deb/; for f in usr/lib/*/libnss_*; do mv $$f $${f#usr/}; done

	dh_install --remaining-packages --sourcedir=debian/install/deb --fail-missing

	# Make sure the runlevel services are known by systemd so their targets
	# get launched. See https://bugzilla.redhat.com/show_bug.cgi?id=1002806
	for t in poweroff rescue multi-user graphical reboot; do \
		mkdir -p debian/systemd/lib/systemd/system/$${t}.target.wants;\
		ln -s ../systemd-update-utmp-runlevel.service \
			debian/systemd/lib/systemd/system/$${t}.target.wants ;\
	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 (,$(findstring stage1,$(DEB_BUILD_PROFILES)))
	rm debian/systemd/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

	# remove duplicate files shipped by systemd-*/udev
	echo "Removing duplicate files in systemd package:"
	set -e; for pkg in $(shell dh_listpackages -Nudev-udeb -Nlibudev1-udeb -Nsystemd); 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) | (cd debian/systemd; xargs rmdir --ignore-fail-on-non-empty --verbose || true); \
	done

	# Ubuntu specific files
	if dpkg-vendor --is ubuntu; then \
		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/systemd-vconsole-setup.service debian/systemd/lib/systemd/system/; \
		install --mode=644 debian/extra/rules-ubuntu/*.rules debian/udev/lib/udev/rules.d/; \
	fi

ifeq (, $(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	echo "Checking that executables don't link to libraries in /usr..."
	set -e; for e in debian/systemd/lib/systemd/systemd* debian/udev/lib/systemd/systemd*; do \
	    echo " $$e"; \
	    OUT=`env -u LD_PRELOAD ldd $$e` || continue; if echo "$$OUT" | grep -q /usr; then \
	        echo "ERROR: $$e links to /usr"; echo "$$OUT"; exit 1; \
	    fi; \
	done
endif

override_dh_installinit:
	dh_installinit -R --no-start
	dh_installinit --name=lvm2 --no-start
	# the following are only upstart jobs
	dh_installinit --name=udevtrigger --noscripts
	dh_installinit --name=udevmonitor --noscripts
	dh_installinit --name=udev-fallback-graphics --noscripts

# 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 -- -dPre-Depends -edebian/systemd/bin/systemctl -dDepends
	dh_shlibdeps --remaining-packages

override_dh_makeshlibs:
	dh_makeshlibs -plibudev1 --add-udeb=libudev1-udeb -- -c4
	dh_makeshlibs --remaining-packages -- -c4

autoreconf:
	intltoolize -f -c
	autoreconf -f -i

override_dh_autoreconf:
	dh_autoreconf debian/rules -- autoreconf

override_dh_strip:
	dh_strip --dbg-package=systemd-dbg

# tests require /etc/machine-id to be present
override_dh_auto_test:
ifeq (, $(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	# some tests hang under fakeroot, so disable fakeroot
	env -u LD_PRELOAD make -C build-deb check || ( \
		cd build-deb; \
		for f in test-*.log; do echo "==== $$f ===="; cat $$f; done; \
		if [ ! -e /etc/machine-id ]; then echo "/etc/machine-id missing, ignoring test failures"; else exit 1; fi; )
endif

%:
	dh $@ --with autoreconf --parallel
