#!/usr/bin/make -f
#
# debian/rules for kernel-image-*-arm
#
# GNU copyright 1997 to 1999 by Joey Hess.
# Copyright (c) 1999-2002 Herbert Xu <herbert@debian.org>
# Copyright (c) 2002 Othmar Pasteka <othmar@debian.org>
# Copyright (c) 2003,2004 Vincent Sanders <vince@debian.org>

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

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

SHELL := sh -e -x

export PATCH_THE_KERNEL=YES
export CC=gcc-2.95

CROSS_COMPILE := arm-linux-

export version := $(shell \
	head -1 debian/changelog | \
	sed 's/^kernel-image-\(.*\)-arm.*$$/\1/' \
)
fullversion := $(shell \
	dpkg-parsechangelog | sed -n 's/^Version: //p; t e; b; :e; q' \
)
flavours := $(shell \
	find config -maxdepth 1 -type f -printf '%f\n' \
)

bpkg := kernel-build-$(version)

unpack: unpack-stamp
unpack-stamp:
	dh_testdir
	tar jxf /usr/src/kernel-source-$(version).tar.bz2
	mkdir kernel-source-$(version)/debian
	cp debian/changelog kernel-source-$(version)/debian
	cp debian/control kernel-source-$(version)/debian
	cp debian/copyright kernel-source-$(version)/debian
	> kernel-source-$(version)/debian/official
	install post-install kernel-source-$(version)/debian
	for i in $(flavours); do \
		cp -al kernel-source-$(version) build-$$i; \
		cp config/$$i build-$$i/.config; \
		cd build-$$i; \
	        if [ "$$i" = "netwinder" ]; then \
			cat ../Config.in_vga16fb.diff | patch -s -p1; \
		fi; \
		cd ..; \
	done
	cp config/netwinder kernel-source-$(version)/.config
	ln -s `command -v touch` bin/touch.orig

	touch unpack-stamp

#	cd kernel-source-$(version); cat ../Makefile.diff | patch -s -p0; cd ..
#		cd build-$$i; cat ../Makefile.diff | patch -s -p0 ; \
#		if [ "$$i" = "riscstation" -o "$$i" = "riscpc" -o \
#		     "$$i" = "shark" -o "$$i" = "lart" ]; then \
#			cat ../Makefile_arm.diff | patch -s -p1 ; \
#		fi; \

build: build-stamp
build-stamp: unpack-stamp
	dh_testdir
	PATH=$$PWD/bin:$$PATH;
	for i in $(flavours); do \
		echo "$$i"; \
		cd build-$$i; \
		make-kpkg --subarch $$i --arch arm --added-patches arm-2_4_27 --append_to_version -$$i build; \
		cd ..; \
	done

	touch build-stamp

clean:
	dh_testdir
	rm -f unpack-stamp build-stamp
	rm -rf kernel-source-$(version) build-* install-*
	rm -f bin/touch.orig
	dh_clean

install: build
	dh_testdir
	dh_clean -k
	dh_installdirs

# Build architecture-independent files here.
binary-indep:

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir

	cd kernel-source-$(version); \
		make-kpkg --arch arm --added-patches arm-2_4_27 kernel-headers
	mv kernel-source-$(version)/debian/files debian

	for i in $(flavours); do \
		cp -al build-$$i install-$$i; \
		cd install-$$i; \
		make-kpkg --subarch $$i --arch arm --added-patches arm-2_4_27 --append_to_version -$$i kernel-image; \
		cd ..; \
		cat install-$$i/debian/files >> debian/files; \
		udeb_list=`dh_listpackages | grep -- $$i-udeb\$$ || true`; \
		if [ "${udeb_list}" != "" ];then \
			./scripts/copy-modules $$i-udeb $(version) build-$$i; \
			install -D -m 644 build-$$i/vmlinux ./debian/kernel-image-$(version)-$$i-udeb/boot/vmlinux; \
			dh_fixperms; \
			for upkg in $$udeb_list; do \
				mkdir -p debian/$$upkg/DEBIAN; \
				dpkg-gencontrol -p$$upkg -Pdebian/$$upkg -isp \
					-n$${upkg}_$(version)_arm.udeb; \
				dh_builddeb --package=$$upkg \
					--filename=$${upkg}_$(version)_arm.udeb; \
			done; \
		fi; \
		rm -rf install-$$i; \
	done

	mv *.deb ..

	dh_installdocs --package=$(bpkg)
	dh_installchangelogs --package=$(bpkg)
	dh_compress --package=$(bpkg)
	dh_fixperms --package=$(bpkg)
	dh_installdeb --package=$(bpkg)
	dh_gencontrol --package=$(bpkg)
	dh_md5sums --package=$(bpkg)
	dh_builddeb --package=$(bpkg)

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install unpack
