#!/usr/bin/make -f
# export DH_VERBOSE = 1

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)"

UDEB := $(filter open-iscsi-udeb,$(shell dh_listpackages))

ifneq ($(UDEB),)
TREENAMES = build-deb build-udeb
else
TREENAMES = build-deb
endif

%:
	dh $@

execute_before_dh_clean:
	for treename in $(TREENAMES); do \
		rm -rf debian/$$treename ;\
	done
	rm -rf debian/tmp-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:
	for treename in $(TREENAMES); do \
		cat debian/buildconfig.common debian/buildconfig.$$treename Makefile > debian/$$treename/Makefile ;\
		CFLAGS="$(CPPFLAGS) $(CFLAGS)" dh_auto_configure --sourcedirectory=debian/$$treename ;\
		CFLAGS="$(CPPFLAGS) $(CFLAGS)" dh_auto_configure --sourcedirectory=debian/$$treename/iscsiuio ;\
	done

override_dh_auto_build:
	for treename in $(TREENAMES); do \
		CFLAGS="$(CPPFLAGS) $(CFLAGS)" NO_SYSTEMD=$$NO_SYSTEMD dh_auto_build --sourcedirectory=debian/$$treename ;\
	done

override_dh_auto_install:
	# workaround upstream makefile parallelism problem
	dh_auto_install --no-parallel --sourcedirectory=debian/build-deb
ifneq ($(UDEB),)
	dh_auto_install --no-parallel --sourcedirectory=debian/build-udeb --destdir=$(CURDIR)/debian/tmp-udeb
endif

ifneq ($(UDEB),)
execute_after_dh_install:
	if strings debian/open-iscsi-udeb/usr/sbin/iscsid | grep libsystemd.so ; then \
		echo ERROR: udeb linked to libsystemd ;\
		exit 1 ;\
	fi
endif

override_dh_installinit:
	dh_installinit -p open-iscsi --no-start --no-enable --no-stop-on-upgrade --name=iscsid
	dh_installinit -p open-iscsi --no-start --no-enable --no-stop-on-upgrade
	dh_installinit -p iscsiuio --no-start --no-enable --no-stop-on-upgrade

override_dh_installsystemd:
	dh_installsystemd -p open-iscsi --no-restart-on-upgrade iscsid.socket
	dh_installsystemd -p open-iscsi --no-restart-on-upgrade open-iscsi.service
	dh_installsystemd -p iscsiuio --no-restart-on-upgrade iscsiuio.service

override_dh_installdocs:
	dh_installdocs -p open-iscsi README sysfs-documentation THANKS
	dh_installdocs -p iscsiuio iscsiuio/RELEASE.TXT iscsiuio/README
	dh_installdocs -p libopeniscsiusr
	dh_installdocs -p libopeniscsiusr-dev

override_dh_makeshlibs:
ifneq ($(UDEB),)
	dh_makeshlibs --add-udeb=open-iscsi-udeb
else
	dh_makeshlibs
endif

# The following is taken from the initramfs-tools package. (We recommend
# busybox in the initramfs because otherwise we don't have access to the
# ip utility to set the default gateway after iscsistart -N is done.)
# On Debian we can use either busybox or busybox-static, but on Ubuntu
# and derivatives only busybox-initramfs will work.
BUSYBOX_PACKAGES := $(shell if dpkg-vendor --derives-from ubuntu; then echo busybox-initramfs; else echo busybox busybox-static; fi)

override_dh_gencontrol:
	echo >> debian/open-iscsi.substvars "busybox:Recommends=$(wordlist 2,100,$(BUSYBOX_PACKAGES:%=| %))"
	dh_gencontrol
