#!/usr/bin/make -f
# Derived from:
# Sample debian/rules that uses debhelper,
# GNU copyright 1997 by Joey Hess.
# Copyright Ron Lee 2003, 2004.

#export DH_VERBOSE=1

# Must be exported or the dh_* scripts will never see it.
export DH_OPTIONS

# Do this here instead of in a compat file so we don't need two of them,
# one for the main source package and one for the module binary package.
export DH_COMPAT=5


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


lw_tools  = linuxwacom
objdir_lw = $(lw_tools)/builddir

package_tools = wacom-tools
package_xorg  = xserver-xorg-input-wacom

### KERNEL SETUP
### Setup things needed for making kernel module packages,

# 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

ifeq ($(strip $(KSRC)),)
$(warning Warning.  KSRC unspecified, using /usr/src/linux for kernel source.)
# KSRC is the location of the kernel source. This is a default value,
# when make-kpkg et al. are used they should supply a real value.
KSRC		= /usr/src/linux
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?)
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)

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

# Prepares the package for distribution.  Intended for the kernel
# maintainer.
kdist: kdist_clean kdist_configure 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_configure:
	./configure 	--disable-libwacomxi	\
			--disable-wacomdrv	\
			--disable-libwacomcfg	\
			--disable-wacdump	\
			--disable-xidump	\
			--disable-xsetwacom	\
			--enable-wacom		\
			--with-kernel=$(KSRC)


# 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 kdist_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: clean-common
	dh_testdir
	dh_testroot

	-$(MAKE) clean;
	rm -f debian/$(pmodules).* debian/control


debian/control:
	sed -e 's/$${kpkg\:Kernel\-Version}/$(non_epoch_version)/' \
	debian/wacom.control > debian/control


# the binary-modules target prepares the $(pmodules) package.
# It is called by make-kpkg and *not* during a normal build
binary-modules: DH_OPTIONS=-p$(pmodules)
binary-modules: debian/control
	# 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.
	# We do this above instead so control exists when the clean targets
	# are run.
	#sed -e 's/$${kpkg\:Kernel\-Version}/$(non_epoch_version)/' \
	#debian/cpad.control > debian/control

	# Make sure we've had a chance to get a control file before doing this:
	dh_testdir
	dh_testroot
	dh_clean -k
	
	# Set up per package control files with suitable names
	for f in modules templates; do						\
	    cp -a debian/wacom.$$f debian/$(pmodules).$$f;			\
	done
	for f in postinst config; do						\
	    echo "#!/bin/sh" | cat - debian/wacom.$$f > debian/$(pmodules).$$f;	\
	done

	# Build the modules
	$(MAKE)

	# It's hard to be sure where the modules are with the upstream
	# build system, so 'install' them in a temp dir for the moment.
	
	mkdir -p temp
	find src/2.4.* -name "*.o" -exec cp {} temp \;
	find src/2.6.* -name "*.ko" -exec cp {} temp \;

	# Install the modules
	if [ -e temp/wacom.o ]; then							\
		dh_install temp/wacom.o							\
			   lib/modules/$(non_epoch_version)/kernel/drivers/usb;		\
	else										\
		dh_install temp/wacom.ko						\
			   lib/modules/$(non_epoch_version)/kernel/drivers/usb/input;	\
	fi
	$(RM) -r temp

	# XXX We still need this for 2.4
	#dh_installmodules

	dh_installdebconf
	dh_installchangelogs
	dh_installdocs
	dh_compress
	dh_fixperms

	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)

### end  KERNEL SETUP


config-arch: config-arch-stamp
config-arch-stamp:
	dh_testdir
	mkdir -p $(objdir_lw)
	cd $(objdir_lw) &&						\
		../configure 	--prefix=/usr				\
				--with-xmoduledir=/usr/lib/xorg/modules/input \
				--disable-wacom				\
				--disable-libwacomxi
	touch $@


build-arch: config-arch build-arch-stamp
build-arch-stamp:
	dh_testdir
	cd $(objdir_lw) && $(MAKE)
	touch $@

build-indep:  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/cpad.sgml > cpad.1

	po2debconf debian/wacom.po-templates > debian/wacom.templates

	touch $@

build: build-indep build-arch

clean: clean-common
	dh_testdir
	dh_testroot
	#-cd $(objdir_lw) && $(MAKE) clean
	$(RM) -r $(objdir_lw)

	# Ensure po files are up to date for the source package.
	debconf-updatepo

clean-common: debian/control
	dh_testdir
	dh_testroot
	rm -f *-stamp
	dh_clean


install: build install-indep install-arch

install-arch: install-wacom-tools install-wacom-drv

install-wacom-tools: DH_OPTIONS=-p$(package_tools)
install-wacom-tools:
	dh_testdir
	dh_testroot
	dh_clean -k

	cd $(objdir_lw) && $(MAKE) install DESTDIR=$(shell pwd)/debian/$(package_tools)

	# Remove these for the moment, they belong in
	# a -dev package but nothing needs it yet.
	$(RM) -r debian/$(package_tools)/usr/include
	$(RM) -r debian/$(package_tools)/usr/lib/libwacomcfg.{a,la,so}

	dh_installdirs usr/share/doc/$(package_tools)
	dh_installman debian/wacom-tools.1
	dh_link usr/share/man/man1/wacom-tools.1 usr/share/man/man1/wacdump.1 \
	        usr/share/man/man1/wacom-tools.1 usr/share/man/man1/xidump.1  \
	        usr/share/man/man1/wacom-tools.1 usr/share/man/man1/xsetwacom.1

	dh_installdirs usr/share/lintian/overrides
	cp debian/lintian-overrides \
	   debian/$(package_tools)/usr/share/lintian/overrides/$(package_tools)


install-wacom-drv: DH_OPTIONS=-p$(package_xorg)
install-wacom-drv:
	dh_testdir
	dh_testroot
	dh_clean -k

	dh_install debian/$(package_tools)/usr/lib/xorg/modules usr/lib/xorg
	$(RM) -r debian/$(package_tools)/usr/lib/xorg

	chmod 755 debian/check_driver
	dh_install debian/check_driver	lib/udev
	dh_installman debian/check_driver.1

	dh_installudev

	dh_installdirs usr/share/lintian/overrides
	cp debian/lintian-overrides \
	   debian/$(package_xorg)/usr/share/lintian/overrides/$(package_xorg)


install-indep: install-source

install-source: DH_OPTIONS=-p$(psource)
install-source:
	dh_testdir
	dh_testroot
	dh_clean -k

	# Copy the driver source
	dh_install -Xbuilddir $(lw_tools)/*  usr/src/modules/wacom
	# Remove some things not needed for the kernel module.
	# Unfortunately, its still bound to the autoconf setup.
	$(RM) debian/$(psource)/usr/src/modules/wacom/src/wacomxi/wacomcpl

	# Copy the needed debian/ pieces
	dh_install debian/rules 				\
		   debian/changelog 				\
		   debian/copyright 				\
		   debian/wacom.config	 			\
		   debian/wacom.control	 			\
		   debian/wacom.modules				\
		   debian/wacom.postinst 			\
		   debian/wacom.templates			\
		   usr/src/modules/wacom/debian

	dh_installdebconf


# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build-indep install-indep
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installchangelogs  -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-arch install-arch
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installcron -a
	dh_installinfo -a
	dh_installchangelogs  -a
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a -L $(package_tools) -l debian/$(package_tools)/usr/lib
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

# No binary arch for this one.
binary: binary-indep binary-arch

.PHONY: build clean clean-common binary-indep binary-arch binary	\
	install install-arch install-indep				\
	install-wacom-tools install-wacom-drv				\
	install-source binary-modules					\
	kdist kdist_configure kdist_image kdist_clean

