#!/usr/bin/make -f

# For Hardening
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk
export KBUILD_BUILD_TIMESTAMP = "@$(SOURCE_DATE_EPOCH)"
TREENAMES = build-deb build-udeb

%:
	dh $@

execute_before_dh_clean:
	for treename in $(TREENAMES); do \
		rm -rf debian/$$treename ;\
	done
	rm -rf debian/tmp-multipath-udeb

execute_before_dh_auto_configure:
	# no out of tree build support, prepare two source trees
	for treename in $(TREENAMES); do \
		mkdir debian/$$treename ;\
		cp -r -t debian/$$treename $$(find . -mindepth 1 -maxdepth 1 \! -name debian \! -name .git) ;\
	done

override_dh_auto_configure:

override_dh_auto_build:
	# parallel build is disabled to avoid race condition in man page build, which will end in
	# unreproducible contents (either regenerated man pages or shipped man pages will be
	# selected, depending on the race). #1030727
	dh_auto_build --sourcedirectory=debian/build-deb --max-parallel=1 -- \
		SCSI_DH_MODULES_PRELOAD="scsi_dh_alua scsi_dh_emc scsi_dh_rdac"
	# multipath-udeb: build separately; don't reference dynamic libgcc at runtime (#779579); disable systemd
	dh_auto_build --sourcedirectory=debian/build-udeb --max-parallel=1 -- \
		SYSTEMD= EXTRACFLAGS="-static-libgcc"

override_dh_auto_test:

override_dh_auto_install:
	dh_auto_install --sourcedirectory=debian/build-deb -- DESTDIR=$(CURDIR)/debian/tmp
	dh_auto_install --sourcedirectory=debian/build-udeb -- DESTDIR=$(CURDIR)/debian/tmp-multipath-udeb

	cp kpartx/del-part-nodes.rules debian/kpartx.del-part-nodes.udev
	cp kpartx/dm-parts.rules debian/kpartx.dm-parts.udev
	cp kpartx/kpartx.rules debian/kpartx.udev
	cp debian/tmp/lib/udev/rules.d/56-multipath.rules debian/multipath.udev
	cp multipath/11-dm-mpath.rules debian/dm-mpath.udev

execute_after_dh_install:
	if strings debian/multipath-udeb/lib/libmultipath.so.0 | grep libsystemd.so ; then \
		echo ERROR: udeb linked to libsystemd ;\
		exit 1 ;\
	fi

override_dh_bugfiles:
	dh_bugfiles -A -pmultipath-tools -pmultipath-tools-boot

override_dh_installinit:
	dh_installinit -pmultipath-tools-boot --name=multipath-tools-boot --no-start -- start 21 S .
	dh_installinit -pmultipath-tools-boot
	dh_installinit -pmultipath-tools

override_dh_installudev:
	dh_installudev -pkpartx
	dh_installudev -pkpartx --name=dm-parts --priority=56
	dh_installudev -pkpartx --name=del-part-nodes --priority=68
	dh_installudev -pmultipath-tools --name=multipath
	dh_installudev -pmultipath-tools --name=dm-mpath --priority=56

override_dh_installsystemd:
	dh_link -a lib/systemd/system/multipathd.service \
		lib/systemd/system/multipath-tools.service
	dh_installsystemd -pmultipath-tools multipathd.service

override_dh_makeshlibs:
	dh_makeshlibs -a --add-udeb=multipath-udeb
