#!/usr/bin/make -f
# debian/rules for thinkpad-modules-$KVERS
# Copyright (C) 2002, 2003  Thomas Hood
# Distributable under the terms of the GNU GPL version 2 (or later).

export DH_COMPAT=3

# Use no get-root command if we're already root
ifeq ($(shell id -u),0)
ROOT_CMD= 
endif

# make-kpkg calls one of the targets:
# kdist, kdist_configure, kdist_changes or kdist_image
# with the following variables set:
#  KVERS
#  KSRC
#  KEMAIL
#  KMAINT
#  KDREV

KSRC ?= /usr/src/linux
KTYPE=$(shell echo $(KVERS) | sed -e 's/^\([2-9].[0-9]\).*/\1/' )
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')
VERSION=$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
ARCH=$(shell dpkg --print-architecture)
CHFILE=$(KSRC)/../thinkpad-modules-$(KVERS)_$(VERSION)_$(ARCH).changes
DEB=$(shell pwd)/debian/thinkpad-modules-$(KVERS)

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

build-modules:
	cd $(KTYPE)/drivers && $(MAKE)

binary-modules: build-modules
	dh_testdir
	dh_testroot
	install -d $(DEB)/lib/modules/$(KVERS)/thinkpad
	cd $(KTYPE)/drivers && install -c -m 0644 *.o $(DEB)/lib/modules/$(KVERS)/thinkpad
	rm -f $(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)/..

kdist_image: kdist_configure
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules binary-modules

kdist_changes: kdist_image
	dpkg-genchanges -b -e"$(KMAINT) <$(KEMAIL)>" -u"$(KSRC)/.." > $(CHFILE)
	debsign -e"$(KMAINT) <$(KEMAIL)>" $(CHFILE)

kdist: kdist_changes

kdist_clean:
	dh_testdir
	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

