#! /usr/bin/make -f

# User configurable piece.
epoch	 := 0

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

# The package version should be UPSTREAM-REAL-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)

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

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

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

subarches := chrp prep pmac # common mbx apus
# mbx and apus configs are old; common does not work with make-kpkg
#  (set kimagesrc, kimagedest, etc.)

build:
	dh_testdir

clean:
	dh_testdir
	rm -rf build-tmp
	dh_clean

binary-indep:	build
	dh_testdir

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

	cp -a powerpc debian/tmp/usr/src/kernel-patches/powerpc
	for i in `ls config`; do \
	  cp config/$$i debian/tmp/usr/src/kernel-patches/powerpc/config.$$i; \
	done

	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)

../kernel-patch-$(basever)-$(arch)_$(v_patch)_all.deb:
	$(MAKE) -f debian/rules binary-patch

binary-images:
	dh_testdir
	dh_testroot
	rm -rf build-tmp
	mkdir 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
	if [ -f $(kdir)/drivers/usb/mkmap.adb ]; then		\
	  chmod a+x $(kdir)/drivers/usb/mkmap.adb;		\
	fi
# If we do this instead of using PATCH_THE_KERNEL, we get
# kernel-image-$(realver)-$(subarch) packages instead of
# kernel-image-$(basever)-$(subarch) packages.
#	cd $(kdir) && /usr/src/kernel-patches/$(arch)/apply/0$(arch)

	# Messes up version detection with kernel-package - but that's good
	export PATCH_THE_KERNEL=YES;					\
	export PATCH_DIR=$(shell pwd)/powerpc;				\
	for i in $(subarches); do					\
	  cp config/$$i $(kdir)/.config;				\
	  if [ -f ../kernel-image-$(basever)-$${i}_$(debver)_$(arch).deb ];	\
	  then :;							\
	  else								\
	    mkdir $(kdir)/debian;					\
	    cp debian/changelog debian/control $(kdir)/debian/;		\
	    echo official > $(kdir)/debian/official;			\
	    (cd $(kdir) && make-kpkg 					\
		  --subarch $$i --arch_in_name				\
		  kernel_image);					\
	    (cd $(kdir) && make-kpkg clean);				\
	    cp build-tmp/kernel-image-$(basever)-$${i}_$(debver)_$(arch).deb	\
		 ..;							\
	  fi;								\
	done;								\
	mkdir $(kdir)/debian;						\
	cp debian/changelog debian/control $(kdir)/debian/;		\
	echo official > $(kdir)/debian/official;			\
	(cd $(kdir) && make-kpkg kernel_headers);			\
	cp build-tmp/kernel-headers-$(basever)_$(debver)_$(arch).deb ..

kernel-images:	../kernel-image-$(basever)-pmac_$(debver)_$(arch).deb	\
		../kernel-image-$(basever)-prep_$(debver)_$(arch).deb	\
		../kernel-image-$(basever)-chrp_$(debver)_$(arch).deb	\
		../kernel-headers-$(basever)_$(debver)_$(arch).deb

binary-arch: binary-patch kernel-images
	dpkg-distaddfile kernel-image-$(basever)-pmac_$(debver)_$(arch).deb base optional
	dpkg-distaddfile kernel-image-$(basever)-prep_$(debver)_$(arch).deb base optional
	dpkg-distaddfile kernel-image-$(basever)-chrp_$(debver)_$(arch).deb base optional
	dpkg-distaddfile kernel-headers-$(basever)_$(debver)_$(arch).deb devel optional

binary: binary-arch binary-indep

update_configs: $(KSRC)/arch/ppc/Makefile
	for i in $(subarches); do					\
	  cp -f config/$$i $(KSRC)/.config;				\
	  (cd $(KSRC) && make oldconfig);				\
	  cp -f $(KSRC)/.config config/new.$$i;				\
	done

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:
