#!/usr/bin/make -f
# This is fairly complicated since it can be called either to build the
# source package, or to build a kernel module package.

#export DH_VERBOSE=1

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

# Strip epoch, which is at the _end_ of KDREV
baseepoch=$(shell expr $(KDREV) : '\([0-9]*\):.*' 2>/dev/null )
ifneq ($(baseepoch),)
EPOCH=$(baseepoch):
endif
REV=$(shell expr $(KDREV) : '[0-9]*:\(.*\)' 2>/dev/null)
ifeq ($(REV),)
REV=$(KDREV)
endif

# If still undefined, set to 0
ifeq ($(REV),)
REV=0
endif

VERSION=$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
KSRC:=/usr/src/linux
KVERS:=$(shell uname -r)
PKG_VERSION=$(EPOCH)$(VERSION)+$(REV)

ARCH = $(shell dpkg --print-architecture)

TIMODULES_DOC = $(CURDIR)/debian/tidev-modules-$(KVERS)/usr/share/doc/tidev-modules-$(KVERS)

# Build kernel modules.
build:
	dh_testdir

	cd tipar_dev/module && $(MAKE) KDIR=$(KSRC) && cd -
	cd tiser_dev/module && $(MAKE) KDIR=$(KSRC) && cd -
	cd tiusb_dev/module && $(MAKE) KDIR=$(KSRC) && cd -

clean:
	dh_testdir
	dh_testroot
	cd tipar_dev/module && $(MAKE) clean && cd -
	cd tiser_dev/module && $(MAKE) clean && cd -
	cd tiusb_dev/module && $(MAKE) clean && cd -
	-cp -f debian/control.template debian/control
	-cp -f debian/tidev-modules-KVERS.postinst.template debian/tidev-modules-$(KVERS).postinst
	-cp -f debian/tidev-modules-KVERS.postrm.template debian/tidev-modules-$(KVERS).postrm
	dh_clean debian/tidev-modules-$(KVERS).*

# Build the kernel module .deb.
binary-modules: build
	dh_clean -k
	dh_testdir
	dh_testroot

	sed -e 's/#KVERS#/$(KVERS)/g' -e 's/#ARCH#/$(ARCH)/g' debian/control.template > debian/control

	sed -e 's/#KVERS#/$(KVERS)/g' debian/tidev-modules-KVERS.postinst.template > debian/tidev-modules-$(KVERS).postinst
	sed -e 's/#KVERS#/$(KVERS)/g' debian/tidev-modules-KVERS.postrm.template > debian/tidev-modules-$(KVERS).postrm

	# And set up files so debhelper can find them.

	dh_installdirs lib/modules/$(KVERS)/misc
	dh_installdirs usr/share/doc/tidev-modules-$(KVERS)

	cd tipar_dev/module && $(MAKE) install KDIR=$(KSRC) KVERS=$(KVERS) DESTDIR=$(CURDIR)/debian/tidev-modules-$(KVERS)
	cd tiser_dev/module && $(MAKE) install KDIR=$(KSRC) KVERS=$(KVERS) DESTDIR=$(CURDIR)/debian/tidev-modules-$(KVERS)
	cd tiusb_dev/module && $(MAKE) install KDIR=$(KSRC) KVERS=$(KVERS) DESTDIR=$(CURDIR)/debian/tidev-modules-$(KVERS)

	mkdir -p $(TIMODULES_DOC)/tipar_dev
	mkdir -p $(TIMODULES_DOC)/tiser_dev
	mkdir -p $(TIMODULES_DOC)/tiusb_dev

	cp -f tipar_dev/README $(TIMODULES_DOC)/tipar_dev
	cp -f tipar_dev/CHANGELOG $(TIMODULES_DOC)/tipar_dev
	cp -f tipar_dev/devices.sh $(TIMODULES_DOC)/tipar_dev
	cp -f tipar_dev/module/Insmod-options $(TIMODULES_DOC)/tipar_dev 

	cp -f tiser_dev/README $(TIMODULES_DOC)/tiser_dev
	cp -f tiser_dev/CHANGELOG $(TIMODULES_DOC)/tiser_dev
	cp -f tiser_dev/devices.sh $(TIMODULES_DOC)/tiser_dev
	cp -f tiser_dev/module/Insmod-options $(TIMODULES_DOC)/tiser_dev 


	cp -f tiusb_dev/README $(TIMODULES_DOC)/tiusb_dev
	cp -f tiusb_dev/CHANGELOG $(TIMODULES_DOC)/tiusb_dev
	cp -f tiusb_dev/devices.sh $(TIMODULES_DOC)/tiusb_dev
	cp -f tiusb_dev/module/Insmod-options $(TIMODULES_DOC)/tiusb_dev 

	dh_installdocs
	dh_installexamples
	dh_installchangelogs
	dh_installmodules
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- -v$(PKG_VERSION)
	dh_makeshlibs
	dh_md5sums
	dh_builddeb --destdir=$(KSRC)/..

# Build architecture-independent files here.
binary-indep:
	# Nothing to do.

# Build architecture-dependant files that arn't kernel modules here.
binary-arch:
	# Nothing to do.

# This is used when kernel-package is building the package.
kdist_image: binary-modules clean
	# The dependancies take care of building the module package.
kdist: clean binary-modules
	# Build a .changes file.
	KVERS="$(KVERS)" KSRC="$(KSRC)" KMAINT="$(KMAINT)" \
	REV="$(REV)" KEMAIL="$(KEMAIL)" \
		sh -v debian/genchanges.sh
	# Only now can we clean up.
	debian/rules clean
kdist_configure:
	# Nothing to do when configured.
kdist_clean: clean

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary kdist kdist_image kdist_configure
