#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that can build a kernel modules
# architecture-dependant package via make-kpkg, as well as an
# architecture-independent module source package, and other packages
# either dep/indep for things like common files or userspace components
# needed for the kernel modules.

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=3


ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

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

# Name of package
package		= syscalltrack
# KSRC is the location of the kernel source. This is the default value,
# when make-kpkg is used it will supply to real value
KSRC		= /usr/src/linux
export KERNEL_DIR = $(KSRC)
# KDREV is the package-revision, as given to make-kpkg by the user.
# Just put a simply default value in here which we use when we test
# the packagebuilding without make-kpkg
ifeq ($(strip $(KDREV)),)
KDREV		= "test1.0"
endif

## Now to determine the kernel version, normally supplied by make-kpkg
ifeq ($(strip $(KVERS)),)
# Now we need to get the kernel-version somehow (we are not running
# under make-kpkg?)
ifeq ($(strip $(KSRC)),)
$(error Error. I do not know how to determine the kernel version)
else
kversion :=$(shell egrep '^VERSION +=' $(KSRC)/Makefile 2>/dev/null | \
                 sed -e 's/[^0-9]*\([0-9]*\)/\1/')
kplevel  :=$(shell egrep '^PATCHLEVEL +=' $(KSRC)/Makefile 2>/dev/null | \
                    sed -e 's/[^0-9]*\([0-9]*\)/\1/')
ksublevel:=$(shell egrep '^SUBLEVEL +=' $(KSRC)/Makefile 2>/dev/null | \
                  sed -e 's/[^0-9]*\([0-9]*\)/\1/')
EXTRA_VERSION:=$(shell egrep '^EXTRAVERSION +=' $(KSRC)/Makefile 2>/dev/null | \
                 sed -e 's/EXTRAVERSION[\t ]*=[\t ]*\(.*\)/\1/')
kextra:=$(strip $(EXTRA_VERSION))
HAVE_NEW_MODLIB:=$(shell egrep '\(INSTALL_MOD_PATH\)' \
                            $(KSRC)/Makefile 2>/dev/null )

# If you want to have more than one kernel configuration per kernel
# version, set FLAVOUR in the top level kernel Makefile *before*
# invoking make-kpkg -- it will be appended to UTS_RELEASE in
# version.h (separated by a hyphen). This affects everything -- the
# names and versions of the image, source, headers, and doc packages,
# and where the modules are searched for in /lib/modules.

ifdef FLAVOUR
# uhm - should check if we really have a Makefile capable of Flavours?
endif

FLAVOUR:=$(shell grep ^FLAVOUR $(KSRC)/Makefile 2>/dev/null | \
                  perl -ple 's/FLAVOUR[\s:=]+//g')

ifneq ($(strip $(FLAVOUR)),)
INT_FLAV := -$(FLAVOUR)
FLAV_ARG := FLAVOUR=$(FLAVOUR)
else
INT_FLAV :=
FLAV_ARG :=
endif

## This is the replacement for FLAVOUR
ifneq ($(strip $(APPEND_TO_VERSION)),)
iatv := $(strip $(APPEND_TO_VERSION))
EXTRAV_ARG := EXTRAVERSION=${EXTRA_VERSION}${iatv}
else
iatv :=
EXTRAV_ARG :=
endif

KVERS = $(kversion).$(kplevel).$(ksublevel)$(kextra)$(iatv)$(INT_FLAV)

endif
endif

non_epoch_version=$(shell echo $(KVERS) | perl -pe 's/^\d+://')
epoch=$(shell echo $(KVERS) | perl -ne 'm/^(\d+:)/ && print $$1')
main_version=$(shell echo $(KVERS) | perl -ne 'm/^(\d+\.\d+)\./ && print $$1')

# We also need the package version
pversion	= $(shell sed -ne '1s/.*\((.*)\).*/\1/' debian/changelog)
pversion	= $(shell sed -ne '1s/.*(\(.*\)).*/\1/p' debian/changelog)

# MODDIR is the place where the final .deb package should be made. This is the
# default value, when make-kpkg is used it will supply to real value
MODDIR		= ..

pmodules = $(package)-modules-$(non_epoch_version)
psource = $(package)-source
sbin_dir = debian/$(package)/usr/sbin
doc_dir = debian/$(package)/usr/share/doc/$(package)

# Prepares the package for distribution.  Intended for the kernel
# maintainer.
kdist: kdist_clean kdist_config kdist_image

# The kdist_configure target is called by make-kpkg modules_config. It
# should configure the module so it is ready for compilation (mostly
# useful for calling configure)
kdist_config:
	# Link to the correct file
	ln -sf Makefile-$(main_version) Makefile

# the kdist_image target is called by make-kpkg modules_image. It is
# responsible for compiling the module and creating the package. It
# should also clean up after making the module. Please note we use a
# seperate binary-modules target to make testing the package building
# easier
kdist_image:
	$(MAKE) $(MFLAGS) -f debian/rules binary-modules
	$(MAKE) $(MFLAGS) -f debian/rules clean

# the kdist_clean target is called by make-kpkg modules_clean. 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:
	-$(MAKE) $(MFLAGS) debian/rules clean
	-rm -f Makefile

### end  KERNEL SETUP

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.

	touch configure-stamp


build-arch: configure-stamp  build-arch-stamp
build-arch-stamp:
	dh_testdir

	# Add here command to compile/build the package.
	$(MAKE)

	touch build-arch-stamp

# the binary-modules target prepares the $(pmodules) package.
# It is called by make-kpkg and *not* during a normal build
binary-modules:
	export DH_OPTIONS='-p$(pmodules)'

	# Is this needed for setting up a Depends?
	#echo "kpkg:Package-Version=$(epoch)$(pversion)+$(non_epoch_version)" \
	#	 >> debian/$(package).substvars
	echo "kpkg:Kernel-Version=$(non_epoch_version)" >> \
		debian/$(pmodules).substvars

	# The substvars mechanism seems slightly broken, hack to get around it
	# stolen from the qce-ga package. Yaaaa!
	sed -e 's/$${kpkg\:Kernel\-Version}/$(non_epoch_version)/; s/$${kpkg\:Kernel\-Package\-Version}/${KDREV}/' \
		debian/control.in > debian/control

	dh_testdir
	dh_testroot
	dh_clean -k
	
	# Link to the correct file
	rm -f Makefile
	ln -sf Makefile-$(main_version) Makefile

	# Build the module
	$(MAKE)

	# Install the module
	install -d $(CURDIR)/debian/$(pmodules)/lib/modules/$(KVERS)/kernel/sct
	install -m 0644 sct_rules.o $(CURDIR)/debian/$(pmodules)/lib/modules/$(KVERS)/kernel/sct/sct_rules.o
	install -m 0644 syscall_hijack.o $(CURDIR)/debian/$(pmodules)/lib/modules/$(KVERS)/kernel/sct/syscall_hijack.o

	# Install extra files
	install -m 0644 syscalls.dat $(CURDIR)/debian/$(pmodules)/lib/modules/$(KVERS)/kernel/sct/syscalls.dat

	dh_installdebconf
	# FIXME dh_installdocs README
	dh_installchangelogs

	# We're not using this yet
	#dh_installmodules

	dh_installdeb
	#dh_gencontrol -- -v$(epoch)$(pversion)+$(non_epoch_version)+$(KDREV)
	dh_gencontrol -- -v$(epoch)$(pversion)
	dh_md5sums
	#dh_builddeb --destdir=$(KSRC)/..
	dh_builddeb --destdir=$(MODDIR)


build-indep:  configure-stamp build-indep-stamp
build-indep-stamp:
	dh_testdir

	# Add here command to compile/build the arch indep package.
	# It's ok not to do anything here, if you don't need to build
	#  anything for this package.
	#/usr/bin/docbook-to-man debian/syscalltrack.sgml > syscalltrack.1

	touch build-indep-stamp

build: build-arch build-indep

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp

	# Add here commands to clean up after the build process.
	-$(MAKE) clean

	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

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

	# Copy only the driver source to the proper location
	cp -a sct_rules_module/*  debian/$(psource)/usr/src/modules/$(package)
	# Copy additional headers needed by the driver
	cp -a sct_rules/*.h sct_rules/*.c \
		debian/$(psource)/usr/src/modules/$(package)/sct_rules
	
	# Copy the needed debian/ pieces to the proper location
	cp debian/*-modules.* \
		debian/$(psource)/usr/src/modules/$(package)/debian
	cp debian/control.modules.in \
		debian/$(psource)/usr/src/modules/$(package)/debian/control.in
	cp debian/rules \
		debian/$(psource)/usr/src/modules/$(package)/debian
	cp debian/changelog \
		debian/$(psource)/usr/src/modules/$(package)/debian
	cp debian/copyright \
		debian/$(psource)/usr/src/modules/$(package)/debian

	# Add here commands to install the package into debian/syscalltrack.
	install -m 0750 sct_config/sct_config $(sbin_dir)/sct_config
	install -m 0750 sct_rules/tracker_ctrl $(sbin_dir)/tracker_ctrl
	install -m 0750 sct_rules_module/sct_load $(sbin_dir)/sct_load
	install -m 0750 sct_rules_module/sct_unload $(sbin_dir)/sct_unload
	
	install -m 644 doc/*.html doc/*.txt doc/FUTURES doc/Version-1.0-features \
		$(doc_dir)/help/
	install -m 644 sct_config/sct_example.conf $(doc_dir)

# 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_installdocs -i
#	dh_installman -i
#	dh_installinfo -i
#	dh_undocumented -i
	dh_installchangelogs ChangeLog -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installman -a debian/sct_load.1 debian/sct_unload.1 debian/sct_config.1
#	dh_installinfo -a
	dh_undocumented -a tracker_ctrl.1
	dh_installchangelogs ChangeLog -a
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure binary-modules kdist kdist_config kdist_image kdist_clean
