#!/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
export CARGO = /usr/share/cargo/bin/cargo
export CARGO_VENDOR_DIR = rust-vendor
export CARGO_HOME = $(CURDIR)/debian/cargo_home

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk
include /usr/share/rustc/architecture.mk
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE

ifeq ($(DEB_HOST_ARCH),s390x)
	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
endif

# Enable signing in Launchpad only, and on s390x only
SIGN_SIPL=
ifeq ($(DEB_HOST_ARCH),s390x)
	ifneq (,$(wildcard /CurrentlyBuilding))
		SIGN_SIPL=yes
	endif
endif

VENDOR_TARBALL = s390-tools_$(DEB_VERSION_UPSTREAM).orig-$(CARGO_VENDOR_DIR).tar.xz

vendor-tarball-sanity-check:
	if [ -e ../$(VENDOR_TARBALL) ]; then echo "../$(VENDOR_TARBALL) already exists, bailing!"; exit 1; fi

vendor-deps:
	if QUILT_PATCHES=debian/patches quilt applied | grep vendor-remove-unused-deps ; then \
		echo "Detecting patch on vendor dir applied, aborting."; \
		exit 1; \
	fi
	rm -rf $(CARGO_VENDOR_DIR)
	# Deliberately don't use the wrapper, as it expects the configure step
	# to have occurred already.
	# If you have to modify the path here, don't forget to change the README.source doc
	# as well.
	cargo vendor --manifest-path rust/Cargo.toml $(CARGO_VENDOR_DIR)
	# Remove some crates that aren't useful for us and take up a lot of space
	rm -r \
		$(CARGO_VENDOR_DIR)/winapi \
		$(CARGO_VENDOR_DIR)/winapi-* \
		$(CARGO_VENDOR_DIR)/windows-* \
		$(CARGO_VENDOR_DIR)/windows_* \
		# End of list
	# Remove the C sources from the binding crates, we're using the system libs
	rm -r \
		$(CARGO_VENDOR_DIR)/libz-sys/src/zlib \
		$(CARGO_VENDOR_DIR)/libz-sys/src/zlib-ng \
		$(CARGO_VENDOR_DIR)/curl-sys/curl \
		# End of list
	# Remove unused and undesirable data files (e.g. test data for vendored crates)
	rm -r \
		$(CARGO_VENDOR_DIR)/vcpkg/test-data \
		$(CARGO_VENDOR_DIR)/*/tests \
		# End of list
	# Remove the checksum files to allow us to patch the crates to remove extraneous dependencies
	for crate in $(CARGO_VENDOR_DIR)/*; do \
		sed -i 's/^{"files":.*"package":"\([a-z0-9]\+\)"}$$/{"files":{},"package":"\1"}/' $$crate/.cargo-checksum.json; \
	done
	# Cleanup temp files
	rm -rf $(CARGO_HOME)

vendor-tarball: vendor-tarball-sanity-check vendor-deps
	tar -caf ../$(VENDOR_TARBALL) $(CARGO_VENDOR_DIR)

%:
	dh $@

override_dh_auto_configure:
	DEB_CARGO_CRATE=s390-tools_$(shell dpkg-parsechangelog --show-field Version) \
	$(CARGO) prepare-debian $(CARGO_VENDOR_DIR)
	/usr/share/cargo/bin/dh-cargo-vendored-sources
	dh_auto_configure

override_dh_install:
ifeq ($(DEB_HOST_ARCH),s390x)
	# 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 -ps390-tools-chreipl-fcp-mpath
	dh_install -ps390-tools-data
	dh_install $(if $(SIGN_SIPL),-Xstage3.bin) -Xcpuplugd -Xosasnmpd -Xprocd -Xfsstatd -X60-readahead.rules -Xzkey -Xchreipl-fcp-mpath -Xdata -Xstage3a.bin -Xstage3b_reloc.bin
	$(if $(SIGN_SIPL),echo 'signing:Depends=s390-tools-signed (= $(DEB_VERSION))') >> debian/s390-tools.substvars
else
	dh_install -Xstage3a.bin -Xstage3b_reloc.bin
endif

override_dh_fixperms:
	dh_fixperms
ifeq ($(DEB_HOST_ARCH),s390x)
	chmod 644 debian/s390-tools-zkey/usr/libexec/zkey/zkey-ekmfweb.so
	chmod 644 debian/s390-tools-zkey/usr/libexec/zkey/zkey-kmip.so
endif

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
ifeq ($(DEB_HOST_ARCH),s390x)
	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 -
endif
override_dh_clean:
	dh_clean
	rm -rf $(CARGO_HOME)
	rm -rf $(signing)
