#!/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>
#

KPKG_MAINTAINER	:= "Ben Collins"
KPKG_EMAIL	:= "bcollins@debian.org"
VERSION		:= $(shell cd /usr/src && ls -d kernel-source-* | tail -1 | sed -e s/kernel-source-\\\(.\*\\\).tar.bz2/\\1/)
SHELL		:= /bin/bash -e
DEFAULT_ARCH	:= sun4cdm
# just a default
SUBARCH		:= $(DEFAULT_ARCH)
# This command ensures we run as sparc32, and not sparc64
ifeq (,$(filter sun4u%,$(SUBARCH)))
SPARC		:= sparc32
else
SPARC		:= sparc64
endif
do_clean	:= no

export KPKG_MAINTAINER KPKG_EMAIL do_clean

kh_pkg		:= kernel-headers-$(VERSION)-sparc
kp_files	:= /usr/share/kernel-package

setup: unpack patch addon

unpack: stamps/$(VERSION)/unpack-stamp
stamps/$(VERSION)/unpack-stamp:
	tar Ixf /usr/src/kernel-source-$(VERSION).tar.bz2
	touch stamps/$(VERSION)/unpack-stamp

patch: stamps/$(VERSION)/patch-stamp
stamps/$(VERSION)/patch-stamp: stamps/$(VERSION)/addon-stamp
	@if [ -d patches/$(VERSION) ]; then verpat="patches/$(VERSION)/*"; fi; \
		for patch in patches/all/* $$verpat; do \
			echo "patch -d kernel-source-$(VERSION) -p1 < $$patch"; \
			patch -d kernel-source-$(VERSION) -p1 < $$patch; \
		done
	touch stamps/$(VERSION)/patch-stamp

addon: stamps/$(VERSION)/addon-stamp
stamps/$(VERSION)/addon-stamp: stamps/$(VERSION)/unpack-stamp
	@if [ -d addons/$(VERSION) ]; then veradd="addons/$(VERSION)/*"; fi; \
		for addon in addons/all/* $$veradd; do \
			tarflags=""; \
			case "$$addon" in \
				*.bz2)	tarflags="Ixf";; \
				*.gz)	tarflags="zxf";; \
			esac; \
			echo "tar $$tarflags $$addon -C kernel-source-$(VERSION)"; \
			tar $$tarflags $$addon -C kernel-source-$(VERSION); \
		done
	touch stamps/$(VERSION)/addon-stamp

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

stamps/$(VERSION)/build-%-stamp: setup config/$(VERSION)/%
	rm -f $@

	mkdir -p kernel-source-$(VERSION)/debian
	cp debian/changelog kernel-source-$(VERSION)/debian
	echo official >kernel-source-$(VERSION)/debian/official
	cp config/$(VERSION)/$* kernel-source-$(VERSION)/.config
	( \
		cd kernel-source-$(VERSION); \
		$(SPARC) make-kpkg clean; \
		$(SPARC) make-kpkg --subarch $* --arch_in_name build; \
		cat ../debian/control > debian/control; \
		$(SPARC) make-kpkg --subarch $* --arch_in_name kernel-image; \
		cat debian/files >> ../debian/files; \
	);
	mv -f kernel-image-$(VERSION)-$*_*.deb ../
	touch $@

stamps/$(VERSION)/kernel-header-stamp: setup config/$(VERSION)/$(DEFAULT_ARCH)
	rm -rf debian/$(kh_pkg)
	mkdir -p debian/$(kh_pkg)/DEBIAN
	mkdir -p debian/$(kh_pkg)/usr/src/$(kh_pkg)
	mkdir -p debian/$(kh_pkg)/usr/share/doc/$(kh_pkg)
	sed -e 's/=P/$(kh_pkg)/g' \
		debian/include.postinst > debian/$(kh_pkg)/DEBIAN/postinst
	chmod 755 debian/$(kh_pkg)/DEBIAN/postinst
	sed -e 's/=P/$(kh_pkg)/g' \
		debian/include.prerm > debian/$(kh_pkg)/DEBIAN/prerm
	chmod 755 debian/$(kh_pkg)/DEBIAN/prerm
	install -m 644 $(kp_files)/copyright.headers \
		debian/$(kh_pkg)/usr/share/doc/$(kh_pkg)/copyright
	install -m 644 debian/changelog \
		debian/$(kh_pkg)/usr/share/doc/$(kh_pkg)/changelog
	gzip -9qf debian/$(kh_pkg)/usr/share/doc/$(kh_pkg)/changelog
	( \
		cp config/$(VERSION)/sun4cdm kernel-source-$(VERSION)/.config; \
		cd kernel-source-$(VERSION); \
		sparc32 make-kpkg clean; \
		sparc32 make oldconfig dep; \
	)
	(cd kernel-source-$(VERSION) ; tar cf - `ls -d include/* | grep -v /asm`\
		include/asm-sparc) | \
		(cd debian/$(kh_pkg)/usr/src/$(kh_pkg); umask 022; tar xsf -)
	( \
		cp config/$(VERSION)/sun4u kernel-source-$(VERSION)/.config; \
		cd kernel-source-$(VERSION); \
		sparc64 make-kpkg clean; \
		sparc64 make oldconfig dep; \
	)
	(cd kernel-source-$(VERSION) ; tar cf - include/asm-sparc64) | \
		(cd debian/$(kh_pkg)/usr/src/$(kh_pkg); umask 022; tar xsf -)
	(cd debian/$(kh_pkg)/usr/src/$(kh_pkg)/include; rm -fr asm)
	[ -x debian/generate-asm.sh ] || chmod 755 debian/generate-asm.sh
	install -m 755 debian/generate-asm.sh debian/$(kh_pkg)/usr/src/$(kh_pkg)
	debian/generate-asm.sh debian/$(kh_pkg)/usr/src/$(kh_pkg)/include
	dpkg-gencontrol -DArchitecture=all -isp \
		-p$(kh_pkg) -Pdebian/$(kh_pkg)/
	chown -R root.root debian/$(kh_pkg)
	chmod -R og=rX debian/$(kh_pkg)
	dpkg --build debian/$(kh_pkg) ../
	rm -rf debian/$(kh_pkg)
	touch $@

clean: debian/control
	rm -rf kernel-source-*
	rm -f *.deb stamps/$(VERSION)/*-stamp debian/files
	dh_clean

install: stamps/$(VERSION)/install-stamp
stamps/$(VERSION)/install-stamp: build stamps/$(VERSION)/kernel-header-stamp
	@for subarch in config/$(VERSION)/*; do \
		subarch=$$(basename $$subarch); \
		debian/rules SUBARCH=$$subarch stamps/$(VERSION)/build-$${subarch}-stamp; \
	done
	touch stamps/$(VERSION)/install-stamp

# Build architecture-independent files here.
binary-indep:

# Build architecture-dependent files here.
binary-arch: install
build: stamps/$(VERSION)/build-stamp
stamps/$(VERSION)/build-stamp: debian/control
	touch stamps/$(VERSION)/build-stamp

FORCE:

binary: binary-indep binary-arch

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