#! /usr/bin/make -f

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

# 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)

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

ktver := $(shell \
        sed -n 's/.*kernel-tree-\([^,]*\).*/\1/p; t e; b; :e q' \
                debian/control \
)

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

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

#configdir := arch/parisc/debian-configs
configdir := config

kpkgopts := --added-patches debian,hppa-2_4_26

subarches := $(shell \
        if ( command -v hppa64-linux-gcc > /dev/null 2>&1 ); \
        then \
                find $(configdir) -maxdepth 1 \( -type f -o -type l \) -a ! -name '*-udeb' -a -printf '%f\n'; \
        else \
                echo -e "\n\nWARNING: ============================================================" >&2; \
                echo "  Cannot build 64 bit kernel images as the 32 to 64 bit hppa cross-compiler" >&2; \
                echo "  is not installed.  If the official package is not yet available, an" >&2; \
                echo "  unofficial one may be found at" >&2; \
                echo "  <http://ftp.parisc-linux.org/unofficial-debs/>" >&2; \
                echo -e "WARNING: ================================================================\n\n" >&2; \
                find $(configdir) -maxdepth 1 \( -type f -o -type l \) -a ! -name '*-udeb' -a ! -name '64*' -printf '%f\n'; \
        fi \
)


build:
	dh_testdir

clean:
	dh_testdir
	rm -f source-unpack-stamp
	rm -rf build-tmp
	dh_clean

binary-indep:	build
	dh_testdir

source-unpack-stamp:
	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

	mkdir $(kdir)/debian || true
	touch source-unpack-stamp

kernel-headers: source-unpack-stamp
	set -e;								\
	export PATCH_THE_KERNEL=YES;					\
	export BUILD_DIR=$(shell pwd);					\
	cp debian/changelog debian/control $(kdir)/debian/;		\
	echo official > $(kdir)/debian/official;			\
	cp config/32 $(kdir)/.config;				\
	(cd $(kdir) && make-kpkg clean && make-kpkg $(kpkgopts) kernel_headers);	\
	cp build-tmp/kernel-headers-$(basever)_$(debver)_$(arch).deb ..; \

binary-images: source-unpack-stamp
	dh_testdir
	dh_testroot
	 # Messes up version detection with kernel-package - but that's good
	set -e;								\
	export PATCH_THE_KERNEL=YES;					\
	export BUILD_DIR=$(shell pwd);					\
	for i in $(subarches); do					\
	  name=kernel-image-$(basever)-$${i};				\
	  cp config/$$i $(kdir)/.config;				\
	  cp debian/changelog debian/control $(kdir)/debian/;		\
	  echo official > $(kdir)/debian/official;			\
	  (set -e;							\
	    cd $(kdir);							\
	    rm -f conf.vars;						\
	    make-kpkg clean;				\
	    make-kpkg $(kpkgopts) --append_to_version -$$i kernel_image;	\
	    );								\
	  cp build-tmp/$${name}_$(debver)_$(arch).deb			\
		..;							\
	done

binary-arch: binary-images kernel-headers
	dpkg-distaddfile kernel-image-$(basever)-32_$(debver)_$(arch).deb base optional
	dpkg-distaddfile kernel-image-$(basever)-32-smp_$(debver)_$(arch).deb base optional
	dpkg-distaddfile kernel-image-$(basever)-64_$(debver)_$(arch).deb base optional
	dpkg-distaddfile kernel-image-$(basever)-64-smp_$(debver)_$(arch).deb base optional
	dpkg-distaddfile kernel-headers-$(basever)_$(debver)_$(arch).deb devel optional

binary: binary-arch binary-indep

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

.PHONY: binary binary-arch binary-indep clean

# Local Variables:
# mode:Makefile
# End:
