#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# Copyright 1997 to 1999 by Joey Hess.
# License: GNU General Public License (GPL)

# Thanks to Paolo Molaro <lupus@debian.org> and heartbeat_0.4.6-2.diff

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

#
# Path to SSH and SCP
# Usually this is guessed if ssh is installed.
# However, there is a long standing bug with installing ssh
# on arm in debian and this is preventing heartbeat from
# moving into sarge. By adding this we can remove the build
# dependancy on ssh and get an updated heartbeat into sarge... 
# I hope. Horms 26th July 2004
#
# If this is removed then the build depandancy on ssh should be
# restored
#

cfg:=--prefix=/usr --sysconfdir=/etc --localstatedir=/var 	  \
  --mandir=/usr/share/man \
  --enable-snmp-subagent --with-mibsdir=/usr/share/snmp/mibs 	  \
  --disable-fatal-warnings --enable-glib-malloc --with-esmtp 	  \
  --with-snmp --with-ais --with-heartbeat --libdir=/usr/lib	  \
  --with-lcrso-dir=/usr/lib/lcrso

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g -I../../../.. -I../../../../include

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
       CFLAGS += -O0
else
       CFLAGS += -O2
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
       INSTALL_PROGRAM += -s
endif

RELEASE := $(shell dpkg-parsechangelog | awk '/^Version: / { print $$2 }')
UPSTREAM := $(shell echo "$(RELEASE)" | sed 's/:\?\([0-9\.]\+\)-[0-9].*/\1/')
ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)

patch:
	override_version=$(RELEASE) sh debian/apply

unpatch:
	override_version=$(RELEASE) sh debian/apply $(UPSTREAM)


build: build-stamp
build-stamp:
	dh_testdir
	[ -f configure ] || sh autogen.sh
	if [ -e configure -a ! -x configure ]; then chmod u+x configure; fi
	if [ -e py-compile -a ! -x py-compile ]; then chmod u+x py-compile; fi
	# run configure script
	./configure $(cfg)
	# set rng compatibility symlinks
	$(shell cd xml && for a in *-1.0.rng; do ln -s "$$a" `echo "$$a" | sed 's/-1.0//g'`; done)
	$(MAKE)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	rm -f tools/ccdv
	rm -f libltdl.tar autoconf automake autoheader
	rm -f build-stamp
	rm -f debian/*.files debian/*.dirs

	rm -rf debian/*-build-*
	rm -rf debian/tmp-*

	## Zero autoconf foo
	-rm -f config.status libltdl/config.log libltdl/config.status 

	# do a make changes
	make changes || true

	dh_clean

install: build install-stamp
install-stamp:
	dh_testdir
	dh_testroot
	dh_installdirs
	$(MAKE) install DESTDIR=`pwd`/debian/tmp docdir=/usr/share/doc/pacemaker

binary-indep: build install

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	
	dh_install --sourcedir=debian/tmp --list-missing

	mkdir -p `pwd`/debian/pacemaker/usr/share/doc/pacemaker/templates/

	# make non-scripts non-executable
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/crm-transitional.dtd
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/resources-1.0.rng
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/nvset-1.0.rng
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/upgrade06.xsl
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/constraints-1.0.rng
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/shelltest/testcases/xmlonly.sh
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/shelltest/testcases/confbasic-xml.filter
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/pacemaker.rng
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/pacemaker-1.0.rng
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/score.rng
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/crm.dtd
	-chmod -x `pwd`/debian/pacemaker/usr/share/pacemaker/rule-1.0.rng

	# delete redundant COPYING files
	rm -rf `pwd`/debian/pacemaker/usr/share/doc/pacemaker/COPYING.LGPL.gz
	rm -rf `pwd`/debian/pacemaker/usr/share/doc/pacemaker/COPYING.gz

	# create special directory
	mkdir -p `pwd`/debian/pacemaker/var/lib/pengine
	mkdir -p `pwd`/debian/pacemaker/var/lib/heartbeat/pengine
	
	dh_installdebconf -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installinit -a -n -u 'defaults 20 32'
	dh_installman -a
	dh_installchangelogs
	dh_link -a
	dh_strip -a

	# make the pacemaker shlib non-executable
	chmod -x `pwd`/debian/pacemaker/usr/lib/lcrso/pacemaker.lcrso

	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -V
	dh_installdeb -a
	dh_perl -a
	dh_pycentral -a
	dh_python -a `pwd`/usr/lib/pacemaker/cts `pwd`/usr/lib/pacemaker
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

.PHONY: build clean binary binary-indep binary-arch install \
	patch unpatch
