#!/usr/bin/make -f
#
# Created 2009 by Brian J. Murrell based on work by
# Alastair McKinstry, <alastair.mckinstry@ichec.ie>, <mckinstry@debian.org>
# Patrick Winnertz, and the pkg-lustre Debian maintainers
#
# Copying and modification is unlimited, provided that the modified
# version is marked as being modified.

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

DEB_BUILD_GNU_SYSTEM = $(shell dpkg-architecture -qDEB_BUILD_GNU_SYSTEM)
DEB_BUILD_GNU_CPU = $(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)

# whether to use module-assistant to build the kernel modules or not
USE_MA = true

CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
        CFLAGS += -O0
else
        CFLAGS += -O2
endif

# We need the kernels to be built with gcc-3.3; suspect that gcc-4.1 uses too
# much stack and crashes because of it.
#CC?=gcc-3.3
#export CC

# This is the debhelper compatability version to use.
export DH_COMPAT=5

# Module-assistant stuff
PACKAGE=lustre-client-modules
MA_DIR ?= /usr/share/modass
-include $(MA_DIR)/include/generic.make
-include $(MA_DIR)/include/common-rules.make

ARCH=$(shell dpkg --print-architecture)
TOP_DIR:=$(shell pwd)

#  the lustre build process. This is $(KERNEL_VERSION)
VERSION=$(shell sed -n -e '1s/.*(\(.*\)) .*/\1/p' debian/changelog)
#SUPPORTED_KERNELS=2.6.27
SUPPORTED_KERNELS=2.6.18 2.6.20 2.6.22
UPVERSION=$(shell echo $VERSION | sed -e 's/-[^-]*$$//')
#KERNEL_VERSION=2.6.27
# the kernel version to build all of this with
#KVER=2.6.27-11-generic
# if the above is not set, default to whatever kernel is booted
KVER?=$$(uname -r)
KERNEL_TREE=/lib/modules/$(KVER)/build
# KVERS, KSRC are the kernel version and source to use when building lustre
# Will be the default when building lustre-utils, etc but whatever module-assistant
# supplied when building modules
KVERS?=$(KERNEL_VERSION)
#KSRC?=$(KERNEL_TREE)
KSRC=$(KERNEL_TREE)

# Packages
PATCH_PKG=linux-patch-lustre
UTILS_PKG=lustre-utils
TESTS_PKG=lustre-tests
DEV_PKG=lustre-dev
LIB_PKG=liblustre
SOURCE_PKG=lustre-source
MODS_PKG=lustre-client-modules

#Build dir
#BUILDDIR=debian/build
#SRCDIR=../..
# I'm afraid we seem to have broken the BUILDDIR concept in Lustre's
# autoconf macros
BUILDDIR=.
SRCDIR=.

autogen: autogen-stamp
autogen-stamp: patch-stamp
	# see if any patches requires us to run autogen
	if ! grep "^--- .*\/autoconf" debian/patches/* || \
	   [ ! -f autogen.sh ]; then \
		exit 0; \
	fi
	cp /usr/share/misc/config.sub config.sub
	cp /usr/share/misc/config.sub libsysio/config.sub
	cp /usr/share/misc/config.sub ldiskfs/config.sub
	cp /usr/share/misc/config.guess config.guess
	cp /usr/share/misc/config.guess libsysio/config.guess
	cp /usr/share/misc/config.guess ldiskfs/config.guess
	sh ./autogen.sh
	touch $@

configure: configure-stamp
configure-stamp: autogen-stamp debian/control.main debian/control.modules.in
	dh_testdir
	# touch files to same date, to avoid auto*
	find . -type f -print0 | xargs -0 touch -r COPYING; \
	if [ "$(BUILDDIR)" != "." ]; then \
		mkdir -p $(BUILDDIR)/build $(BUILDDIR)/lustre/contrib $(BUILDDIR)/libsysio; \
		cp build/Makefile $(BUILDDIR)/build/; \
		cp lustre/contrib/mpich-*.patch $(BUILDDIR)/lustre/contrib/; \
		ln -s ../../../libsysio/include $(BUILDDIR)/libsysio/; \
	fi
	( cd $(BUILDDIR) && \
	 $(SRCDIR)/configure --disable-dependency-tracking \
		   --disable-modules \
		   --disable-snmp \
		   --disable-client \
		   --enable-quota \
		   --disable-server )
	touch $@

build-arch build-indep: build

build: build-stamp
build-stamp: patch-stamp configure-stamp
	dh_testdir
	$(MAKE) -C $(BUILDDIR)
	$(MAKE) -C $(BUILDDIR) DESTDIR=$(TOP_DIR)/debian/tmp install
	# jump our lustre-client-modules into the control file if not using m-a
	if ! $(USE_MA); then \
		(cat debian/control.main; sed -e '1,/^$$/d' -e "s/_KVERS_/$(KVER)/g" < debian/control.modules.in) > debian/control; \
		for file in debian/*_KVERS_*; do \
			newfile=$$(echo $$file | sed -e "s/_KVERS_/$(KVER)/g"); \
			cp $$file $$newfile; \
		done; \
	fi
	touch $@

# let's try and operate as close as the user would and not build modules
# here (for now)
# instead we will build the modules with m-a in the "make debs" target
# binary-arch: binary-$(UTILS_PKG) binary-$(TESTS_PKG) binary-$(DEV_PKG) binary-$(LIB_PKG) binary-$(MODS_PKG) binary-kern-mods
binary-arch: binary-$(UTILS_PKG) binary-$(TESTS_PKG) binary-$(DEV_PKG) binary-$(LIB_PKG)

binary-indep: binary-$(PATCH_PKG) binary-$(SOURCE_PKG)

binary: binary-indep binary-arch

binary-$(PATCH_PKG): patch-stamp
	dh_testdir
	dh_testroot
	dh_installdirs -p $(PATCH_PKG)
	dh_installdocs -p $(PATCH_PKG)
#	dh_installkpatches -p $(PATCH_PKG)
	dh_install -p $(PATCH_PKG)

	# Install apply script
	cp $(TOP_DIR)/debian/linux-patch-lustre.apply $(TOP_DIR)/debian/$(PATCH_PKG)/usr/src/kernel-patches/lustre/scripts/apply
	chmod a+x $(TOP_DIR)/debian/$(PATCH_PKG)/usr/src/kernel-patches/lustre/scripts/apply
	set -e; for KERNEL in $(SUPPORTED_KERNELS); do \
	  mkdir -p $(TOP_DIR)/debian/$(PATCH_PKG)/usr/src/kernel-patches/all/$$KERNEL/apply/; \
	  ln -s ../../../lustre/scripts/apply $(TOP_DIR)/debian/$(PATCH_PKG)/usr/src/kernel-patches/all/$$KERNEL/apply/lustre; \
	done

	# Install unpatch script
	cp $(TOP_DIR)/debian/linux-patch-lustre.unpatch $(TOP_DIR)/debian/$(PATCH_PKG)/usr/src/kernel-patches/lustre/scripts/unpatch
	chmod a+x $(TOP_DIR)/debian/$(PATCH_PKG)/usr/src/kernel-patches/lustre/scripts/unpatch
	set -e; for KERNEL in $(SUPPORTED_KERNELS); do \
	  mkdir -p $(TOP_DIR)/debian/$(PATCH_PKG)/usr/src/kernel-patches/all/$$KERNEL/unpatch/; \
	  ln -s ../../../lustre/scripts/unpatch $(TOP_DIR)/debian/$(PATCH_PKG)/usr/src/kernel-patches/all/$$KERNEL/unpatch/lustre; \
	done

	# Install series
	cp lustre/kernel_patches/series/*.series $(TOP_DIR)/debian/$(PATCH_PKG)/usr/src/kernel-patches/lustre/series/

	# Install patches
	cp lustre/kernel_patches/patches/*.patch $(TOP_DIR)/debian/$(PATCH_PKG)/usr/src/kernel-patches/lustre/patches/

	dh_installchangelogs -p $(PATCH_PKG) lustre/ChangeLog
	dh_compress -p $(PATCH_PKG)
	dh_installdeb -p $(PATCH_PKG)
	dh_fixperms -p $(PATCH_PKG)
	dh_gencontrol -p $(PATCH_PKG)
	dh_md5sums -p $(PATCH_PKG)
	dh_builddeb -p $(PATCH_PKG)

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_shlibdeps -L $(LIB_PKG) -l debian/$(LIB_PKG)/usr/lib
	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-$(TESTS_PKG): build-stamp
	dh_testdir
	dh_testroot
	dh_installdirs -p $(TESTS_PKG)
	dh_installdocs -p  $(TESTS_PKG)
	dh_installman -p $(TESTS_PKG)
	dh_install -p $(TESTS_PKG)
	dh_makeshlibs -p $(TESTS_PKG)
	dh_shlibdeps -L $(LIB_PKG) -l debian/$(LIB_PKG)/usr/lib
	dh_installexamples -p $(TESTS_PKG)
	dh_installchangelogs -p $(TESTS_PKG) lustre/ChangeLog
	dh_compress -p $(TESTS_PKG)
	dh_strip -p $(TESTS_PKG)
	dh_installdeb -p $(TESTS_PKG)
	dh_fixperms -p $(TESTS_PKG)
	dh_gencontrol -p $(TESTS_PKG)
	dh_md5sums -p $(TESTS_PKG)
	dh_builddeb -p $(TESTS_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-$(LIB_PKG): build-stamp
	dh_testdir
	dh_testroot
	dh_installdirs -p $(LIB_PKG)
	dh_installdocs -p $(LIB_PKG)
	dh_install -p $(LIB_PKG)
	dh_makeshlibs -p $(LIB_PKG)
	dh_shlibdeps -L $(LIB_PKG) -l debian/$(LIB_PKG)/usr/lib
	dh_installchangelogs -p $(LIB_PKG) lustre/ChangeLog
	dh_compress -p $(LIB_PKG)
	dh_installdeb -p $(LIB_PKG)
	dh_strip -p $(LIB_PKG)
	dh_fixperms -p $(LIB_PKG)
	dh_gencontrol -p $(LIB_PKG)
	dh_md5sums -p $(LIB_PKG)
	dh_builddeb -p $(LIB_PKG)

binary-$(SOURCE_PKG): build-stamp
	dh_testdir 
	dh_testroot
	# Copy everything 
	dh_installdirs -p $(SOURCE_PKG) usr/src/modules/lustre/debian
	rsync -av --exclude='debian/$(SOURCE_PKG)/*' \
	      --link-dest=$(PWD) \
	      . debian/$(SOURCE_PKG)/usr/src/modules/lustre
	# Remove FULLCLEAN setting
	rm debian/$(SOURCE_PKG)/usr/src/modules/lustre/debian/rules
	sed -e 's/FULLCLEAN=full-clean/FULLCLEAN=/' < debian/rules > debian/$(SOURCE_PKG)/usr/src/modules/lustre/debian/rules
	# clean up dest
	rm -rf debian/$(SOURCE_PKG)/usr/src/modules/lustre/debian/patched \
	       debian/$(SOURCE_PKG)/usr/src/modules/lustre/debian/patches/* 
	$(MAKE) -C debian/$(SOURCE_PKG)/usr/src/modules/lustre -f debian/rules clean
	# Create the module-source tarball.
	cd debian/$(SOURCE_PKG)/usr/src && tar jcf lustre.tar.bz2 modules 
	rm -rf debian/$(SOURCE_PKG)/usr/src/modules
	dh_install -p$(SOURCE_PKG)
	dh_installchangelogs -p $(SOURCE_PKG) lustre/ChangeLog
	dh_installdocs -p $(SOURCE_PKG) 
	dh_link -p $(SOURCE_PKG) /usr/share/modass/packages/default.sh /usr/share/modass/overrides/lustre-source
	dh_compress -p $(SOURCE_PKG)
	dh_installdeb -p $(SOURCE_PKG)
	dh_fixperms -p $(SOURCE_PKG)
	dh_gencontrol -p $(SOURCE_PKG)
	dh_md5sums -p $(SOURCE_PKG)
	dh_builddeb -p $(SOURCE_PKG)

# if only we could use m-a for this, but this stupid "compliant.list"
# thing fouls that up
binary-kern-mods:
	mkdir -p debian/m-a_root/usr_src/modules
	ln -s ../../../../ debian/m-a_root/usr_src/modules/lustre
	m-a -t -u debian/m-a_root/ -d -v -k $(KSRC) build lustre
	mv -f debian/m-a_root/*.deb ..

# an alternative (to module-assistant) method of building the kernel modules
binary-$(MODS_PKG): build-stamp
	if ! $(USE_MA); then \
		cp debian/lustre-client-modules.install debian/lustre-client-modules-$(KVER).install ; \
		dh_testdir; \
		dh_testroot; \
		dh_installdirs -p $(MODS_PKG)-$(KVER); \
		dh_installdocs -p $(MODS_PKG)-$(KVER); \
		dh_install -p $(MODS_PKG)-$(KVER); \
		dh_makeshlibs -p $(MODS_PKG)-$(KVER); \
		dh_shlibdeps -L $(LIB_PKG) -l debian/$(LIB_PKG)/usr/lib; \
		dh_installchangelogs -p $(MODS_PKG)-$(KVER) lustre/ChangeLog; \
		dh_compress -p $(MODS_PKG)-$(KVER); \
		dh_installdeb -p $(MODS_PKG)-$(KVER); \
		dh_strip -p $(MODS_PKG)-$(KVER); \
		dh_fixperms -p $(MODS_PKG)-$(KVER); \
		dh_gencontrol -p $(MODS_PKG)-$(KVER); \
		dh_md5sums -p $(MODS_PKG)-$(KVER); \
		dh_builddeb -p $(MODS_PKG)-$(KVER); \
	fi

###
### For module-assistant
###

# the kdist_clean target is called by make-kpkg modules_clean and from
# kdist* rules. It is responsible for cleaning up any changes that have
# been made by the other kdist_commands (except for the .deb files created)

kdist_clean: 
	# can't dh_clean here or we remove the build artifacts for the other packages
	# still in progress
	#dh_clean
	#-$(MAKE) $(MFLAGS) -f debian/rules clean
	# put the userspace tools' control file back
	cp debian/control.main debian/control


# module assistant calculates all needed things for us and sets
# # following variables:
# # KSRC (kernel source directory), KVERS (kernel version string), KDREV
# # (revision of the Debian kernel-image package), CC (the correct
# # compiler), VERSION (the final package version string), PKGNAME (full
# # package name with KVERS included), DEB_DESTDIR (path to store DEBs)
#
# # The kdist_configure target is called by make-kpkg modules_config and
# # by kdist* rules by dependency. It should configure the module so it is
# # ready for compilation (mostly useful for calling configure).
# # prep-deb-files from module-assistant creates the neccessary debian/ 
kdist_config: prep-deb-files patch-stamp
	#  Check build dependencies again, needs to be done since we just
	# replaced the control file.
	dpkg-checkbuilddeps
	# Prepare the kernel to lustres' liking
	# as non-root, we won't have permission for this
	#-$(MAKE) -C $(KSRC) prepare scripts
	# touch files to same date, to avoid auto*
	find . -type f -print0 | xargs -0 touch -r COPYING \;
	# Doesn't seem possible to only build modules...
	./configure --with-linux=$(KSRC) \
		    --disable-quilt  \
		    --disable-dependency-tracking \
		    --disable-doc  \
		    --disable-utils \
		    --disable-liblustre \
		    --disable-snmp \
		    --enable-ldiskfs \
		    --disable-zerocopy \
		    --disable-tests \
		    --enable-quota

kdist_configure: kdist_config 

# the binary-modules rule is invoked by module-assistant while processing the
# kdist* targets. It is called by module-assistant or make-kpkg and *not*
# during a normal build
binary-modules: prep-deb-files
	dh_testroot
	dh_clean -k
	$(MAKE)
	$(MAKE) install DESTDIR=$(CURDIR)/debian/$(PKGNAME)
	# Remove stuff that doesn't belong (no module-only target)
	cd $(CURDIR)/debian/$(PKGNAME) && rm -rf usr sbin etc
	dh_installdocs -p$(PKGNAME)
	dh_installchangelogs -p$(PKGNAME)
	dh_installmodules -p $(PKGNAME)
	dh_compress -p $(PKGNAME)
	dh_fixperms -p $(PKGNAME)
	dh_installdeb -p $(PKGNAME)
	dh_gencontrol -- -v$(VERSION)
	dh_md5sums -p $(PKGNAME)
	dh_builddeb --destdir=$(DEB_DESTDIR) -p $(PKGNAME)
	dh_clean -k -p $(PKGNAME)

clean:
	dh_testdir
	dh_testroot
	dpatch deapply-all
	-$(MAKE) distclean
	rm -rf  debian/substvars debian/*.bak debian/*~ *~  *-stamp debian/$(PATCH_PKG)
	ls -d debian/lustre-client-modules-* | grep -v _KVERS_ | xargs rm -f || true
	# only remove this if the clean was not called from kdist_clean
	if [ "$$MA_SOURCE_PKG" = "" ]; then \
		rm -rf  debian/m-a_root; \
	fi
	dh_clean
	# make sure we have at least a basic control in place (see build-stamp)
	rm -f debian/control
	cp debian/control.main debian/control

patch: patch-stamp
patch-stamp:
	dpatch apply-all
	touch $@

unpatch:
	dpatch deapply-all
	rm -rf debian/patched patch-stamp

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