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

CFLAGS = -Wall -g


ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
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		= rlocate
# 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
# 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')

# 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)-module-$(non_epoch_version)
psource = $(package)-source

# RLOCATE_EXTRA_CFLAGS, module_DATA and KERNEL_LOCATION are passed to the 
# Makefile.kernel after it is renamed to Makefile
module_DATA = rlocate.o
KERNEL_LOCATION = $(KSRC)

RLOCATE_EXTRA_CFLAGS = -DEXPORT_SYMTAB $(DEFS) \
		       -DRL_VERSION=\"$(pversion)\" 
export RLOCATE_EXTRA_CFLAGS KERNEL_LOCATION module_DATA

CLEANFILES = $(module_DATA) .*.cmd  $(module_DATA:.o=.mod.c) $(module_DATA:.o=.ko)
# 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:
	@echo Nothing to configure

# 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-modules

# 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) -f debian/rules clean-modules

### end  KERNEL SETUP

configure: configure-stamp
configure-stamp: 
	dh_testdir
	# Add here commands to configure the package.
	./autogen.sh --exec_prefix=/usr \
		     --localstatedir=/var/lib \
		     --sysconfdir=/etc

	touch configure-stamp


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

	# Add here command to compile/build the package.
	$(MAKE) rlocate
	cd rlocate-daemon; $(MAKE)
	cd doc; $(MAKE)

	touch build-arch-stamp

RLOCATE_PATCHLEVEL=$(shell grep -s PATCHLEVEL ${KSRC}/Makefile |  head -n 1 | sed s/'PATCHLEVEL = '//)

# 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)/' \
	debian/control.in > debian/control

	dh_testdir
	dh_testroot
	dh_clean -k
	
	# Build and install the module
	if [ $(RLOCATE_PATCHLEVEL) = 4 ]; then                          \
		$(MAKE) only_modules_install KERNEL_SOURCES=$(KSRC)	\
			DESTDIR=$(CURDIR)/debian/$(pmodules) KVERS=$(KVERS);    \
	elif [ $(RLOCATE_PATCHLEVEL) = 6 ]; then                        \
		mv rlocate-module/Makefile rlocate-module/Makefile.automake; \
		cp rlocate-module/Makefile.kernel rlocate-module/Makefile; \
		$(MAKE) -C $(KSRC) SUBDIRS=$(CURDIR)/rlocate-module $(KPKG_EXTRAV_ARG)	\
			INSTALL_MOD_PATH=$(CURDIR)/debian/$(pmodules) modules modules_install;	\
		mv rlocate-module/Makefile.automake rlocate-module/Makefile; \
	fi

#	# Build the module
#	$(MAKE) modules HPATH=$(KSRC)/include
#
#	# Install the module
#	$(MAKE) only_modules_install DESTDIR=$(CURDIR)/debian/$(pmodules) KVERS=$(KVERS)
	mkdir -p $(CURDIR)/debian/$(pmodules)/usr/share/doc/$(pmodules)
	sed -e "s%#KERNEL_VERSION#%$(KVERS)%" \
		debian/$(package)-module.postinst.in >debian/$(package)-module.postinst

	dh_installdebconf
	dh_installchangelogs ChangeLog
	dh_installdocs 
	# We're not using this yet
	#dh_installmodules

	dh_installdeb
	dh_gencontrol -- -v$(epoch)$(pversion)+$(non_epoch_version)+$(KDREV)
	dh_compress
	#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.

	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
	rm -f config.log config.cache config.status Makefile
	dh_clean

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

	# Add here commands to clean up after the build process.
	cd rlocate-module;test -z "$(CLEANFILES)" || rm -f $(CLEANFILES)
	dh_clean


install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -
	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)/debian

	# Copy only the driver source to the proper location
	cp -a rlocate-module debian/$(psource)/usr/src/modules/$(package)
	install -m 644 debian/$(package)-source.override \
		$(CURDIR)/debian/$(psource)/usr/share/lintian/overrides/$(package)-source
	# Copy the needed debian/ pieces to the proper location
	cp debian/*-module.* \
		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 ChangeLog \
		debian/$(psource)/usr/src/modules/$(package)
	cp debian/copyright \
		debian/$(psource)/usr/src/modules/$(package)/debian
	cp debian/compat \
		debian/$(psource)/usr/src/modules/$(package)/debian
	# And finally, make a tarball from the module source.
	tar czf debian/$(psource)/usr/src/$(package).tar.gz \
		-C debian/$(psource)/usr/src modules
	rm -rf debian/$(psource)/usr/src/modules

	# install rlocate package
	#$(MAKE) install DESTDIR=`pwd`/debian/rlocate
	install -m 0755 -o root rlocate `pwd`/debian/rlocate/usr/bin
	install -m 0755 rlocate-daemon/rlocated `pwd`/debian/rlocate/usr/sbin
	sed -e 's,@RLOCATE_DB@,/var/lib/rlocate/rlocate.db,' \
	    -e 's,@RLOCATED_CMD@,/usr/sbin/rlocated,' \
		rlocate-scripts/rlocate-checkpoint > \
		rlocate-scripts/rlocate-checkpoint.temp
	mv rlocate-scripts/rlocate-checkpoint.temp \
	   rlocate-scripts/rlocate-checkpoint
	install -m 0755 -o root rlocate-scripts/rlocate-checkpoint \
			        `pwd`/debian/rlocate/usr/bin
	install -m 644 doc/man/rlocate.1 `pwd`/debian/rlocate/usr/share/man/man1/rlocate.1
	gzip -9 `pwd`/debian/rlocate/usr/share/man/man1/rlocate.1
	install -m 644 doc/man/updatedb.1 `pwd`/debian/rlocate/usr/share/man/man1/updatedb.1
	gzip -9 `pwd`/debian/rlocate/usr/share/man/man1/updatedb.1
	install -m 644 doc/man/rlocated.1 `pwd`/debian/rlocate/usr/share/man/man1/rlocated.1
	gzip -9 `pwd`/debian/rlocate/usr/share/man/man1/rlocated.1
	install -m 644 doc/man/rlocate-checkpoint.1 `pwd`/debian/rlocate/usr/share/man/man1/rlocate-checkpoint.1
	gzip -9 `pwd`/debian/rlocate/usr/share/man/man1/rlocate-checkpoint.1
	
	cp debian/rlocate.override $(CURDIR)/debian/rlocate/usr/share/lintian/overrides/rlocate

	dh_install

# 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 ChangeLog
	dh_installdocs -i debian/README.Debian doc/rlocate.html
#	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_installdebconf -s
	dh_installdocs -s 
	dh_installexamples -s
	dh_installmenu -s
#	dh_installlogrotate -s
#	dh_installemacsen -s
#	dh_installpam -s
#	dh_installmime -s
	dh_installinit -s --update-rcd-params="defaults 19"
	dh_installcron -s
#	dh_installman -s
	dh_installinfo -s
	dh_installchangelogs  -s ChangeLog
	dh_strip -s
	dh_link -s
	dh_compress -s
	dh_fixperms -s
#	dh_makeshlibs -s
	dh_installdeb -s
#	dh_perl -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

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