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

override_dh_auto_build-arch:
	dh_auto_build --sourcedirectory=debian/build-deb
	# 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-arch:
	dh_auto_install --sourcedirectory=debian/build-deb -- DESTDIR=$(CURDIR)/debian/tmp/deb
	dh_auto_install --sourcedirectory=debian/build-udeb -- DESTDIR=$(CURDIR)/debian/tmp/udeb

	# udebs dont get manpages
	rm -rf $(CURDIR)/debian/tmp/udeb/usr/share/man

	# Delete development files from udebs.
	rm \
		$(CURDIR)/debian/tmp/udeb/usr/include/mpath_cmd.h \
		$(CURDIR)/debian/tmp/udeb/usr/include/mpath_persist.h \
		$(CURDIR)/debian/tmp/udeb/usr/lib/libm*.so

override_dh_auto_install-indep:

execute_after_dh_install:
	if strings debian/multipath-udeb/usr/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_installsystemd:
	dh_installsystemd -pmultipath-tools multipathd.service multipathd-queueing.service \
		--no-stop-on-upgrade

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