#!/usr/bin/make -f
#
# debian/rules for kernel-image-*-sparc
#
# GNU copyright 1997 to 1999 by Joey Hess.
# Copyright (c) 1999 Herbert Xu <herbert@debian.org>
# Copyright (c) 2000 Ben Collins <bcollins@debian.org>
# Copyright (c) 2004 Joshua Kwan <joshk@triplehelix.org>

-include /etc/kernel-pkg.conf

KPKG_MAINTAINER	:= "Debian SPARC 2.4 Kernel Maintainers"
KPKG_EMAIL	:= "sparc24-maint@triplehelix.org"
VERSION		:= 2.4.26
kdir		:= kernel-source-$(VERSION)
debver		:= $(shell dpkg-parsechangelog | awk -F' ' '/^Version:/ {print $$2}' | sed 's/^[0-9]\+://')
SHELL		:= /bin/bash -e
do_clean	:= no
SMP_CPU_COUNT	:= 32
KPKG_ARCH	:= sparc

ifneq (root,$(shell whoami))
ROOT		:= --rootcmd fakeroot
endif

ifeq (,$(DEB_HOST_ARCH))
DEB_HOST_ARCH	:= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_BUILD_ARCH  := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
endif

ifneq ($(DEB_HOST_ARCH),$(KPKG_ARCH))
ifneq (,$(filter-out debian/control clean stamps/build-dummy-%-stamp stamps/patch-stamp stamps/unpack-stamp patch unpatch,$(MAKECMDGOALS)))
$(error Use dpkg-buildpackage -asparc! ($(DEB_HOST_ARCH) != $(KPKG_ARCH)))
endif
endif

ifneq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
CROSS_COMPILE	:= sparc-linux
endif

export KPKG_MAINTAINER KPKG_EMAIL KPKG_ARCH CROSS_COMPILE do_clean HEADER_CLEAN_HOOK

all_subarches		:= $(foreach subarch,$(shell ls config),stamps/build-$(subarch)-stamp stamps/build-$(subarch)-smp-stamp)
all_dummies		:= $(foreach subarch,sparc32 sparc64,stamps/build-dummy-2.4-$(subarch)-stamp stamps/build-dummy-2.4-$(subarch)-smp-stamp)

stamps/unpack-stamp:
	tar --bzip2 -xf /usr/src/$(kdir).tar.bz2
	touch $@

patch: stamps/patch-stamp
stamps/patch-stamp: stamps/unpack-stamp 
	dpatch -d $(kdir) apply-all
	dpatch cat-all >  $@

unpatch:
ifneq (,$(wildcard stamps/patch-stamp))
	dpatch -d $(kdir) deapply-all
endif
	rm -f stamps/patch-stamp

debian/control: debian/control.in debian/rules
	sed -e 's/=V/$(VERSION)/g' debian/control.in > debian/control

stamps/build-dummy-%-stamp:
	dh_testdir
	dh_testroot

	dh_clean -k -pkernel-image-$*
	dh_installdeb -pkernel-image-$*
	dh_installdocs -pkernel-image-$*
	dh_installchangelogs -pkernel-image-$*
	dh_compress -pkernel-image-$*
	dh_gencontrol -pkernel-image-$*
	dh_builddeb -pkernel-image-$*

stamps/build-%-stamp: config/% stamps/patch-stamp
	rm -f $@

	mkdir -p $(kdir)/debian
	cp debian/changelog debian/control $(kdir)/debian
	echo official > $(kdir)/debian/official
	cp config/$* $(kdir)/.config
	$(SHELL) -c "( \
		cd $(kdir); \
		make-kpkg $(ROOT) --subarch $* clean; \
		make-kpkg --subarch $* --append-to-version "-$*" build; \
		$(CROSS_COMPILE)-strip -R .comment -R .note -K sun4u_init -K _end -K _start vmlinux; \
		make-kpkg $(ROOT) --subarch $* --append-to-version "-$*" kernel-image; \
		cat debian/files >> ../debian/files; \
	);"
	mv -f kernel-image-$(VERSION)-$*_$(debver)_sparc.deb ../
	touch $@

config/%-smp: config/%
	sed -e 's/# CONFIG_SMP is not set/CONFIG_SMP=y/' $< > $@
	echo "CONFIG_NR_CPUS=$(SMP_CPU_COUNT)" >> $@

clean: debian/control
	rm -rf $(kdir) debian/patched
	rm -f *.deb debian/files stamps/* config/*-smp
	dh_clean

install: stamps/install-stamp
stamps/install-stamp: build stamps/patch-stamp debian/control
	touch $@

binary-indep: build install
# Ye olde no-op.

# Build architecture-dependent files here.
binary-arch: $(all_dummies) build install

build: debian/control stamps/build-stamp
stamps/build-stamp: stamps/patch-stamp $(all_subarches) 
	touch $@

binary: binary-indep binary-arch

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