#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

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

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

# Name of package
package         = zaptel
# 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 )

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

# 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

# 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

### end  KERNEL SETUP


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

configure: configure-stamp
configure-stamp:
	dh_testdir

	touch configure-stamp

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

	$(MAKE) programs

	touch build-stamp

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

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

	dh_clean

# 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)'

	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 the module
	$(MAKE) modules

	# Install the module
	$(MAKE) install-modules DESTDIR=$(CURDIR)/debian/$(pmodules)

	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=$(MODDIR)

install: install-arch install-indep

install-arch: build
	dh_testdir
	dh_testroot
	dh_clean -k -a
	dh_installdirs -a

	# Add here commands to install the package into debian/tmp

	$(MAKE) install-programs DESTDIR=debian/tmp INSTALL_PREFIX=/usr

	install -d debian/tmp/etc
	install -m 644 zaptel.conf.sample debian/tmp/etc/zaptel.conf

	dh_movefiles -a

install-indep:
	dh_testdir
	dh_testroot
	dh_clean -k -i
	dh_installdirs -i

	# Build module source package
	mkdir -p debian/tmp/usr/src/modules/$(package)/debian

	# driver source code
	cp Makefile *.c *.h *.rbt debian/tmp/usr/src/modules/$(package)

	# Packaging infrastructure
	cp debian/*-modules.* debian/rules debian/changelog debian/copyright \
		debian/tmp/usr/src/modules/$(package)/debian
	cp debian/control.modules.in \
		debian/tmp/usr/src/modules/$(package)/debian/control.in

	dh_movefiles -i

# Build architecture-independent files here.
binary-indep: build install-indep
	dh_testdir
	dh_testroot

	dh_installdocs -i
	dh_installchangelogs -i ChangeLog
	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-arch
	dh_testdir
	dh_testroot

	dh_installdocs -a
	dh_installexamples -a
	dh_installmodules -a
	dh_installchangelogs -a ChangeLog
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -V
	dh_installdeb -a
	dh_shlibdeps -a -ldebian/libtonezone1/usr/lib
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
