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

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

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

# 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
VERSION=$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
KSRC:=/usr/src/linux

# Build everything except kernel modules.
build: build-stamp
build-stamp:
	dh_testdir
	./configure --prefix=/ --mandir=share/man
	$(MAKE)
	touch build-stamp

# Build kernel modules.
build-modules:
	dh_testdir
	cd src && \
	$(CC) -m486 -O2 -Wall -D__KERNEL__ -DKERNEL -DMODULE -c toshiba.c \
		-o toshiba.o -I $(KSRC)/include

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	-$(MAKE) distclean
	dh_clean src/toshiba.o src/Makefile

# Build architecture-independent files here.
binary-indep: build
# We have nothing to do by default.

# Build the kernel module .deb.
binary-modules: build-modules
	# We have to hack the control file first.
	sed 's/#KVERS#/$(KVERS)/g' debian/control.modules > debian/control
	# And set up files so debhelper can find them.
	ln -sf toshiba.modules toshiba-$(KVERS).modules

	dh_clean -k
	dh_testdir
	dh_testroot
	dh_installdirs lib/modules/$(KVERS)/misc
	install -m 0644 src/toshiba.o debian/tmp/lib/modules/$(KVERS)/misc
	dh_installdocs
	dh_installchangelogs
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -u"-v$(EPOCH)$(VERSION)+$(REV)"
	dh_md5sums
	dh_builddeb --destdir=$(KSRC)/..

# Build architecture-dependent files that arn't kernel modules here.
binary-arch: build
#	dh_testversion
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs usr/share/man/man1 usr/share/man/man8 usr/bin \
		usr/X11R6/include/X11/pixmaps \
		usr/src/modules/toshutils/src \
		usr/src/modules/toshutils/debian
		
	cp pixmaps/fan*.xpm pixmaps/displayswitch.xpm pixmaps/tuxtime.xpm \
		debian/tmp/usr/X11R6/include/X11/pixmaps
	$(MAKE) DESTDIR=$(shell pwd)/debian/tmp/usr install
	# Copy in the few files necessary to build the kernel module.
	cp src/toshiba.c src/toshiba.h \
		debian/tmp/usr/src/modules/toshutils/src
	cp -a debian/rules debian/*.modules debian/changelog \
		debian/copyright debian/control debian/genchanges.sh \
		debian/tmp/usr/src/modules/toshutils/debian
	dh_installdocs README* TODO CONTRIBUTE 
	dh_installexamples
	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installinit
	dh_installcron
#	dh_installmanpages
	dh_installinfo
	dh_installchangelogs ChangeLog
	dh_installmodules
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_suidregister
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

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

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

