#!/usr/bin/make -f
#
# debian/rules for kernel-image-*-ia64
#
# Copyright (c) 2001,2002 Bdale Garbee <bdale@gag.com>
#

# 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

export version := $(shell \
	head -1 debian/changelog | \
	sed 's/^kernel-image-\(.*\)-ia64.*$$/\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' \
)

unpack: stamp-unpack
stamp-unpack:
	# unpack Linus' source tree
	tar jxf linux-$(version).tar.bz2

	# apply Mosberger patch
	(cd linux ; bunzip2 < ../linux-2.4.17-ia64-011226.diff.bz2 \
		| patch -p1 )

	# apply patch from HP to add support for systems using ZX1 chipset
	# includes some additional / replacement I/O device drivers
	(cd linux ; bunzip2 < ../hplso-CVS-2002-03-27.diff.bz2 \
		| patch -p1 )

	# patch for ia32 subsystem missed in big diff set
	(cd linux ; patch -p1 < ../patches/patch.ia32 )

	# patch to add FireWire drivers to set that are buildable
	(cd linux ; patch -p1 < ../patches/patch.firewire )

	# DVD+RW enablement patch for dvd100i and kin - prone to I/O errors!
	(cd linux ; patch -p0 < ../patches/patch.dvd+rw )

	# add patch for access to last sector on odd-sector-count disks needed
	# for parted to support GPT on those disks
	(cd linux ; patch -p1 < ../patches/patch.blkioctl-sector )

	# apply patch from Herbert Xu for zlib double free problem
	(cd linux ; patch -p1 < ../patches/patch.zlib-double-free )

	# apply patches from Matt Domsch via Richard Hirst for GPT issues
	(cd linux ; patch -p1 < ../patches/patch.gpt-guidcleanup )
	(cd linux ; patch -p1 < ../patches/patch.guid-byte-order )

	# security updates
	(cd linux ; patch -p1 < ../patches/patch.zlib-double-free2 )
	(cd linux ; patch -p1 < ../patches/2.4.18-5_to_2.4.18-14_backport.patch )
	(cd linux ; patch -p1 < ../patches/patch.mremap )
	(cd linux ; patch -p1 < ../patches/patch.mremap2 )

	(cd linux && patch -p0 < ../patches/patch.CAN-2004-0003.r128)
	(cd linux && patch -p1 < ../patches/patch.CAN-2004-0010.ncpfs)
	(cd linux && patch -p0 < ../patches/patch.CAN-2004-0109.ernie)
	(cd linux && patch -p1 < ../patches/patch.CAN-2004-0177.ext3_2.4)
	(cd linux && patch -p7 < ../patches/patch.CAN-2004-0178.sb)
	(cd linux && patch -p1 < ../patches/2.4.18-14.3_to_2.4.18-14.4_backport.patch)
	## prereq changes for 2.4.18-14.3_to_2.4.18-14.4_backport.patch
	(cd linux && patch -p1 < ../patches/patch.fpu-fixup)

	# add Debian packaging goo
	mkdir linux/debian
	cp debian/changelog linux/debian
	cp debian/control linux/debian
	> linux/debian/official

	# copy the source tree for each image flavor, fold in config file
	for i in $(flavours); do \
		cp -al linux build-$$i; \
		cp config/$$i build-$$i/.config; \
		cd build-$$i; \
		cd ..; \
	done

	# use itanium config as default for building kernel-headers
	cp config/itanium linux/.config

	touch stamp-unpack

build: stamp-build
stamp-build: stamp-unpack

	# little to do here... the real work is done in binary-arch target

	touch stamp-build

clean:
	rm -rf linux build-* stamp-* 
	dh_clean

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

	# create kernel-headers package

	cd linux; \
		make-kpkg --append-to-version=-ia64 kernel-headers
	cd linux; \
		make-kpkg --append-to-version=-ia64 kernel-source
	cat linux/debian/files >> debian/files

	mv *.deb ..

# Build architecture-dependent files here.
binary-arch: stamp-build

	# create kernel-image packages

	for i in $(flavours); do \
		cd build-$$i; \
		make-kpkg --append-to-version=-$$i kernel_image; \
		cd ..; \
		cat build-$$i/debian/files >> debian/files; \
	done

	mv *.deb ..

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