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

ifeq ($(arch),mips)
  subarches := sb1-swarm-bn r4k-ip22 r5k-ip22
endif
ifeq ($(arch),mipsel)
  subarches := r5k-cobalt sb1-swarm-bn r3k-kn02 r4k-kn04 r5k-lasat xxs1500
endif

# handle DEB_BUILD_OPTIONS for mips-tools package
MT_CFLAGS = -W -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=`cat /proc/cpuinfo | grep '^processor[:space:]*' | wc -l`

k_patch_dir := debian/$(p_patch)/usr/src/kernel-patches

clean:
	dh_testdir
	rm -f build-stamp
	rm -f source-unpack-stamp
	rm -rf build-tmp
	dh_clean

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

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

build: build-stamp
build-stamp: source-unpack-stamp
	dh_testdir

	# build the mips-tools
	( PATCH_THE_KERNEL=YES; PATCH_DIR=$(shell pwd)/mips; \
	  export PATCH_THE_KERNEL PATCH_DIR; \
	  cd $(kdir) && make-kpkg clean && make-kpkg debian )
	cd $(kdir)/arch/mips/boot \
		&& gcc $(MT_CFLAGS) -o addinitrd addinitrd.c \
		&& gcc $(MT_CFLAGS) -o elf2ecoff elf2ecoff.c
	touch $@

source-unpack-stamp:
	rm -rf build-tmp
	mkdir build-tmp
	tar -C build-tmp --bzip2 -xf /usr/src/kernel-source-$(basever).tar.bz2
	ln -sf asm-mips $(kdir)/include/asm
	touch $@

binary-headers: source-unpack-stamp
	dh_testdir
	dh_testroot

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

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

	# Messes up version detection with kernel-package - but that's good
	$(foreach sub,$(subarches), \
		set -e; \
		echo "Building subarch $(sub)"; \
		( PATCH_THE_KERNEL=YES; PATCH_DIR=$(shell pwd)/mips; \
		  export PATCH_THE_KERNEL PATCH_DIR; \
		  cd $(kdir) && make-kpkg clean && make-kpkg debian ); \
		cp debian/changelog debian/control $(kdir)/debian/; \
		echo official > $(kdir)/debian/official; \
		cp config/$(arch)/$(sub) $(kdir)/.config; \
		( PATCH_THE_KERNEL=YES; PATCH_DIR=$(shell pwd)/mips; \
		  export PATCH_THE_KERNEL PATCH_DIR; \
		  cd $(kdir) && CONCURRENCY_LEVEL=$(NUM_CPUS) make-kpkg \
		  --subarch $(sub) --append-to-version=-$(sub) kernel_image ); \
		cp build-tmp/kernel-image-$(basever)-$(sub)_$(debver)_$(arch).deb ..;)

binary-arch: binary-images binary-headers
	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
	$(foreach sub,$(subarches), \
	    dpkg-distaddfile kernel-image-$(basever)-$(sub)_$(debver)_$(arch).deb base optional;)
ifneq ($(subarches),)
	dpkg-distaddfile kernel-headers-$(basever)_$(debver)_$(arch).deb devel optional
endif

binary-indep:

binary: binary-arch binary-indep

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

# Local Variables:
# mode:Makefile
# End:
