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

# 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
ARCH := $(shell dpkg --print-architecture)
MODULE_LOC ?= /usr/src/modules
PKG_VERSION := $(EPOCH)$(VERSION)+$(REV)
MIN_I2C_VERSION := 2.8.0
CHFILE := $(KSRC)/../lm-sensors-$(KVERS)_$(PKG_VERSION)_$(ARCH).changes
I2CORIGIN := $(shell perl debian/i2ccheck.pl $(KSRC) $(MODULE_LOC)/i2c $(MIN_I2C_VERSION))
ifeq ($(I2CORIGIN),i2c)
I2CFLAGS := I2C_HEADERS=$(MODULE_LOC)/i2c
I2CFIXUP := /^Suggests:/d
else
I2CFLAGS :=
I2CFIXUP := /^Depends:/s/, i2c-.*//
endif

# Man pages to be generated from DocBook source
DOCBOOK_8_PAGES=i2cdump i2cset isadump

# Kernel versions to automatically build
#AUTO_KERNELS=2.4.22-1
AUTO_KERNELS=
# And some magic:
KERNEL_BUILD_DIRS=$(foreach ver,$(AUTO_KERNELS),$(wildcard /usr/src/kernel-build-$(ver)/*))
SLASH_FLAVORS=$(KERNEL_BUILD_DIRS:/usr/src/kernel-build-%=%)
AUTO_FLAVORS=$(subst /,-,$(SLASH_FLAVORS))

sanity-check:
	@if [ -z "$(KSRC)" ]; then \
		echo "KSRC is unset; please set it at the command line" >&2; \
		exit 1; \
	elif [ ! -d "$(KSRC)/include" ]; then \
		echo "KSRC is $(KSRC), but" >&2; \
		echo "doesn't appear to include an include directory." >&2; \
		echo "Please set KSRC to point to a Linux kernel source" >&2; \
		echo "tree at the command line." >&2; \
		exit 1; \
	elif [ -z "$(KVERS)" ]; then \
		echo "KVERS is unset; please set it at the command line" >&2; \
		exit 1; \
	elif [ "$(I2CORIGIN)" = error ]; then \
		echo "You don't appear to have i2c header files" >&2; \
		echo "available on your system.  You must have i2c" >&2; \
		echo "header files installed under $(MODULE_LOC)/i2c." >&2; \
		exit 1; \
	fi

debian/stamp-control:
	cp debian/control.base debian/control
	for flavor in $(AUTO_FLAVORS); do \
		sed -e "s/#KVERS#/$$flavor/g" -e '/^Suggests:/d' \
			debian/control.per-mod >> debian/control; \
	done
	touch $@

# Build everything that goes into the Debian package.  Use recursive make
# invocations to build all of the interesting components.
build: debian/stamp-control debian/stamp-build \
	$(AUTO_FLAVORS:%=debian/stamp-module-%)

# Go off and build a particular module.  $* will be of the form
# 2.4.20-1-686-smp, where 2.4.20-1 is in AUTO_KERNELS and 686-smp will be
# in AUTO_FLAVORS.  For now, /usr/src/kernel-build/$KERNEL is just a
# link to a kernel-headers directory, so use that; if it ever becomes
# different, we need to figure out where the right hyphen to turn
# into a slash is, which is hard.
debian/stamp-module-%:
	$(MAKE) -f debian/rules build-a-module KSRC=/usr/src/kernel-headers-$* KVERS=$*
	touch $@

debian/stamp-modules:
	dh_testdir
	tar xzf /usr/src/i2c.tar.gz -C debian
	touch $@

debian/stamp-build: debian/stamp-modules
	dh_testdir
	$(MAKE) user \
		I2C_HEADERS=`pwd`/debian/modules/i2c \
		PROG_EXTRA=sensord
	for i in $(DOCBOOK_8_PAGES); do \
		docbook-to-man debian/$$i.sgml > debian/$$i.8; \
	done
	touch $@

build-a-module: debian/stamp-modules
	dh_testdir
	mkdir -p debian/build/$(KVERS)
	find . -path ./debian -prune -o -print | \
		cpio -admp debian/build/$(KVERS)
	$(MAKE) -C debian/build/$(KVERS) \
		all-kernel all-kernel-busses all-kernel-chips \
		LINUX=$(KSRC) I2C_HEADERS=`pwd`/debian/modules/i2c \
		SRCDIRS="kernel kernel/busses kernel/chips kernel/include"

# Build kernel modules.
build-modules: sanity-check
	dh_testdir
	$(MAKE) all-kernel all-kernel-busses all-kernel-chips \
		LINUX=$(KSRC) $(I2CFLAGS) \
		SRCDIRS="kernel kernel/busses kernel/chips kernel/include"

# For people allergic to kernel-package, build and install modules.
modules-nokpkg: build-modules
	$(MAKE) install-kernel install-kernel-busses install-kernel-chips \
		LINUX=$(KSRC) $(I2CFLAGS) \
		SRCDIRS="kernel kernel/busses kernel/chips kernel/include"

clean:
	dh_testdir
	dh_testroot
	$(MAKE) clean LINUX=$(KSRC) PROG_EXTRA=sensord
	dh_clean debian/lm-sensors-$(KVERS).*
	rm -rf debian/modules debian/stamp-modules debian/build
	rm -f $(DOCBOOK_8_PAGES:%=debian/%.8)
	cp debian/control.base debian/control
	rm -f debian/stamp-control debian/stamp-build
	rm -f $(AUTO_FLAVORS:%=debian/stamp-module-%)

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

	sed -e 's/#KVERS#/$(KVERS)/g' -e 's/#KDREV#/$(KDREV)/' \
		-e '$(I2CFIXUP)' \
		debian/control.modules debian/control.per-mod > debian/control
	ln -sf lm-sensors-mod.README.Debian debian/lm-sensors-$(KVERS).README.Debian

	dh_installdirs lib/modules/$(KVERS)/lm-sensors
	find -name \*.o | grep /kernel/ | egrep -v 'RPM|CVS' | \
		xargs -i install -m 0644 {} debian/lm-sensors-$(KVERS)/lib/modules/$(KVERS)/lm-sensors
	dh_installdocs
	dh_installexamples
	dh_installchangelogs CHANGES
	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.
# In particular, this is the lm-sensors-source package-with-tarball.
binary-indep:
	dh_clean -i -k
	dh_testdir -i
	dh_testroot -i
	dh_installdirs -i

	# Copy this whole source tree into the temp directory and clean it.
	find . -path ./debian -prune -o -print | \
		cpio -admp debian/tmp/usr/src/modules/lm-sensors
	find debian -path debian -o -type d -prune -o -print | \
		cpio -admp debian/tmp/usr/src/modules/lm-sensors
	find debian/tmp/usr/src -name CVS -type d | \
		xargs rm -rf
	cd debian/tmp/usr/src/modules/lm-sensors; \
		$(MAKE) -f debian/rules clean

	# Tar up stuff under /usr/src/modules into a tarball.
	cd debian/tmp/usr/src; \
		tar czf lm-sensors.tar.gz modules; \
		rm -rf modules

	dh_movefiles -i
#	dh_link -i
	dh_installdocs -i
#	dh_installexamples -i
	dh_installdebconf -i
	dh_installchangelogs -i CHANGES
	dh_compress -i
	dh_fixperms -i

	dh_perl -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependant files that arn't kernel modules here.
binary-arch: build
	dh_clean -a -k
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a

	$(MAKE) user_install DESTDIR=`pwd`/debian/tmp PREFIX=/usr \
		I2C_HEADERS=`pwd`/debian/modules/i2c PROG_EXTRA=sensord
	install -m 0755 prog/detect/sensors-detect debian/tmp/usr/sbin
	install -m 0755 prog/sensord/sensord debian/tmp/usr/sbin
	install -m 0755 prog/dump/i2cdump debian/tmp/usr/sbin
	install -m 0755 prog/dump/i2cset debian/tmp/usr/sbin
	install -m 0755 prog/dump/isadump debian/tmp/usr/sbin

	for flavor in $(AUTO_FLAVORS); do \
		mkdir -p debian/lm-sensors-$$flavor/lib/modules/$$flavor/lm-sensors ; \
		find debian/build/$$flavor/kernel -name \*.o -print | \
		xargs -i install -m 0644 {} debian/lm-sensors-$$flavor/lib/modules/$$flavor/lm-sensors ; \
	done

	# Install extra files as needed
	install -m 0644 debian/sensord.logcheck debian/sensord/etc/logcheck/ignore.d.server/sensord
	install -m 0644 debian/sensord.logcheck debian/sensord/etc/logcheck/ignore.d.workstation/sensord

	dh_movefiles -a
	dh_link -a
	dh_installdocs -a -XCVS
	dh_installexamples -a -XCVS
	dh_installdebconf -a
	dh_installmenu -a
	dh_installcron -a
	# Should start after modutils (20)
	dh_installinit -psensord -- defaults 21
	dh_installmodules -a
	dh_installman -a
	dh_installchangelogs -a CHANGES
	dh_strip -a
	dh_compress -a
	dh_fixperms -a

	dh_perl -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

# This is used when kernel-package is building the package.
kdist_image:
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules binary-modules
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

kdist:
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules binary-modules
	# Build a .changes file.
	dpkg-genchanges -b -e"$(KMAINT) <$(KEMAIL)>" -u"$(KSRC)/.." > $(CHFILE)
	debsign -e"$(KMAINT) <$(KEMAIL)>" $(CHFILE)
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

kdist_configure:
	# Nothing to do when configured.
kdist_clean:
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules 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
