#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#

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

# The kernel version and flavours that we are building for
VERSION	= 2.6.5
FLAVOUR	= powerpc powerpc-smp

# the build directory for the modules
MODDIR	= modules/mol

# how to call ourselves recursively
RECURSION = $(MAKE) -f debian/rules

# kernel version, kernel build tree, modules install directory
export KVERS=$(VERSION)-$(flavour)
export KSRC=$(CURDIR)/kernel-build-$(VERSION)-$(flavour)
export DESTDIR=$(CURDIR)/debian/mol-modules-$(VERSION)-$(flavour)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

configure: stamp-configure
stamp-configure: \
stamp-configure-prepare \
$(foreach flavour,$(FLAVOUR),stamp-configure-flavour-$(flavour))
	touch $@

# unpack the modules source
stamp-configure-prepare:
	dh_testdir
	tar zxf /usr/src/mol-modules.tar.gz
	touch $@

ifeq (0,$(MAKELEVEL))

stamp-configure-flavour-%:
	$(RECURSION) stamp-configure-flavour-$* flavour=$*

else
ifeq (1,$(MAKELEVEL))

# create separate module and kernel build trees for a given flavour.
# The module build tree is simply copied using hard links.  The kernel
# build tree is first copied from the header files, then, the other
# build files are added and finally, the kernel build tree is touched
# back to the dawn of time to prevent unnecessary rebuilds.
stamp-configure-flavour-$(flavour): stamp-configure-prepare
	cp -al $(MODDIR) $(MODDIR)-$(flavour)
	cp -a /usr/src/kernel-headers-$(VERSION) $(KSRC)
	cd /usr/src/kernel-build-$(VERSION)-$(flavour); \
	find . | cpio -dup $(KSRC)
	find $(KSRC) -exec touch -d "January 24 1984" {} \;

	touch $@

endif
endif

build: stamp-configure stamp-build
stamp-build: \
stamp-build-prepare \
$(foreach flavour,$(FLAVOUR),stamp-build-flavour-$(flavour))
	touch $@

stamp-build-prepare:
	dh_testdir

ifeq (0,$(MAKELEVEL))

stamp-build-flavour-%:
	$(RECURSION) stamp-build-flavour-$* flavour=$*

else
ifeq (1,$(MAKELEVEL))

# build the modules for a given flavour
stamp-build-flavour-$(flavour):
	$(MAKE) -C $(MODDIR)-$(flavour)
	touch $@

endif
endif

install: stamp-configure stamp-build stamp-install
stamp-install: \
stamp-install-prepare \
$(foreach flavour,$(FLAVOUR),stamp-install-flavour-$(flavour))
	touch $@

stamp-install-prepare:
	dh_testdir

ifeq (0,$(MAKELEVEL))

stamp-install-flavour-%:
	$(RECURSION) stamp-install-flavour-$* flavour=$*

else
ifeq (1,$(MAKELEVEL))

# install the modules for a given flavour
stamp-install-flavour-$(flavour):
	$(MAKE) -C $(MODDIR)-$(flavour) install
	touch $@

endif
endif

# Build architecture-dependent files here.
binary: stamp-configure stamp-build stamp-install \
$(foreach flavour,$(FLAVOUR),binary-flavour-$(flavour))

binary-flavour-%:
	$(RECURSION) binary-common DH_OPTIONS="-pmol-modules-$(VERSION)-$* -P$(CURDIR)/debian/mol-modules-$(VERSION)-$*"

binary-common:
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installchangelogs
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

clean:
	dh_testdir
	dh_testroot

	rm -rf kernel-build-$(VERSION)-*
	rm -rf modules
	rm -f stamp-*

	dh_clean

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