#!/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 extraversion that we are building for
VERSION	= 2.4.26
EXTRA	= powerpc powerpc-small powerpc-smp

# The location of the patch file and build tarball
PATCH	= /usr/src/kernel-patches/powerpc/debian-powerpc.diff.gz

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

CFLAGS = -Wall -g

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

configure: configure-stamp
configure-stamp:
	dh_testdir

#	unpack the modules source
	tar zxf /usr/src/mol-modules.tar.gz

#	unpack and patch the kernel source
	tar jxf /usr/src/kernel-source-$(VERSION).tar.bz2
	gunzip -c $(PATCH) | patch -d kernel-source-$(VERSION) -p1 -l -f

#	create separate build trees using hard links and add the build files
	for extra in $(EXTRA); do \
	cp -pal $(MODDIR) $(MODDIR)-$$extra; \
	cp -pal kernel-source-$(VERSION) kernel-build-$(VERSION)-$$extra; \
	tar jxf /usr/src/kernel-build-$(VERSION)-$$extra.tar.bz2; \
	done

	touch configure-stamp

build: configure-stamp  build-stamp
build-stamp:
	dh_testdir

#	depend the kernel source
	for extra in $(EXTRA); do \
	$(MAKE) -C kernel-build-$(VERSION)-$$extra EXTRAVERSION=-$$extra oldconfig depend; \
	done

#	build the modules
	for extra in $(EXTRA); do \
	$(MAKE) -C $(MODDIR)-$$extra KSRC=`pwd`/kernel-build-$(VERSION)-$$extra; \
	done

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	rm -rf kernel-source-$(VERSION) kernel-build-$(VERSION)*
	rm -rf modules

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k

#	install the modules
	for extra in $(EXTRA); do \
	$(MAKE) -C $(MODDIR)-$$extra KVERS=$(VERSION)-$$extra KSRC=$(CURDIR)/kernel-build-$(VERSION)-$$extra DESTDIR=$(CURDIR)/debian/mol-modules-$(VERSION)-$$extra install; \
	done

# Build architecture-dependent files here.
binary: build install
	for extra in $(EXTRA); do \
	$(MAKE) -f debian/rules binary-common DH_OPTIONS="-pmol-modules-$(VERSION)-$$extra -P$(CURDIR)/debian/mol-modules-$(VERSION)-$$extra"; \
	done

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

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