#! /usr/bin/make -f

SHELL=/bin/bash -e

DEBIAN=debian

SOURCE=$(shell dpkg-parsechangelog -S Source)
# Work out the source package name of the main kernel package linux-FLAVOUR
MAIN_SOURCE=$(shell echo "$(SOURCE)" | sed 's/-modules.*//')
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 nvidia-tegra-rt
PACKAGED_BRANCHES=jetson
# List of retired branches and their pinned ABI. Format is <branch>=<abi>
RETIRED_BRANCHES=
# Generate control stanzas for only these branches, i.e PACKAGED_BRANCHES/RETIRED_BRANCHES
CONTROL_GEN_BRANCHES=$(filter-out $(shell echo "$(RETIRED_BRANCHES)" | sed -E 's/(\S+)=\S+/\1/g'),$(PACKAGED_BRANCHES))

OVERRIDES=debian/file-overrides

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

branches:
	$(call gen_branch_files)
	$(call gen_branch_templates)

debian/control:
	cp $(DEBIAN)/control.stub $(DEBIAN)/control.in
	rm -f $(DEBIAN)/control.branch $(DEBIAN)/control.noflavour
	for branch in $(CONTROL_GEN_BRANCHES); \
	do \
		branch_escaped=$${branch//./\\.}; \
		for srcdst in "$(DEBIAN)/control.d/branch.stub $(DEBIAN)/control.branch" \
		              "$(DEBIAN)/control.d/branch-noflavour.stub $(DEBIAN)/control.noflavour"; \
		do \
			src=$${srcdst%% *}; \
			dst=$${srcdst##* }; \
			cat "$$src" | sed >> "$$dst" \
				-e "s/@BRANCH@/$$branch/g" \
				-e "/<!branch\.$$branch_escaped/d" \
				-e "/<branch\..*>/b @ ; p ; :@ /<branch\.$$branch_escaped>/!d"; \
		done \
	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"; \
	done
	cat $(DEBIAN)/control.in $(DEBIAN)/control.noflavour | 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/@MAINPKGNAME@/$(MAIN_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 branches

%:
	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_files =
	find debian/files.*branch* -type f | while read -r i; \
	do \
		[ -e "$$i" ] || continue; \
		for b in $(PACKAGED_BRANCHES); \
		do \
			dest=`echo $$i | sed -e "s|branch|$$b|g"`; \
			mkdir -p "$${dest%/*}"; \
			sed -e "s|@branch@|$$b|g" "$$i" > "$$dest"; \
		done \
	done
endef

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"`; \
			[ -e "$(OVERRIDES)/$$dest" ] && continue; \
			rm -rf "$$dest"; \
			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: install-defaults
	for flavour in $(FLAVOURS); \
	do \
		for file in $(UNSIGNED_VER)/$$flavour/*; \
		do \
			destpkg=$$(basename $$file); \
			mod_abi=$(ABI); \
			for branch in $(PACKAGED_BRANCHES); \
			do \
				echo "$$destpkg" | grep -q "$$branch" || continue; \
				branch_abi=$$(echo "$(RETIRED_BRANCHES)" | grep -oE "$$branch=\S+" | sed "s/$$branch=//"); \
				if ! [ -z "$$branch_abi" ]; then \
					mod_abi="$$branch_abi"; \
					continue; \
				fi; \
			done; \
			find "$(UNSIGNED_VER)/$$flavour/$$destpkg/" -name "*.ko" -print | \
			while read -r path; \
			do \
				module=$$(basename "$$path"); \
				package="linux-modules-$$destpkg-$$mod_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/$$mod_abi-$$flavour/updates/$$destpkg"; \
			done; \
			$(call gen_templates,$$flavour,$$destpkg); \
		done; \
	done

	dh_install

install-defaults: flavour = $(subst linux-,,$(MAIN_SOURCE))
install-defaults:
	for b in $(PACKAGED_BRANCHES); \
	do \
		$(call gen_templates,$(flavour),tegra-oot-$$b-defaults); \
		$(call gen_templates,$(flavour),tegra-oot-$$b-uc-defaults); \
		$(call gen_templates,$(flavour),tegra-igpu-$$b-defaults); \
	done
