#!/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

# Reproducible timestamp
export KBUILD_BUILD_TIMESTAMP = "@$(SOURCE_DATE_EPOCH)"

# Disable libdmmp, no known users exist
export ENABLE_LIBDMMP=0

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
	# Force recreation of docs re. reproducibility
	rm -fv libdmmp/docs/man/dmmp_strerror.3

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:
	dh_auto_build --sourcedirectory=debian/build-deb -- \
		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 -- \
		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

	# Delete development files to prevent accidential linking of libraries with unlikely-to-be-stable ABI.
	rm \
		$(CURDIR)/debian/tmp/usr/include/mpath_cmd.h \
		$(CURDIR)/debian/tmp/usr/include/mpath_persist.h \
		$(CURDIR)/debian/tmp/usr/share/man/man3/mpath_persistent* \
		$(CURDIR)/debian/tmp/lib/libm*.so \
		$(CURDIR)/debian/tmp-multipath-udeb/lib/libm*.so

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_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
	# Don't auto-enable/start the service; instead, rely on
	# socket-activation.
	dh_installsystemd -pmultipath-tools multipathd.service \
		--no-stop-on-upgrade --no-enable
	dh_installsystemd -pmultipath-tools multipathd.socket

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