#!/usr/bin/make -f
# You must remove unused comment lines for the released package.
#export DH_VERBOSE = 1
#export DH_OPTIONS = -v
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# setup kernel list only if debian/control is already generated
KERNELS := $(patsubst nerve-de01-kmod-%,%,$(shell [ -f debian/control ] && dh_listpackages | grep nerve-de01-kmod-))

################################################################################
# control and install files generation
################################################################################

comma:= ,
empty:=
space:= $(empty) $(empty)

# get package name(s) from string
# $(call apt-package,<pkg name>,<optional: shell filter pipe>
apt-package = $(shell apt-cache search --names-only $1 | cut -d' ' -f1 $2)

# the kernel suffixes that should be taken into account
suffixes := amd64 rt-amd64 intel-lts-acrn-sos

#####
# helper files to avoid excessive apt-cache serach operations
#####

###
# get list of all lkernel versions to be supported by looking for
# linux-headers packages
debian/genhelper/kversions:
	@mkdir -p $(@D)
	@echo "kversions = $(call apt-package,linux-headers,| \
		grep -E 'linux-headers-[0-9]+\.[0-9]+\.[0-9]+-' | \
		sed -e 's/linux-headers-\([0-9\.]*-[^-]*\)-.*/\1/g' | \
		sort -u)" > $@
include debian/genhelper/kversions

###
# all possible permutations of versions and suffixes
debian/genhelper/kvariants_all: debian/genhelper/kversions
	@mkdir -p $(@D)
	$(eval $(shell cat $<))
	@echo "kvariants_all = $(foreach s,$(suffixes),$(addsuffix -$s,$(kversions)))" > $@
include debian/genhelper/kvariants_all

###
# find all available header packages
debian/genhelper/linux-headers: debian/genhelper/kvariants_all
	@mkdir -p $(@D)
	$(eval $(shell cat $<))
	@echo "linux-headers = $(strip $(foreach v,$(kvariants_all),$(call apt-package,$(addprefix linux-headers-,$v))))" > $@
include debian/genhelper/linux-headers

###
# derive available variants from header packages
debian/genhelper/kvariants: debian/genhelper/linux-headers
	@mkdir -p $(@D)
	$(eval $(shell cat $<))
	@echo "kvariants = $(patsubst linux-headers-%,%,$(linux-headers))" > $@
include debian/genhelper/kvariants

###
# list of all required install files
debian/genhelper/install-files: debian/genhelper/kvariants
	@mkdir -p $(@D)
	$(eval $(shell cat $<))
	@echo "install-files = $(addsuffix .install,$(addprefix debian/nerve-de01-kmod-,$(kvariants)))" > $@
include debian/genhelper/install-files

###
# list of all required lintian-overrides files
debian/genhelper/lintian-overrides-files: debian/genhelper/kvariants
	@mkdir -p $(@D)
	$(eval $(shell cat $<))
	@echo "lintian-overrides-files = $(addsuffix .lintian-overrides,$(addprefix debian/nerve-de01-kmod-,$(kvariants)))" > $@
include debian/genhelper/lintian-overrides-files

#####
# End of helper files to avoid excessive apt-cache serach operations
#####

# create install file
debian/nerve-de01-kmod-%.install: debian/nerve-de01-kmod.install.in
	@sed -e 's/@kversion@/$(patsubst %.install,%,$(patsubst debian/nerve-de01-kmod-%,%,$@))/g' $< >$@

debian/nerve-de01-kmod-%.lintian-overrides:
	@echo "package-has-long-file-name" >$@

# create an appropriate control file
debian/control: debian/control.in debian/package.in debian/genhelper/kvariants
	@echo "Generating nerve-de01-kmod driver packages for the following kernel versions:"
	@sed -e 's/@linux-headers@/$(subst $(space),$(comma),$(linux-headers))/g' \
		debian/control.in >$@.tmp
	@$(foreach v,$(kvariants), \
		sed -e 's/@package-version@/$v/g' \
		    -e 's/@linux-image-version@/$(call apt-package,linux-image-$v,| grep -v linux-image-$v-dbg | sort | head -1)/g' \
		    debian/package.in >> $@.tmp; \
		    echo "  $v"; \
	)
	@mv $@.tmp $@

genfiles: debian/control $(install-files) $(lintian-overrides-files)

.PHONY: genfiles

################################################################################
# End of control and install files generation
################################################################################

###
# enhance clean to remove generated files and helper files
clean:
	dh $@ --with-systemd
	@rm -f debian/control
	@rm -f $(install-files)
	@rm -f $(lintian-overrides-files)
	@rm -rf debian/genhelper

%:
	dh $@ --with-systemd

# external kernel modules cannot be built out-of-tree,
# so copy the source tree for each variant
# to decouple them from each other
override_dh_auto_build:
	$(foreach k,$(KERNELS), \
		cp -a $(CURDIR)/src $(CURDIR)/src-$k;	\
		dh_auto_build --sourcedirectory=$(CURDIR)/src-$k -- SRC_DIR=$(CURDIR)/src-$k KDIR=/lib/modules/$k/build; \
	)

override_dh_auto_install:
	$(foreach k,$(KERNELS),dh_auto_install --sourcedirectory=$(CURDIR)/src-$k -- SRC_DIR=$(CURDIR)/src-$k KDIR=/lib/modules/$k/build;)
