#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

unexport CFLAGS
unexport LDFLAGS

# This is the debhelper compatability version to use.
export DH_COMPAT=10
export LC_ALL=C
export SHELL=/bin/bash -e

include debian/parallel.mk

stub=lkrg
flavours	= generic hardened

release := 4.15.3-2

builddir	:= $(CURDIR)/debian/build

build-arch: $(addprefix build-modules-,$(flavours))
binary: $(addprefix binary-modules-,$(flavours))

build-modules-%:
	@echo "Preparing $*..."
	install -d $(builddir)/build-$*
	@echo "Building $*..."
	make -C $(CURDIR)/lkrg $(conc_level) P_KVER=$(release)-$*
	strip --strip-debug lkrg/p_lkrg.ko
	cp lkrg/p_lkrg.ko $(builddir)/build-$*
	md5sum $(builddir)/build-$*/p_lkrg.ko
	make -C $(CURDIR)/lkrg clean P_KVER=$(release)-$*
	@touch $@

# Install the finished build
install-%: build-modules-%
	dh_testdir
	dh_testroot
	dh_prep -p$(stub)-$*

	md5sum $(builddir)/build-$*/p_lkrg.ko

	for script in postinst postrm install; do \
		sed -e 's/@@KVER@@/$(release)-$*/g' \
			debian/control-scripts/$(stub).$$script > \
			debian/$(stub)-$*.$$script;	\
	done;

	dh_install --sourcedir=$(builddir)/build-$* -p$(pkgimg)
	dh_installdeb -p$(pkgimg)

binary-modules-%: pkgimg = $(stub)-$*
binary-modules-%: install-%
	dh_testdir
	dh_testroot

	dh_installchangelogs -p$(pkgimg)
	dh_installdocs -p$(pkgimg)
	dh_compress -p$(pkgimg)
	dh_fixperms -p$(pkgimg)
	dh_installdeb -p$(pkgimg)
	dh_gencontrol -p$(pkgimg)
	dh_md5sums -p$(pkgimg)
	dh_builddeb -p$(pkgimg)

# Debian Build System targets
build: build-arch

clean-scripts-%:
	for script in postinst postrm install; do \
		rm -f debian/$(stub)-$*.$$script;	\
	done;

clean: $(addprefix clean-scripts-,$(flavours))
	dh_testdir
	dh_testroot
	dh_clean

	rm -rf $(builddir)
