#! /usr/bin/make -f

##export DH_VERBOSE := 1

#VERSION := $(shell LC_ALL=C dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Work out the source package name and version.  We assume the source package
# is the name of this package with -signed stripped.  The version is identical
# to this package less any rebuild suffic (+signedN).
src_package := $(shell LC_ALL=C dpkg-parsechangelog | grep ^Source: | cut -d ' ' -f 2 | sed -e 's/-signed//')
src_fullversion = $(shell LC_ALL=C dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
src_version = $(shell echo $(src_fullversion) | sed -e 's/+signed[0-9][0-9]*.*//')
src_abi = $(shell echo "$(src_version)" | sed -ne 's/\([0-9]*\.[0-9]*\.[0-9]*\-[0-9]*\)\..*/\1/p')

test:
	echo "$(src_fullversion) $(src_version)"

# We build our control file.  This has to be done before dh runs otherwise
# we have no binary files and we will not run the appropriate targets.
pre-clean:
	sed <debian/control.stub >debian/control			\
		-e "s/ABI/$(src_abi)/g"					\
		-e "s/VERSION/$(src_version)/g"
	rm -rf ./$(src_version) UNSIGNED SIGNED
	rm -f 	debian/linux-image-*.install				\
		debian/linux-image-*.preinst 				\
		debian/linux-image-*.prerm 				\
		debian/linux-image-*.postinst 				\
		debian/linux-image-*.postrm
	rm -f 	debian/kernel-signed-image-*.install

PHONY: pre-clean

clean:: pre-clean

%:
	dh $@

override_dh_auto_build:
	./download-signed "linux-libc-dev" "$(src_version)" "$(src_package)"
	#./download-unsigned "$(DEB_HOST_ARCH)" "$(src_version)"
	mkdir SIGNED
	(									\
		cd "$(src_version)" || exit 1;					\
		for s in *.efi.signed; do					\
			[ ! -f "$$s" ] && continue;				\
			chmod 600 "$$s";					\
			base=$$(echo "$$s" | sed -e 's/.efi.signed//');		\
			ln "$$s" "../SIGNED/$$base";				\
		done;								\
		for s in *.opal.sig; do						\
			[ ! -f "$$s" ] && continue;				\
			chmod 600 "$$s";					\
			base=$$(echo "$$s" | sed -e 's/.opal.sig//');		\
			cat "$$base.opal" "$$s" >"../SIGNED/$$base";\
		done								\
	)

override_dh_auto_install:
	for signed in "SIGNED"/*; do						\
		flavour=$$(echo "$$signed" | sed -e "s@.*-$(src_abi)-@@");	\
		instfile=$$(echo "$$signed" | sed -e "s@[^/]*/@@"		\
			-e "s@-$(src_abi)-.*@@");				\
		verflav="$(src_abi)-$$flavour";					\
										\
		package="kernel-signed-image-$$verflav-di";			\
		echo "$$package: adding $$signed";				\
		echo "$$signed boot" >>"debian/$$package.install";		\
										\
		package="linux-image-$$verflav";				\
		echo "$$package: adding $$signed";				\
		echo "$$signed boot" >>"debian/$$package.install";		\
										\
		./generate-depends linux-image-unsigned-$$verflav $(src_version)	\
			linux-image-$$verflav					\
			>>"debian/linux-image-$$verflav.substvars";	\
										\
		for which in postinst postrm preinst prerm; do			\
			template="debian/templates/image.$$which.in";		\
			script="debian/$$package.$$which";			\
			sed -e "s/@abiname@/$(src_abi)/g"			\
			    -e "s/@localversion@/-$$flavour/g"			\
			    -e "s/@image-stem@/$$instfile/g"			\
				<"$$template" >"$$script";			\
		done;								\
		echo "interest linux-update-$(src_abi)-$$flavour" 		\
			>"debian/$$package.triggers";				\
	done
	dh_install

override_dh_builddeb:
	dh_builddeb
	for pkg in $$(dh_listpackages); do \
		case $$pkg in *dbgsym) ;; *) continue ;; esac; \
		mv ../$${pkg}_$(src_fullversion)_$(DEB_HOST_ARCH).deb \
		   ../$${pkg}_$(src_fullversion)_$(DEB_HOST_ARCH).ddeb; \
		sed -i "/^$${pkg}_/s/\.deb /.ddeb /" debian/files; \
	done

override_dh_fixperms:
	dh_fixperms -X/boot/
