#!/usr/bin/make -f
SHELL+= -e

BUILD_UDEB := 1



D   := $(CURDIR)/debian/kmod
DK  := $(CURDIR)/debian/libkmod2
DD  := $(CURDIR)/debian/libkmod-dev
DKU := $(CURDIR)/debian/libkmod2-udeb

CONFFLAGS = --sysconfdir=/etc --bindir=/bin
CONFFLAGS_deb  = $(CONFFLAGS) \
  $(shell DEB_BUILD_MAINT_OPTIONS="hardening=+pie" \
    dpkg-buildflags --export=configure || true) \
  --libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
  --with-rootlibdir=/lib/$(DEB_HOST_MULTIARCH) \
  --enable-debug
CONFFLAGS_udeb = $(CONFFLAGS) \
  $(subst -O2,-Os -fomit-frame-pointer,$(shell DEB_BUILD_MAINT_OPTIONS="hardening=-all" \
    dpkg-buildflags --export=configure || true)) \
  --libdir=/lib --with-rootlibdir=/lib

##############################################################################
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
  CONFFLAGS += --build $(DEB_HOST_GNU_TYPE)
else
  CONFFLAGS += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

ifdef BUILD_UDEB
  BUILD_UDEB_TARGET := .stamp-build-udeb
else
  NO_PACKAGE := --no-package=libkmod2-udeb
endif

KMOD_RECONFIGURE = yes

##############################################################################
clean:
	rm -rf .stamp-* build-*deb
ifdef KMOD_RECONFIGURE
	rm -f aclocal.m4 autom4te.cache/* config.h.in config.h.in~ configure \
		Makefile.in $$(find m4/ -type l)
endif
	dh_clean

# if enabled, must build-depend on autoconf, automake, libtool
ifdef KMOD_RECONFIGURE
configure: configure.ac
	gtkdocize --docdir libkmod/docs
	autoreconf --force --install --symlink
endif

build-deb/config.status: configure
	[ -d build-deb ] || mkdir build-deb
	cd build-deb/ && \
	../configure $(CONFFLAGS_deb)

build-udeb/config.status: configure
	[ -d build-udeb ] || mkdir build-udeb
	cd build-udeb/ && \
	../configure $(CONFFLAGS_udeb)

build: build-arch build-indep
build-arch: .stamp-build $(BUILD_UDEB_TARGET)
build-indep: .stamp-build $(BUILD_UDEB_TARGET)
.stamp-build: build-deb/config.status
	dh_testdir
	cd build-deb/ && \
	$(MAKE) all V=1
	touch $@

build-udeb: .stamp-build-udeb
.stamp-build-udeb: build-udeb/config.status
	dh_testdir
	cd build-udeb/ && \
	$(MAKE) all V=1
	touch $@

binary-all: .stamp-binary
.stamp-binary: build checkroot
	dh_testdir
	dh_prep

	cd build-deb/ && \
	$(MAKE) install DESTDIR=$D
	rm $D/usr/lib/*/*.la

	dh_movefiles --sourcedir=debian/kmod
	rmdir --parents --ignore-fail-on-non-empty $$(find $D -type d -empty)

	dh_installdirs
	install --mode=755 debian/kmod.initramfs-hook \
		$D/usr/share/initramfs-tools/hooks/kmod

	install --mode=755 -d $D/etc/modprobe.d
	install --mode=644 -t $D/etc/modprobe.d debian/modprobe.d/*
	install --mode=755 -d $D/etc/depmod.d
	install --mode=644 -t $D/etc/depmod.d debian/depmod.d/*

ifdef BUILD_UDEB
	cd build-udeb/ && \
	$(MAKE) install-exec DESTDIR=$(DKU)
	install --mode=755 -d $(DKU)/etc/modprobe.d
	install --mode=644 -t $(DKU)/etc/modprobe.d debian/modprobe.d/mlx4.conf
	rm -r $(DKU)/lib/
endif

	#mv $D/usr/share/doc/kmod $D/usr/share/doc/libkmod2
	dh_installdocs -pkmod -plibkmod-dev -pmodule-init-tools \
		--link-doc=libkmod2
	dh_installdocs -pkmod --link-doc=libkmod2 README TODO
	dh_installdocs -plibkmod2
	cp libkmod/README $(DD)/usr/share/doc/libkmod2/README.libkmod
	dh_installchangelogs NEWS
	dh_installman extra/*.5
	dh_installinit --no-start -- start 15 S .
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs --add-udeb=libkmod2-udeb -- -c4
	dh_shlibdeps -- --warnings=7
	dh_installdeb
	dh_gencontrol $(NO_PACKAGE)
	dh_md5sums
	touch $@

binary-arch: .stamp-binary
	dh_builddeb -a $(NO_PACKAGE)

binary-indep: .stamp-binary
	dh_builddeb -i

binary:	binary-arch binary-indep

checkroot:
	test root = "`whoami`"

get-orig-source:
	cd $(dir $(firstword $(MAKEFILE_LIST)))../ && \
	uscan --rename --force-download --watchfile debian/watch --destdir $(CURDIR)

.PHONY: clean build build-arch build-indep binary binary-arch binary-indep checkroot
