#!/usr/bin/make -f

#export DH_VERBOSE=1

MODULE		 = someproject
MODSRCPKG	 = $(MODULE)-source

# The following variable is required by module-assistant:
#   PACKAGE: modules binary package prefix (as in debian/control.modules.in)
PACKAGE		 = $(MODULE)-modules

MA_DIR		?= /usr/share/modass
include $(MA_DIR)/include/generic.make
include $(MA_DIR)/include/common-rules.make

# The following variables are defined by module-assistant:
#   KVERS:   kernel version string (version-abi-flavor-arch)
#   KSRC:    path to the kernel headers/sources (/lib/modules/$(KVERS)/build)
#   PKGNAME: package name to be built ($(PACKAGE)-$(KVERS))
#   VERSION: package version to be built (module-source-debversion+kernel-debversion)
#   DEB_DESTDIR: destination of the to-be-built package (/usr/src)

get_built_using	?= $(subst (= ),,$(shell dpkg-query -f='$${source:Package} (= $${source:Version})' -W $1))


kdist_config: prep-deb-files

binary-modules: kdist_config
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
# Build and install the module.
	CUSTOMIZE ME!
	# Kbuild build mode:
	$(MAKE) -C $(KSRC) M=$(CURDIR) modules
	# Kbuild build+install mode:
	$(MAKE) -C $(KSRC) M=$(CURDIR) INSTALL_MOD_PATH=$(CURDIR)/debian/$(PKGNAME)/ INSTALL_MOD_DIR=extra modules modules_install
	# Makefile mode:
	$(MAKE) KVERS=$(KVERS) LINUXSRC=$(KSRC) MODPREFIX=$(CURDIR)/debian/$(PKGNAME)/ installmod
# Build the package.
	dh_install
	dh_installdocs
	dh_installchangelogs
	dh_installmodules
	dh_bugfiles
	dh_lintian
	dh_link
	dh_strip_nondeterminism
	dh_compress
	dh_fixperms
	dh_missing
	dh_dwz
	dh_strip
	dh_installdeb
	dh_gencontrol -- -v$(VERSION) \
		-V'built-using:kernel=$(call get_built_using,linux-headers-$(KVERS))' \
		-V'built-using:module=$(call get_built_using,$(MODSRCPKG))'
	dh_md5sums
	dh_builddeb --destdir=$(DEB_DESTDIR)

kdist_clean: prep-deb-files
	dh_testdir
	CUSTOMIZE ME!
	# Kbuild mode:
	$(MAKE) -C $(KSRC) M=$(CURDIR) clean
	# Makefile mode:
	$(MAKE) KVERS=$(KVERS) LINUXSRC=$(KSRC) clean
	dh_clean $(patsubst %.modules.in,%,$(wildcard debian/*.modules.in))

.PHONY: kdist_config binary-modules kdist_clean
