#! /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)-ia64
v_patch  := $(realver)-$(minorver)

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

subarches := itanium-smp itanium mckinley mckinley-smp

kpkgopts := --added-patches debian,ia64-2_4_25 --initrd

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

	if [ -f $(kdir)/drivers/usb/mkmap.adb ]; then		\
	  chmod a+x $(kdir)/drivers/usb/mkmap.adb;		\
	fi

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

kernel-headers: source-unpack-stamp
	cp debian/changelog debian/control $(kdir)/debian/
	echo official > $(kdir)/debian/official
	cp config/itanium $(kdir)/.config
	cd $(kdir) && 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
	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 &&                       \
	  (cd $(kdir) &&                                                   \
	   make-kpkg $(kpkgopts) --append_to_version -$$i kernel_image) && \
	  (cd $(kdir) &&                                                   \
	   make-kpkg $(kpkgopts) --append_to_version -$$i clean) &&        \
	  cp build-tmp/$${name}_$(debver)_$(arch).deb ..;                  \
	done

binary-arch: binary-images kernel-headers
	dpkg-distaddfile kernel-image-$(basever)-itanium_$(debver)_$(arch).deb base optional
	dpkg-distaddfile kernel-image-$(basever)-itanium-smp_$(debver)_$(arch).deb base optional
	dpkg-distaddfile kernel-image-$(basever)-mckinley_$(debver)_$(arch).deb base optional
	dpkg-distaddfile kernel-image-$(basever)-mckinley-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/ia64/Makefile
	for i in $(subarches); do					\
	  cp -f config/$$i $(KSRC)/.config;				\
	  (cd $(KSRC) && make oldconfig);				\
	  cp -f $(KSRC)/.config config/new.$$i;				\
	done

## assume the itanium config is up-to-date.  generate the other configs
## by copying it and making the necessary modifications
sync_configs: config/itanium
	sed 's/# CONFIG_SMP is not set/CONFIG_SMP=y\nCONFIG_NR_CPUS=32/' \
	  < config/itanium > config/itanium-smp
	sed 's/# CONFIG_MCKINLEY is not set/CONFIG_MCKINLEY=y/' \
	  < config/itanium > config/mckinley.tmp
	sed 's/CONFIG_ITANIUM=y/# CONFIG_ITANIUM is not set/' \
	  < config/mckinley.tmp > config/mckinley
	rm -f config/mckinley.tmp
	sed 's/# CONFIG_SMP is not set/CONFIG_SMP=y\nCONFIG_NR_CPUS=32/' \
	  < config/mckinley > config/mckinley-smp
 
.PHONY: binary binary-arch binary-indep clean

# Local Variables:
# mode:Makefile
# End:
