#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
export DEB_CPPFLAGS_MAINT_APPEND = -D_GNU_SOURCE
export DEB_LDFLAGS_MAINT_APPEND=-rdynamic
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

options= V=1 HAVE_SNMP=1 SYSTEMDSYSTEMUNITDIR=/lib/systemd/system ZDEV_ALWAYS_UPDATE_INITRD=1 ZKEYKMSPLUGINDIR=/usr/libexec/zkey USRLIB64DIR=/usr/lib/s390x-linux-gnu

# Enable signing in Launchpad Only for now
SIGN_SIPL=
ifneq (,$(wildcard /CurrentlyBuilding))
SIGN_SIPL=yes
endif
SIGN_SIPL=yes

%:
	dh $@

override_dh_install:
	# no dracut
	rm -f debian/tmp/lib/s390-tools/zdev-root-update
	chmod -x debian/tmp/etc/*/*.conf debian/tmp/lib/s390-tools/zfcpdump/zfcpdump-initrd
	dh_install -ps390-tools-cpuplugd
	dh_install -ps390-tools-statd
	dh_install -ps390-tools-osasnmpd
	dh_install -ps390-tools-zkey
	dh_install $(if $(SIGN_SIPL),-Xstage3.bin) -Xcpuplugd -Xosasnmpd -Xprocd -Xfsstatd -X60-readahead.rules -Xzkey
	$(if $(SIGN_SIPL),echo 'signing:Depends=s390-tools-signed (= $(DEB_VERSION))') >> debian/s390-tools.substvars

override_dh_fixperms:
	dh_fixperms
	chmod 644 debian/s390-tools-zkey/usr/libexec/zkey/zkey-ekmfweb.so
	chmod 644 debian/s390-tools-zkey/usr/libexec/zkey/zkey-kmip.so
	chmod 755 debian/s390-tools-chreipl-fcp-mpath/usr/lib/chreipl-fcp-mpath/chreipl-fcp-mpath-common.sh

override_dh_auto_clean:
	rm -f zipl/boot/.*.d
	dh_auto_clean -- $(options)

override_dh_auto_build:
	dh_auto_build -- $(options)

override_dh_auto_install:
	HAVE_INITRAMFS=1 HAVE_DRACUT=1 dh_auto_install -- $(options)

signing=debian/s390-tools-$(DEB_VERSION)-signing/
signingv=$(signing)/$(DEB_VERSION)
signing_tar=$(DEB_SOURCE)_$(DEB_VERSION)_$(DEB_HOST_ARCH).tar.gz
override_dh_builddeb:
	dh_builddeb
	mkdir -p $(signingv)/control
	echo 'tarball' >$(signingv)/control/options
	cp debian/tmp/lib/s390-tools/stage3.bin $(signingv)/stage3.bin.sipl
	tar -C $(signing) -czvf $(CURDIR)/../$(signing_tar) .
	dpkg-distaddfile $(signing_tar) raw-signing -
override_dh_clean:
	dh_clean
	rm -rf $(signing)
