#! /usr/bin/make -f

# User configurable piece.
epoch	 := 0

arch     := $(shell dpkg --print-architecture)

# The package version should be UPSTREAM-MINOR
# For instance:
#  2.2.15pre16-1
# Just one '-' should be present.
realver := $(shell dpkg-parsechangelog | awk -F' ' '/^Version:/ {print $$2}' | awk -F- '{print $$1}')
minorver  := $(shell dpkg-parsechangelog | awk -F- '/^Version:/ {print $$2}')

# The hypothetical kernel version - the unpatched source tree in use.
# The changelog should refer to kernel-patch-$(basever)-$(arch)
basever  := $(shell dpkg-parsechangelog | awk -F- '/^Source:/ {print $$3}')

# The debian revision of the image packages.
debver   := $(realver)-$(minorver)

# The names of the kernel image and headers packages
imgname  := kernel-image-$(basever)-apus_$(debver)_$(arch).deb
hdrname  := kernel-headers-$(basever)-apus_$(debver)_$(arch).deb

ifneq ($(epoch),0)
 packver := $(epoch):$(debver)
else
 packver := $(debver)
endif

p_patch  := kernel-patch-$(basever)-apus
v_patch  := $(realver)-$(minorver)

kdir := build-tmp/kernel-source-$(basever)

build:	build-stamp

build-stamp:
	dh_testdir
	mkdir -p build-tmp
	if [ -f  /usr/src/kernel-source-$(basever).tar.bz2 ]; then		\
	  cd build-tmp && tar --bzip2 -xf /usr/src/kernel-source-$(basever).tar.bz2;	\
	else								\
	  cd build-tmp && tar xzf /usr/src/kernel-source-$(basever).tar.gz;	\
	fi
	ln -sf asm-ppc $(kdir)/include/asm

	touch $@

clean:
	dh_testdir
	rm -rf build-tmp *-stamp
	dh_clean

binary-indep:
	dh_testdir

binary-image-apus-stamp:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -pkernel-image-apus
	dh_installdocs -pkernel-image-apus
	dh_installchangelogs -pkernel-image-apus
	dh_compress -pkernel-image-apus
	dh_installdeb -pkernel-image-apus
	dh_fixperms -pkernel-image-apus
	dh_gencontrol -pkernel-image-apus
	dh_md5sums -pkernel-image-apus
	dh_builddeb -pkernel-image-apus

	touch $@

binary-patch-stamp:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -p$(p_patch)

	cp -a $(arch) debian/tmp/usr/src/kernel-patches/$(arch)
	cp config/apus debian/tmp/usr/src/kernel-patches/$(arch)/config.apus;

	dh_installdocs -p$(p_patch)
	dh_installchangelogs -p$(p_patch)
	dh_compress -p$(p_patch)
	dh_installdeb -p$(p_patch)
	dh_fixperms -p$(p_patch)
	dh_gencontrol -p$(p_patch)
	dh_md5sums -p$(p_patch)
	dh_builddeb -p$(p_patch)

	touch $@

binary-image:
	dh_testdir
	dh_testroot
	dpkg-checkbuilddeps -B

# Messes up version detection with kernel-package - but that's good
	export PATCH_THE_KERNEL=YES;					\
	export PATCH_DIR=$(shell pwd)/$(arch);				\
	cp config/apus $(kdir)/.config;					\
	mkdir $(kdir)/debian;						\
	cp debian/changelog debian/control $(kdir)/debian/;		\
	echo official > $(kdir)/debian/official;			\
	(cd $(kdir) && make-kpkg --append-to-version -apus		\
		--subarch apus kernel_{image,headers});						\
	cp build-tmp/{$(imgname),$(hdrname)} ..;

binary-arch: binary-image-apus-stamp binary-patch-stamp binary-image
	dpkg-distaddfile $(imgname) base optional
	dpkg-distaddfile $(hdrname) devel optional

binary: binary-indep binary-arch


source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

.PHONY: binary binary-arch binary-indep clean

# Local Variables:
# mode:Makefile
# End:
