#! /usr/bin/make -f

# 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:/ { if (match($$4,"^pre") || match($$4,"^rc")) print $$3"-"$$4; else print $$3}')

# The debian revision of the image packages.
debver   := $(realver)-$(minorver)

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

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

p_tools  := mips-tools

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

# sb1-swarm-bn temporarily removed due to
# build problems
ifeq ($(arch),mips)
  subarches := sb1-swarm-bn r4k-ip22 r5k-ip22
  udeb_subarches := # r4k-ip22
else
  subarches := r5k-cobalt sb1-swarm-bn r3k-kn02 r4k-kn04 r5k-lasat
  udeb_subarches := # r4k-kn04 r3k-kn02
endif

# handle DEB_BUILD_OPTIONS for mips-tools package
MT_CFLAGS = -Wall -g -pipe
MT_INSTALL = install
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  MT_CFLAGS += -O0
else
  MT_CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  MT_INSTALL += -s
endif

NUM_CPUS=1
#This leads to spurious unresolved symbols:
#NUM_CPUS=`cat /proc/cpuinfo | grep '^processor[:space:]*' | wc -l`

clean:
	dh_testdir
	rm -rf build-tmp
	dh_clean
	[ -f mips/linux-mips.diff ] || gunzip mips/linux-mips.diff.gz
	rm -f source-unpack-stamp
	rm -f build-stamp

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -p$(p_tools)

	# install files for kernel-patch package
	mkdir -p debian/$(p_patch)/usr/src/kernel-patches/mips/
	for i in `ls config/mips`; do   \
	  cp config/mips/$$i 		\
	  	debian/$(p_patch)/usr/src/kernel-patches/mips/config.mips.$$i; \
	done
	for i in `ls config/mipsel`; do \
	  cp config/mipsel/$$i 		\
	  	debian/$(p_patch)/usr/src/kernel-patches/mips/config.mipsel.$$i; \
	done
	cp -a mips debian/$(p_patch)/usr/src/kernel-patches/
	# and for mips-tools package
	$(MT_INSTALL) $(kdir)/arch/mips/boot/elf2ecoff debian/$(p_tools)/usr/bin

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
	ln -sf asm-mips $(kdir)/include/asm

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

build: build-stamp
build-stamp: source-unpack-stamp
	dh_testdir
	
	[ -f mips/linux-mips.diff.gz ] || gzip -9 mips/linux-mips.diff
	# build the mips-tools
	export PATCH_THE_KERNEL=YES;					\
	export PATCH_DIR=$(shell pwd)/mips;				\
	(cd $(kdir) && make-kpkg clean && make-kpkg debian)
	( cd build-tmp/kernel-source-$(basever)/arch/mips/boot	&& 	\
		gcc $(MT_CFLAGS) -o addinitrd addinitrd.c	&& 	\
		gcc $(MT_CFLAGS) -o elf2ecoff elf2ecoff.c )
	touch build-stamp

binary-indep: build
	dh_testdir

binary-headers: source-unpack-stamp
	dh_testdir
	dh_testroot

	export PATCH_THE_KERNEL=YES;					\
	export PATCH_DIR=$(shell pwd)/mips;				\
	set -e;								\
	(cd $(kdir) && make-kpkg clean);				\
	mkdir $(kdir)/debian || true;					\
	cp debian/changelog debian/control $(kdir)/debian/;		\
	echo official > $(kdir)/debian/official;			\
	(cd $(kdir) && make-kpkg kernel_headers);			\
	cp build-tmp/kernel-headers-$(basever)_$(debver)_$(arch).deb ..

binary-images: source-unpack-stamp build install
	dh_testdir
	dh_testroot
	chmod a+x ./scripts/copy-modules

	# Messes up version detection with kernel-package - but that's good
	export PATCH_THE_KERNEL=YES;					\
	export PATCH_DIR=$(shell pwd)/mips;				\
	set -e;								\
	for i in $(subarches); do					\
	    name=kernel-image-$(basever)-$${i};				\
	    echo "Building subarch $$i";				\
	    rm -rf build-tmp/kernel-source-$(basever);			\
	    (cd build-tmp;						\
	     tar --bzip2 -xf /usr/src/kernel-source-$(basever).tar.bz2); \
	    mkdir $(kdir)/debian || true;				\
	    cp config/$(arch)/$$i $(kdir)/.config;			\
	    cp debian/changelog debian/control $(kdir)/debian/;		\
	    echo official > $(kdir)/debian/official;			\
	    (cd $(kdir) && CLEAN_SOURCE=NO CONCURRENCY_LEVEL=$(NUM_CPUS) \
	        make-kpkg --subarch $$i					\
	        --append-to-version=-$$i kernel_image);			\
	    cp build-tmp/$${name}_$(debver)_$(arch).deb	..;		\
            if expr match "$(udeb_subarches)" $$i; then			\
		./scripts/copy-modules $$i $(basever) $(kdir);		\
		install -D -m 644 $(kdir)/vmlinux 			\
		    debian/kernel-image-$(basever)-$$i-udeb/boot/vmlinux; \
	    fi;								\
	done

binary-image-udebs: binary-images
	dh_installdirs
	dh_fixperms

	for i in $(udeb_subarches); do					\
	    for j in $$(dh_listpackages | grep -- $$i-udeb\$$); do 	\
	        mkdir -p debian/$$j/DEBIAN; 				\
	        dpkg-gencontrol -p$$j -Pdebian/$$j -isp 		\
	            -n$${j}_$(debver)_$(arch).udeb; 			\
	        dh_builddeb --package=$$j 				\
	            --filename=$${j}_$(debver)_$(arch).udeb;		\
	    done;							\
	done

binary-arch: binary-images binary-headers binary-image-udebs
	dh_testdir
	dh_testroot
	dh_installdocs -p$(p_tools) -p$(p_patch)
	dh_installchangelogs -p$(p_tools) -p$(p_patch)
	dh_installman -p$(p_tools)

	dh_compress -p$(p_tools) -p$(p_patch)
	dh_installdeb -p$(p_tools) -p$(p_patch)
	dh_fixperms -p$(p_tools) -p$(p_patch)
	dh_shlibdeps -p$(p_tools)
	dh_gencontrol -p$(p_tools) -p$(p_patch)
	dh_md5sums -p$(p_tools) -p$(p_patch)
	dh_builddeb -p$(p_tools) -p$(p_patch)
	# finally add the kernel images and headers
	for i in $(subarches); do				 	\
	    dpkg-distaddfile kernel-image-$(basever)-$${i}_$(debver)_$(arch).deb base optional; \
	done
	dpkg-distaddfile kernel-headers-$(basever)_$(debver)_$(arch).deb devel optional

binary: binary-arch binary-indep

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

# Local Variables:
# mode:Makefile
# End:
