#!/usr/bin/make -f
#
# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1
include /usr/share/dpkg/pkg-info.mk

VERSION=$(DEB_VERSION_UPSTREAM)

# Packages provided for client builds
DEV_PKG=lustre-dev
UTILS_PKG=lustre-client-utils
DKMS_PKG=lustre-client-modules-dkms

autogen: autogen-stamp
autogen-stamp:
	touch $@

configure: configure-stamp
configure-stamp: autogen-stamp
	dh_testdir
	# touch files to same date, to avoid auto*
	find . -type f -print0 | xargs -0 touch -r COPYING; \
	./configure \
            --disable-server \
            --disable-dependency-tracking \
            --disable-tests \
            --disable-iokit \
            --disable-crypto \
            --disable-doc \
            --disable-quilt \
            --enable-quota \
            --with-kmp-moddir=updates/kernel \
            --with-o2ib=yes; \
	touch $@

build-arch build-indep: build

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	$(MAKE) -j $(shell nproc)
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	touch $@

binary-arch: binary-$(UTILS_PKG) binary-$(DEV_PKG)

binary-indep: binary-$(DKMS_PKG)

binary: binary-indep binary-arch

binary-$(UTILS_PKG): build-stamp
	dh_testdir
	dh_testroot
	dh_installdirs -p $(UTILS_PKG)
	dh_installdocs -p  $(UTILS_PKG)
	dh_installman -p $(UTILS_PKG)
	dh_install -p $(UTILS_PKG)
	dh_makeshlibs -p $(UTILS_PKG)
	dh_installexamples -p $(UTILS_PKG)
	dh_installchangelogs -p $(UTILS_PKG) lustre/ChangeLog
	dh_compress -p $(UTILS_PKG)
	dh_strip -p $(UTILS_PKG)
	dh_installdeb -p $(UTILS_PKG)
	dh_fixperms -p $(UTILS_PKG)
	dh_gencontrol -p $(UTILS_PKG)
	dh_md5sums -p $(UTILS_PKG)
	dh_builddeb -p $(UTILS_PKG)

binary-$(DEV_PKG): build-stamp
	dh_testdir
	dh_testroot
	dh_installdirs -p $(DEV_PKG)
	dh_install -p $(DEV_PKG)
	dh_installdocs -p $(DEV_PKG)
	dh_installchangelogs -p $(DEV_PKG) lustre/ChangeLog
	dh_compress -p $(DEV_PKG)
	dh_installdeb -p $(DEV_PKG)
	dh_fixperms -p $(DEV_PKG)
	dh_gencontrol -p $(DEV_PKG)
	dh_md5sums -p $(DEV_PKG)
	dh_builddeb -p $(DEV_PKG)

binary-$(DKMS_PKG): build-stamp
	dh_testdir
	dh_testroot
	dh_installdirs -p $(DKMS_PKG) usr/src/lustre-client-modules-$(VERSION)
	dh_dkms -p $(DKMS_PKG) -V $(VERSION) 
	# Copy the source
	rsync -a --exclude='debian/$(DKMS_PKG)/*' \
                  --exclude='.git/' \
                  . \
		  debian/$(DKMS_PKG)/usr/src/lustre-client-modules-$(VERSION)
	# Clean it up
	$(MAKE) -C debian/$(DKMS_PKG)/usr/src/lustre-client-modules-$(VERSION) -f debian/rules clean

	# Put the DKMS configuration file in place
	dh_install -p $(DKMS_PKG)
	dh_installchangelogs -p $(DKMS_PKG) lustre/ChangeLog
	dh_installdocs -p $(DKMS_PKG)
	dh_compress -p $(DKMS_PKG)
	dh_installdeb -p $(DKMS_PKG)
	dh_strip -p $(DKMS_PKG)
	dh_fixperms -p $(DKMS_PKG)
	dh_gencontrol -p $(DKMS_PKG)
	dh_md5sums -p $(DKMS_PKG)
	dh_builddeb -p $(DKMS_PKG)

clean:
	dh_testdir
	dh_testroot
	-$(MAKE) distclean
	dh_clean

.PHONY: clean binary binary-indep binary-arch install configure
