#!/usr/bin/make -f
#
# debian/rules for m68k kernel packages
#

VERSION        := 2.4.27
SUBARCHES      := amiga atari bvme6000 mac mvme147 mvme16x q40 
## current fails: sun3 sun3x

SHELL          := /bin/bash -e
PATCHES        := /usr/src/kernel-patches/m68k/$(VERSION)

DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)

MAKEKPKG       := make-kpkg
ifneq ($(DEB_BUILD_ARCH),m68k)
MAKEKPKG       += --arch m68k --cross_compile m68k-linux
endif

build: build-stamp
build-stamp: unpack-stamp patch-stamp
	dh_testdir
	touch build-stamp

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

unpack: unpack-stamp
unpack-stamp:
	if   [ -f   /usr/src/kernel-source-$(VERSION).tar ]; then \
		 tar xf /usr/src/kernel-source-$(VERSION).tar;        \
	else bzcat  /usr/src/kernel-source-$(VERSION).tar.bz2 | tar xf -; fi
	touch unpack-stamp

patch: patch-stamp
patch-stamp: unpack-stamp
	dh_testdir
	cd kernel-source-$(VERSION); $(PATCHES)/apply/apply
	touch patch-stamp

unpatch: patch-stamp
	dh_testdir
	cd kernel-source-$(VERSION); $(PATCHES)/unpatch/unpatch
	rm -f patch-stamp

image: image-stamp
image-stamp: build-stamp
	dh_testdir
	dh_testroot
	for SUBARCH in $(SUBARCHES); do \
	  cd kernel-source-$(VERSION); \
	  make-kpkg clean ; \
	  mkdir -p                debian ; \
	  touch                   debian/official ; \
	  cp ../debian/control       debian/ ; \
	  cp ../debian/changelog     debian/ ; \
	  cp ../debian/config.$$SUBARCH .config ; \
	  $(MAKEKPKG) --append-to-version=-$$SUBARCH kernel_image	; \
	  cd .. ; \
	  sed -e s/_$(DEB_BUILD_ARCH).deb/_m68k.deb/ \
		<kernel-source-$(VERSION)/debian/files >>debian/files ; \
	done
	mv *.deb ..
	touch image-stamp

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

# Build architecture-dependent files here.
binary-arch: image-stamp

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary
.PHONY: unpack patch unpatch image
