#!/usr/bin/make -f
# debian/rules for thinkpad-modules
# Copyright (C) 1999 Steve Haslam
# Distributable under the terms of the GNU GPL version 2 (or later).

# This rules file is intended only to build the thinkpad-modules-KVER
# package.

export DH_COMPAT=3

DEB=$(shell pwd)/debian/thinkpad-modules-$(KVERS)

NONEPOCH=$(shell echo $(KDREV) | sed -e 's/^[0-9]*://')
EPOCHT=$(shell echo $(KDREV) | sed -e 's/$(NONEPOCH)$$//g')
EPOCH=$(shell echo $(EPOCHT) | sed -e 's/://g')
KTYPE=$(shell echo $(KVERS) | sed -e 's/^\([2-9].[0-9]\).*/\1/' )

# We are called from make-kpkg through one of the targets kdist,
# kdist_configure, kdist_changes or kdist_image
# When called from make-kpkg we have available:
#  KVERS
#  KSRC
#  KEMAIL
#  KMAINT
#  KDREV

SCRIPT=s!\$$KVERS!$(KVERS)!g; \
       s!\$$KSRC!$(KSRC)!; \
       s!\$$KEMAIL!$(KEMAIL)!; \
       s!\$$KMAINT!$(KMAINT)!; \
       s!\$$NONEPOCH!$(NONEPOCH)!; \
       s!\$$EPOCH!$(EPOCH)!; \
       s!\$$DEBDATE!$(shell 822-date)!

debian/control: debian/control.sed
	sed -e "$(SCRIPT)" $< > $@
	chmod ugo+x $@

debian/changelog: debian/changelog.sed
	sed -e "$(SCRIPT)" $< > $@
	chmod ugo+x $@

debian/postinst: debian/postinst.sed
	sed -e "$(SCRIPT)" $< > $@
	chmod ugo+x $@

debian/postrm: debian/postrm.sed
	sed -e "$(SCRIPT)" $< > $@
	chmod ugo+x $@

kdist_configure: debian/control debian/changelog debian/postinst debian/postrm

kdist_image: kdist_configure
#	dh_testversion
	dh_testdir
	dh_testroot
	cd $(KTYPE)/drivers && $(MAKE)
	install -d $(DEB)/lib/modules/$(KVERS)/thinkpad
	cd $(KTYPE)/drivers && install -c -m 0644 *.o $(DEB)/lib/modules/$(KVERS)/thinkpad
	rm $(DEB)/lib/modules/$(KVERS)/thinkpad/smapi_*.o
	dh_installdocs
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb --destdir=$(KSRC)/..
	# kernel-package says to do this
	$(MAKE) -f debian/rules kdist_clean

kdist_clean:
#	dh_testdir
	dh_testroot
	dh_clean
	cd $(KTYPE)/drivers && $(MAKE) clean
	rm -f debian/control debian/changelog debian/postinst debian/postrm

.PHONY: kdist kdist_image kdist_changes kdist_configure
.PHONY: binary binary-arch binary-indep source diff build install configure

