#! /usr/bin/make -f

SHELL=/bin/bash -e

DEBIAN=debian

SOURCE=$(shell dpkg-parsechangelog -S Source)
VERSION=$(shell dpkg-parsechangelog -S Version)
ABI=$(shell echo "$(VERSION)" | sed -ne 's/\([0-9]*\.[0-9]*\.[0-9]*\-[0-9]*\)\..*/\1/p')

# Work out the source package name and version of the unsigned package
# By convention, it is the name of this package with -signed stripped.
# The version is required to be identical to this package.
UNSIGNED_SRC=$(shell echo $(SOURCE) | sed -e 's/-signed//')
UNSIGNED_VER=$(VERSION)

FLAVOURS=nvidia-tegra-igx nvidia-tegra-igx-rt
PACKAGED_BRANCHES=jetson r36.3.1 r36.4.1 lws
CONTROL_GEN_BRANCHES=jetson r36.4.1 lws

clean: debian/control
	dh_testdir
	dh_testroot
	rm -rf ./$(UNSIGNED_VER)
	dh_clean


debian/control:
	cp $(DEBIAN)/control.stub $(DEBIAN)/control.in
	rm -f $(DEBIAN)/control.branch
	for branch in $(CONTROL_GEN_BRANCHES); \
	do \
		branch_escaped=$${branch//./\\.}; \
		cat $(DEBIAN)/control.d/branch.stub | sed >>$(DEBIAN)/control.branch \
			-e "s/@BRANCH@/$$branch/g" \
			-e "/<!branch\.$$branch_escaped/d" \
			-e "/<branch\..*>/b @ ; p ; :@ /<branch\.$$branch_escaped>/!d"; \
	done
	for flavour in $(FLAVOURS); \
	do \
		cat $(DEBIAN)/control.d/flavour.stub $(DEBIAN)/control.branch | sed >>$(DEBIAN)/control.in \
			-e "s/@FLAVOUR@/$$flavour/g" \
			-e "/<!flavour\.$$flavour>/d" \
			-e "/<flavour\..*>/b @ ; p ; :@ /<flavour\.$$flavour>/!d"; \
	done
	cat $(DEBIAN)/control.in | sed -E >debian/control \
		-e "s/@ABI@/$(ABI)/g"				\
		-e "s/@UNSIGNED_SRC_PACKAGE@/$(UNSIGNED_SRC)/g"	\
		-e "s/@UNSIGNED_SRC_VERSION@/$(UNSIGNED_VER)/g"	\
		-e 's/@SRCPKGNAME@/$(SOURCE)/g' \
		-e 's/<!?flavour\..*>//g' \
		-e 's/<!?branch\..*>//g'

.PHONY: debian/control debian/dkms-packages templates override_dh_auto_build override_dh_auto_install clean

%:
	dh $@

override_dh_auto_build:
	./download-signed "$(UNSIGNED_SRC)" "$(UNSIGNED_VER)" "$(UNSIGNED_SRC)"

ifeq ($(arch),ppc64el)
instfile=vmlinux
else
instfile=vmlinuz
endif

define gen_branch_templates =
	for i in debian/templates.*branch*; \
	do \
		[ -e "$$i" ] || continue; \
		for b in $(PACKAGED_BRANCHES); \
		do \
			dest=`echo $$i | sed -e "s|branch|$$b|g"`; \
			cp -r "$$i" "$$dest"; \
		done \
	done
endef

define gen_templates =
	for i in debian/templates/*.in debian/templates.$(2)/*.in; \
	do \
		[ -e "$$i" ] || continue; \
		dest=`echo $$i | sed -e "s|templates\/||g" | \
				 sed -e "s|templates\.$(2)\/||g" | \
				 sed -e "s|\.in\$$||g" | \
				 sed -e "s|flavour|$(1)|g" | \
				 sed -e "s|kernelabiversion|$(ABI)|g" | \
				 sed -e "s|modpkg|$(2)|g"`; \
		sed -e "s|@abiname@|$(ABI)|g" \
			-e "s|@localversion@|-$(1)|g" \
			-e "s|@image-stem@|$(instfile)|g" \
			-e "s|@flavour@|$(1)|g" \
			-e "s|@kernelabiversion@|$(ABI)|g" \
			-e "s|@modpkg@|$(2)|g" "$$i" > "$$dest"; \
		chmod +x "$$dest"; \
	done
endef

override_dh_auto_install:
	$(call gen_branch_templates); \
	for flavour in $(FLAVOURS); \
	do \
		for file in $(UNSIGNED_VER)/$$flavour/*; \
		do \
			destpkg=$$(basename $$file); \
			find "$(UNSIGNED_VER)/$$flavour/$$destpkg/" -name "*.ko" -print | \
			while read -r path; \
			do \
				module=$$(basename "$$path"); \
				package="linux-modules-$$destpkg-$(ABI)-$$flavour"; \
				cp $$path $$path.tmp; \
				cat $$path.tmp $$path.sig >$$path; \
				rm $$path.tmp; \
				echo "$$package: adding $$module"; \
				dh_install -p"$$package" "$$path" "/lib/modules/$(ABI)-$$flavour/updates/$$destpkg"; \
			done; \
			$(call gen_templates,$$flavour,$$destpkg); \
		done; \
	done

	dh_install
