#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

SHELL  := bash -e
UPLOADER  := $(shell dpkg-parsechangelog | sed -ne 's,^Maintainer: .[^<]*<\([^>]*\)>,\1,p')

DEB_REVISION :=  $(shell echo $(DEB_VERSION) | awk -F- '{print $$(NF)}')
ABINAME=$(DEB_VERSION_UPSTREAM_REVISION)
UPSTREAMVERSION=$(DEB_VERSION_UPSTREAM)

# Cross compilation setup

KERNEL_ARCH := $(DEB_HOST_GNU_CPU)

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
	NUMJOBS = 1
endif

BUILD_DIR=debian/build
BUILD_HEADERS_DIR=debian/build-headers

export DEB_HOST_ARCH DEB_HOST_GNU_TYPE DEB_BUILD_ARCH

setup_env := env -u ABINAME -u ARCH -u FEATURESET -u FLAVOUR -u VERSION -u LOCALVERSION
setup_env += DISTRIBUTION_OFFICIAL_BUILD=1 DISTRIBUTION_UPLOADER=$(UPLOADER) DISTRIBUTION_VERSION="$(DEB_VERSION)"

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	setup_env += CROSS_COMPILE=$(DEB_HOST_GNU_TYPE)-
	setup_env += ARCH=$(KERNEL_ARCH)
endif

ifneq (,$(findstring ccache,$(DEB_BUILD_OPTIONS)))
	setup_env += 'CC="ccache $(CROSS_COMPILE)gcc"'
	HEADERS_CC := "ccache $(HEADERS_CC)"
endif

MAKE_CLEAN = $(setup_env) $(MAKE)

PACKAGE = bubba3-kernel
HEADER_PACKAGE := bubba3-kernel-headers

CONFIG=bubba3_defconfig

PACKAGE_DIR=debian/$(PACKAGE)

%:
	dh $@

define REAL_VERSION
$(shell cat $(BUILD_DIR)/include/config/kernel.release)
endef

override_dh_auto_configure: 
	mkdir -p $(BUILD_DIR)
	touch .scmversion
	echo '-$(DEB_REVISION)' > $(BUILD_DIR)/localversion
	+$(MAKE_CLEAN) O=$(BUILD_DIR) $(CONFIG)
	+$(MAKE_CLEAN) O=$(BUILD_DIR) prepare

# taken from rules.real
define make-tools
+mkdir -p $(BUILD_HEADERS_DIR)/$(1) && $(MAKE_CLEAN) -C $(BUILD_HEADERS_DIR)/$(1) -f $(CURDIR)/debian/rules.d/$(1)/Makefile top_srcdir=$(CURDIR) top_rulesdir=$(CURDIR)/debian/rules.d OUTDIR=$(1) KERNEL_ARCH=$(KERNEL_ARCH)
endef

override_dh_auto_build:
	+$(MAKE_CLEAN) O=$(BUILD_DIR) -j$(NUMJOBS) zImage
	+$(MAKE_CLEAN) O=$(BUILD_DIR) kirkwood-b3.dtb
	debian/build_b3_uImage.sh $(BUILD_DIR) $(call REAL_VERSION)
	+$(MAKE_CLEAN) O=$(BUILD_DIR) -j$(NUMJOBS) modules
	sed 's/@VERSION@/$(call REAL_VERSION)/g' < debian/bubba3-kernel.prerm.in > debian/bubba3-kernel.prerm
	sed 's/@VERSION@/$(call REAL_VERSION)/g' < debian/bubba3-kernel.postinst.in > debian/bubba3-kernel.postinst
	# headers tools, take from debian kernel package
	# step $(STAMPS_DIR)/build-tools-headers
	mkdir -p $(BUILD_HEADERS_DIR)/headers-tools
	$(MAKE) ARCH=$(KERNEL_ARCH) O=$(BUILD_HEADERS_DIR)/headers-tools \
		INSTALL_HDR_PATH=$(BUILD_HEADERS_DIR) headers_install
	$(call make-tools,certs)
	$(call make-tools,scripts)
	$(call make-tools,tools/objtool)
	
override_dh_installmodules:
	dh_installmodules -n

override_dh_auto_install: install_headers
override_dh_auto_install:
	+$(MAKE_CLEAN) O=$(BUILD_DIR) INSTALL_MOD_PATH=$(CURDIR)/$(PACKAGE_DIR) INSTALL_MOD_STRIP=1 modules_install
	mkdir -p $(PACKAGE_DIR)/boot
	cp $(BUILD_DIR)/uImage $(PACKAGE_DIR)/boot
	cp $(BUILD_DIR)/.config $(PACKAGE_DIR)/boot/config-$(call REAL_VERSION)
	cp $(BUILD_DIR)/System.map $(PACKAGE_DIR)/boot/System.map-$(call REAL_VERSION)
	find $(PACKAGE_DIR)/lib/modules/$(call REAL_VERSION)/ \
		-mindepth 1 -maxdepth 1 \
		\! -name kernel \! -name modules.order \
		\! -name modules.builtin \
		\! -name modules.builtin.modinfo \
		-exec rm {} \+
	rm -rf $(PACKAGE_DIR)/lib/firmware

install_headers: PACKAGE_NAME = $(HEADER_PACKAGE)
install_headers: DEST_DIR = $(CURDIR)/debian/$(PACKAGE_NAME)
install_headers: BASE_DIR = /usr/src/$(PACKAGE_NAME)
install_headers: DIR = $(DEST_DIR)/$(BASE_DIR)
install_headers:
	# taken from official debian 6.1 kernel package rules.real, section binary_headers
	mkdir -p $(DIR)/arch/$(KERNEL_ARCH)/kernel
	cp -a $(BUILD_DIR)/{.config,Module.symvers,include} $(DIR)
	cp -a $(BUILD_DIR)/arch/$(KERNEL_ARCH)/include $(DIR)/arch/$(KERNEL_ARCH)
	find $(DIR) -name '*.cmd' -delete
	# taken from official debian 6.1 kernel package rules.real, section binary_headers-common
	( \
	echo Makefile; \
	find arch/$(KERNEL_ARCH) -maxdepth 1 -name 'Makefile*' -print; \
	find arch/$(KERNEL_ARCH) \( -name 'Kbuild.platforms' -o -name 'Platform' \) -print; \
	find $$(find arch/$(KERNEL_ARCH) \( -name include -o -name scripts \) -type d -print) -print; \
	find include -type f; \
	) \
	| \
	cpio -pd --preserve-modification-time '$(DIR)'

	$(call make-tools,certs) install DESTDIR=$(DEST_DIR) prefix=$(BASE_DIR)
	$(call make-tools,scripts) install DESTDIR=$(DEST_DIR) prefix=$(BASE_DIR)
	$(call make-tools,tools/objtool) install DESTDIR=$(DEST_DIR) prefix=$(BASE_DIR)
	cp -a $(BUILD_DIR)/scripts/module.lds $(DIR)/scripts

	mkdir -p $(DEST_DIR)/lib/modules/$(call REAL_VERSION)
	ln -s $(BASE_DIR) $(DEST_DIR)/lib/modules/$(call REAL_VERSION)/build

override_dh_clean:
	rm -f debian/bubba3-kernel.prerm debian/bubba3-kernel.postinst
	rm -rf $(BUILD_DIR)
	rm -rf $(BUILD_HEADERS_DIR)
	dh_clean $@

override_dh_auto_clean:
	+ARCH=$(DEB_HOST_ARCH_CPU) make mrproper
	rm -f .scmversion

override_dh_strip:
	dh_strip --no-automatic-dbgsym

override_dh_auto_test:
