#!/usr/bin/make -f
# -*- makefile -*-
#
# debian/rules file for panasonic-hotkey-kernel
#
# 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

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

# some default definitions, important!
# 
# Name of the source package
psource:=panasonic-hotkey-kernel-source

# The short upstream name, used for the module source directory
sname:=panasonic-hotkey-kernel

# prefix of the target package name
PACKAGE=panasonic-hotkey-kernel
# modifieable for experiments or debugging m-a
MA_DIR ?= /usr/share/modass
MOD_SRCDIR=/usr/src
# load generic variable handling
-include $(MA_DIR)/include/generic.make
# load default rules, including kdist, kdist_image, ...
-include $(MA_DIR)/include/common-rules.make

# module assistant calculates all needed things for us and sets
# following variables:
# KSRC (kernel source directory), KVERS (kernel version string), KDREV
# (revision of the Debian kernel-image package), CC (the correct
# compiler), VERSION (the final package version string), PKGNAME (full
# package name with KVERS included), DEB_DESTDIR (path to store DEBs)

### KERNEL SETUP
### Setup the stuff needed for making kernel module packages
### taken from /usr/share/kernel-package/sample.module.rules

# Kernel ABI version (Ubuntu kernel package revision)
abi_version        = 26
kernel_version     = $(shell dpkg-parsechangelog | grep ^Source | sed 's/.*-//')
kernel_abi_version = $(kernel_version)-$(abi_version)

# This is where the magic happens.  We need to find all the variants of the
# kernel (386, 686, k7 for i386 architecture) for the target architecture.
#
# The '30' in the substr() call strips off everything but the kernel version
# from the auto-generated package name in debian/control (which comes from
# debian/control.in).
ifeq ($(DEB_HOST_ARCH), i386)
export karch=i386
endif 

ifeq ($(DEB_HOST_ARCH), amd64)
export karch=x86_64
endif

flavours = $(foreach flavor,$(shell cat debian/flavours.$(DEB_HOST_ARCH)),$(kernel_abi_version)-$(flavor))

LINUX_IMAGE_RECOMMENDS = $(shell echo '$(foreach flavor,$(flavours),linux-image-$(flavor) |)' | sed -e 's/ |$$//' )

builddir := $(CURDIR)/debian/build
rbuilddir := debian/build

# Toggle our generation behavior depending if we're running from
# module-assistant, or if we're running from the main build.
# 
# If module-assistant is installed, the line above:
#
# -include $(MA_DIR)/include/generic.make
#
# will set $(KVERS) to either 'unknown', or the target version of the kernel.
#
# NOTE: If module-assistant is NOT installed, the line above will not do
# anything, so $(KVERS) will be left unset.  The build daemon machines
# do not install build-dependencies before running debian/rules clean,
# so this is critical to get correct.
#
# TODO: This can all be replaced by having two debian/rules files; one
# for the main package, and a second for the module-assistant build from
# source.
ifeq ($(KVERS), unknown)
MAIN_PACKAGE_BUILD := 1
else
ifndef KVERS
MAIN_PACKAGE_BUILD := 1
else
# $(KVERS) is set, but is not 'unknown'.  We must be being called from m-a.
MODULE_ASSISTANT_BUILD := 1
endif
endif

# debian/control auto-generation
debian/control: 
	if [ "$(MAIN_PACKAGE_BUILD)" ]; then \
		echo '# NOTE: THIS FILE IS AUTO-GENERATED FROM control.in' > \
		  debian/control; \
		sed -e 's/@@KVERSION@@/$(kernel_version)/g' \
		    -e 's/@@ABIVER@@/$(kernel_abi_version)/g' \
		   debian/control.in >> debian/control ; \
	fi

# debian/control.modules.in autogeneration
debian/control.modules.in:
	echo '# NOTE: THIS FILE IS AUTO-GENERATED FROM control.modules.in.in' > \
	  debian/control.modules.in
	sed -e 's/@@KVERSION@@/$(kernel_version)/g' \
	    -e 's/@@ABIVER@@/$(kernel_abi_version)/g' \
	   debian/control.modules.in.in >> debian/control.modules.in

unpack: unpack-stamp
unpack-stamp:
	dh_testdir
	if [ -e $(builddir) ]; then \
		mv $(builddir) $(builddir).old; \
	fi
	mkdir $(builddir)
	for i in $(flavours); do \
		mkdir -p $(builddir)/$$i; \
		cp -a $(CURDIR)/pcc-acpi $(builddir)/$$i; \
		cp -a $(CURDIR)/pcc-cd   $(builddir)/$$i; \
	done
	touch unpack-stamp

build: build-kernel
build-kernel: unpack build-kernel-stamp
build-kernel-stamp: 
	dh_testdir
	for i in $(flavours); do \
		cd $(builddir)/$$i/pcc-acpi && $(MAKE) KVER=$$i; \
		cd $(builddir)/$$i/pcc-cd   && $(MAKE) KVER=$$i; \
	done
	touch build-kernel-stamp

###
# The kdist_configure target is called by make-kpkg modules_config and
# by kdist* rules by dependency. It should configure the module so it is
# ready for compilation (mostly useful for calling configure).
# prep-deb-files from module-assistant creates the neccessary debian/ files 
kdist_config: debian/control.modules.in prep-deb-files
kdist_configure: debian/control.modules.in prep-deb-files

# the kdist_clean target is called by make-kpkg modules_clean and from
# kdist* rules. It is responsible for cleaning up any changes that have
# been made by the other kdist_commands (except for the .deb files created)
kdist_clean: debian/control.modules.in clean prep-deb-files
	$(MAKE) $(MFLAGS) -f debian/rules clean
	cd pcc-acpi && $(MAKE) clean
	cd pcc-cd   && $(MAKE) clean
#
### end  KERNEL SETUP

# the binary-modules rule is invoked by module-assistant while processing the
# kdist* targets. It is called by module-assistant or make-kpkg and *not*
# during a normal build
binary-modules: debian/control.modules.in prep-deb-files clean
	dh_testroot
	dh_clean -k 
	dh_installdirs lib/modules/$(KVERS)/misc

	# Build the module
	cd pcc-acpi && $(MAKE) KVER=$(KVERS)
	cd pcc-cd   && $(MAKE) KVER=$(KVERS)

	# Install the modules
	cp pcc-acpi/pcc_acpi.ko debian/$(PACKAGE)-modules-$(KVERS)/lib/modules/$(KVERS)/kernel/drivers/acpi
	cp pcc-cd/pcc_acpi_cd.ko debian/$(PACKAGE)-modules-$(KVERS)/lib/modules/$(KVERS)/kernel/drivers/acpi

	dh_installdocs
	dh_installchangelogs
	dh_installmodules
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v$(VERSION)
	dh_md5sums
	dh_builddeb --destdir=$(DEB_DESTDIR)	
	dh_clean -k

clean: debian/control
	dh_testdir
	#dh_testroot
	rm -f build-arch-stamp build-indep-stamp unpack-stamp build-kernel-stamp
	rm -f install-stamp
	rm -rf debian/build*
	# The postinst/postrm files need to stick around in a m-a build.
	if [ $(MAIN_PACKAGE_BUILD) ]; then \
		rm -f debian/$(PACKAGE)-*.postinst; \
		rm -f debian/$(PACKAGE)-*.postrm; \
	fi
	dh_clean

install: build install-stamp
install-stamp: 
	dh_testdir
	dh_testroot
	dh_clean -k

	###
	### Source install
	###

	# Create the directories to install the source into
	dh_installdirs -p$(psource)  usr/src/modules/$(sname)/debian

	# Copy only the driver source to the proper location
	cp -a pcc-acpi pcc-cd debian/$(psource)/usr/src/modules/$(PACKAGE)
	# Copy the needed debian/ pieces to the proper location
	cp debian/*.modules.in* \
		debian/$(psource)/usr/src/modules/$(PACKAGE)/debian
	cp debian/*_KVERS_* \
		debian/$(psource)/usr/src/modules/$(PACKAGE)/debian
	cp debian/compat debian/rules debian/changelog debian/copyright \
		debian/$(psource)/usr/src/modules/$(PACKAGE)/debian/
	cd debian/$(psource)/usr/src && tar c modules | bzip2 -9 > panasonic-hotkey-kernel-source.tar.bz2 && rm -rf modules

	###
	### Binary install
	###

	for i in $(flavours); do \
		dh_installdirs -p$(PACKAGE)-modules-$(kernel_abi_version) lib/modules/$$i/misc; \
		cp debian/build/$$i/pcc-acpi/pcc_acpi.ko debian/$(PACKAGE)-modules-$(kernel_abi_version)/lib/modules/$$i/misc; \
		cp debian/build/$$i/pcc-cd/pcc_acpi_cd.ko debian/$(PACKAGE)-modules-$(kernel_abi_version)/lib/modules/$$i/misc; \
	done
	cp debian/$(PACKAGE)-modules-_KVERS_.postinst.modules.in \
	  debian/$(PACKAGE)-modules-$(kernel_abi_version).postinst ; \
	cp debian/$(PACKAGE)-modules-_KVERS_.postrm.modules.in \
	  debian/$(PACKAGE)-modules-$(kernel_abi_version).postrm ; \
	touch install-stamp

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs  -i
	dh_installdocs -i
	dh_installexamples -i
#	dh_install -i
#	dh_installmenu -i
#	dh_installdebconf -i
#	dh_installlogrotate -i
#	dh_installemacsen -i
#	dh_installpam -i
#	dh_installmime -i
#	dh_installinit -i
#	dh_installcron -i
#	dh_installinfo -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
#	dh_perl -i
#	dh_python -i
#	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -s
	dh_testroot -s
	dh_installchangelogs  -s
	dh_installdocs -s
	dh_installexamples -s
#	dh_install -s
#	dh_installmenu -s
#	dh_installdebconf -s
#	dh_installlogrotate -s
#	dh_installemacsen -s
#	dh_installpam -s
#	dh_installmime -s
#	dh_installinit -s
#	dh_installcron -s
#	dh_installinfo -s
	dh_installman -s
	dh_link -s
	dh_compress -s
	dh_fixperms -s
	dh_installdeb -s
#	dh_perl -s
#	dh_python -s
#	dh_makeshlibs -s
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s -- -Vlinuximagerecommends="$(LINUX_IMAGE_RECOMMENDS)"
	dh_md5sums -s
	dh_builddeb -s

binary: binary-indep binary-arch
.PHONY: clean binary-indep binary-arch binary install binary-modules kdist 
.PHONY: kdist_config kdist_image kdist_clean build-kernel unpack install-stamp
.PHONY: build

# Without this next line, debian/control won't be rebuilt each time.  Important!
.PHONY: debian/control debian/control.modules.in
