#!/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-rsq10-kmod-%,%,$(shell [ -f debian/control ] && dh_listpackages | grep nerve-rsq10-kmod-))

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

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

# eventually predefined by buildsystem as comma separated list
kvariants ?= $(subst $(comma),$(space),${GBP_KERNELVERSIONS})

# kvariants: list of kernel variants (version+feat+arch) must be set at make call
linux-headers = $(addprefix linux-headers-,$(kvariants))
install-files = $(addsuffix .install,$(addprefix debian/nerve-rsq10-kmod-,$(kvariants)))
lintian-overrides-files = $(addsuffix .lintian-overrides,$(addprefix debian/nerve-rsq10-kmod-,$(kvariants)))

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

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

# create an appropriate control file
debian/control: debian/control.in debian/package.in
	@echo "Generating nerve-rsq10-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@/$$(apt-cache search --names-only $(addprefix linux-image-,$v) | cut -d' ' -f1 | sort | head -1)/g" \
		    debian/package.in >> $@.tmp; \
		    echo "  $v"; \
	)
	@mv $@.tmp $@

genfiles: debian/control $(install-files) $(lintian-overrides-files)
genfiles-cleanup:
	@rm -f debian/control
	@rm -f $(install-files)
	@rm -f $(lintian-overrides-files)


.PHONY: genfiles genfiles-cleanup

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

%:
	dh $@

# 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), \
		mkdir -p $(CURDIR)/$k; \
		cp -a $(CURDIR)/platform $(CURDIR)/$k/platform;	\
		cp -a $(CURDIR)/leds $(CURDIR)/$k/leds; \
		dh_auto_build --sourcedirectory=$(CURDIR)/$k/platform -- \
			SRC_DIR=$(CURDIR)/$k/platform \
			KDIR=/lib/modules/$k/build; \
		dh_auto_build --sourcedirectory=$(CURDIR)/$k/leds -- \
			SRC_DIR=$(CURDIR)/$k/leds \
			KDIR=/lib/modules/$k/build; \
	)

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