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

export DH_COMPAT=3

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

# 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

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/' )

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 $@

configure-modules: debian/control debian/changelog debian/postinst debian/postrm

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

binary-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)/..

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

kdist_image: configure-modules build-modules
	$(ROOT_CMD) $(MAKE) -f debian/rules binary-modules && $(MAKE) -f debian/rules kdist_clean

kdist_clean:
	$(ROOT_CMD) $(MAKE) -f debian/rules clean-modules

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

