#!/usr/bin/make -f
# -*- makefile -*-
# debian/rules file - for drbd
# Patterned after the pcmcia-cs package by Brian Mays.

# STATUS
# drbd package seems fine. Doesn't need any other things

package=drbd

# Strip epoch, which is at the _end_ of KDREV
# (modified copy from the lm_sensors package)
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

PKGVERSION=$(EPOCH)$(VERSION)+$(REV)
MVARUSUBDIRS=$(shell grep ^SUBDIRS Makefile.vars | sed 's/ drbd / /g' | sed 's/ = /=/g'|cut -d '\#' -f1 )
#MVARKSUBDIRS=drbd
export DH_VERBOSE=1
export DH_COMPAT=4

SMPFLAG=$(shell grep -q '^CONFIG_SMP=y' $(KSRC)/.config && echo "-D__SMP__")

install-kernelpatch:
	dh_testdir
	dh_testroot
	dh_installdirs -pkernel-patch-wup
	dh_installkpatches -pkernel-patch-wup

binary-kernelpatch: install-kernelpatch
	dh_testdir
	dh_testroot
	dh_installdocs -pkernel-patch-wup
	dh_installchangelogs -pkernel-patch-wup
	dh_installdeb -pkernel-patch-wup
	dh_gencontrol -pkernel-patch-wup -Pdebian/kernel-patch-wup
	dh_fixperms -pkernel-patch-wup
	dh_builddeb -pkernel-patch-wup -Pdebian/kernel-patch-wup

binary-indep:   binary-source binary-kernelpatch
    $(checkdir)

binary-arch: checkroot binary-drbd
	$(checkdir)

binary-drbd:  checkroot build
	$(checkdir)
	$(RM) -r debian/drbd
	dh_installdirs -pdrbd DEBIAN usr/sbin var/lib/drbd etc/heartbeat/resource.d etc/init.d
# Install changelog & copyright
	dh_installman -pdrbd documentation/*.[1-9]
	dh_installdocs -pdrbd -Xxgml -XMakefile README documentation/HOWTO
	dh_installchangelogs -pdrbd ChangeLog 
	install -m755 user/drbdsetup debian/drbd/usr/sbin/
	dh_compress -pdrbd
#       We don't wan't the examples to be compressed :)
	install -m755 scripts/datadisk debian/drbd/etc/heartbeat/resource.d/
	cat scripts/drbd.conf | sed -e 's/^/#/' >debian/drbd/etc/drbd.conf
	cp scripts/drbd debian/drbd.init
	dh_installinit -n -pdrbd -Pdebian/drbd -- defaults 19 21
	dh_installexamples -pdrbd scripts/drbd.conf
	dh_strip -pdrbd
	dh_fixperms -pdrbd
# Generate deb file
	dh_gencontrol -pdrbd -Pdebian/drbd
	dh_installdeb -pdrbd -Pdebian/drbd
# dpkg-deb --build debian/drbd ..
	dh_builddeb -pdrbd -Pdebian/drbd

binary: binary-arch binary-indep

binary-source:
	$(checkdir)
	$(RM) -r debian/src
	dh_installdirs -Pdebian/src -pdrbd-source DEBIAN usr/src/modules/$(package)/debian usr/share/doc/drbd-source
# Install files
	cp -a debian/rules debian/changelog debian/control.modules debian/src/usr/src/modules/$(package)/debian
	cp -a debian/control.modules debian/src/usr/src/modules/$(package)/debian/control
	cp -a drbd debian/src/usr/src/modules/$(package)/
#	cp -a debian/copyright debian/src/usr/share/doc/
	cp Makefile.vars debian/src/usr/src/modules/$(package)/
	cp drbd_config.h debian/src/usr/src/modules/$(package)/
	cp debian/Makefile.modules debian/src/usr/src/modules/$(package)/Makefile
	chown -R root.src debian/src/usr/src/modules/$(package)/
	chmod -R g+w debian/src/usr/src/modules/$(package)/
#	find debian/src -type d | xargs chmod 755
#	find debian/src -type f -perm -100 | xargs chmod 755
#	find debian/src -type f -not -perm -100 | xargs chmod 644
	cd debian/src/usr/src && \
	tar cf $(package).tar modules && \
	$(RM) -r modules/$(package)
	gzip -9 debian/src/usr/src/$(package).tar
	chmod 644 debian/src/usr/src/$(package).tar.gz
	dh_fixperms -pdrbd-source -Pdebian/src
# Install changelog & copyright
	dh_installchangelogs -pdrbd-source -Pdebian/src ChangeLog 
	dh_installdocs -pdrbd-source -Pdebian/src README
	dh_compress -pdrbd-source -Pdebian/src
	dh_installinit --update-rcd-params='start 70 2 3 4 5 . stop 08 0 1 6 .'
# Generate deb file
	dh_gencontrol -pdrbd-source -Pdebian/src
# dpkg-deb --build debian/src ..
	dh_builddeb -pdrbd-source -Pdebian/src


build:
	$(checkdir)
	$(MAKE) "$(MVARUSUBDIRS)"
# Could not get docbook2man to work...
# But at least we have manpages for now :)
#	cd documentation && nsgmls drbdsetup.sgml | sgmlspl /usr/lib/perl5/sgmlspl-specs/docbook2man-spec.pl
#	cd documentation && nsgmls drbd.conf.sgml | sgmlspl /usr/lib/perl5/sgmlspl-specs/docbook2man-spec.pl

clean:
	$(MAKE) clean
	-rm -rf debian/src
	-rm debian/drbd.init
	dh_clean

checkroot:
	$(checkdir)
	test root = "`whoami`"

# This is used when kernel-package is building the package. 
# Build the kernel module .deb.
build-modules:
	dh_testdir
	cd drbd && $(MAKE) KDIR=$(KSRC) KERNVER=$(KVERS) CC=gcc SMPFLAG=$(SMPFLAG)

binary-modules: build-modules
	dh_clean -k
	dh_testdir
	dh_testroot

	rm -f debian/control.save
	cp debian/control debian/control.save
	rm -f debian/control
	sed 's/#KVERS#/$(KVERS)/g' debian/control.modules > debian/control
# And set up files so debhelper can find them.
	dh_installdirs lib/modules/$(KVERS)/misc
	dh_install -m644 drbd/drbd.o lib/modules/$(KVERS)/misc
	dh_installdocs
	dh_installmodules
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v$(PKGVERSION)
	dh_makeshlibs
	dh_md5sums
	dh_builddeb --destdir=$(KSRC)/..

kdist_image: binary-modules clean

kdist: kdist_clean binary-modules

kdist_clean:
	dh_testdir
	cd drbd && $(MAKE) INCLUDE=-I$(KSRC)/include KERNVER=$(KVERS) clean

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

.PHONY: binary binary-arch binary-indep binary-modules \
	clean clean-modules checkroot \
	kconf kdist kdist_image kdist_changes

