#!/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

# enable hardening
export DEB_BUILD_HARDENING=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
# dependency 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 	  \
  --libexecdir=/usr/lib --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

# 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)

# CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
# CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
# CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
# LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

# 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

# ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
#   NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
#   MAKEFLAGS += -j$(NUMJOBS)
# 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-arch: build
build-indep: build
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
	
	[ ! -f Makefile ] || $(MAKE) clean
	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 
	
	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
	for file in `find debian/tmp -name '*.la'`; do \
		sed -i "/dependency_libs/ s/'.*'/''/" $$file ; \
	done

binary-indep: build install

binary-arch: build install
	dh_testdir -a
	dh_testroot -a

	rm -rf `pwd`/debian/tmp/usr/lib/service_crm.so
	rm -rf `pwd`/debian/tmp/usr/lib/service_crm.la
	rm -rf `pwd`/debian/tmp/usr/lib/service_crm.a

	dh_install --sourcedir=debian/tmp --list-missing
	
	# install default file
	mkdir -p $(CURDIR)/debian/pacemaker/etc/default
	cp -ax $(CURDIR)/mcp/pacemaker.sysconfig $(CURDIR)/debian/pacemaker/etc/default/pacemaker

	# 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 debian/pacemaker/usr/share/doc/pacemaker/COPYING.LGPL
	rm -rf debian/pacemaker/usr/share/doc/pacemaker/COPYING.LIB
	rm -rf debian/pacemaker/usr/share/doc/pacemaker/COPYING
	
	# 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_lintian
	dh_installchangelogs
	dh_link -a
	dh_strip -a --dbg-package=pacemaker-dbg
	
	dh_compress -a
	dh_fixperms -a

	chmod -x $(CURDIR)/debian/pacemaker/usr/share/pacemaker/*.rng

	dh_makeshlibs -a -V
	dh_perl -a
	dh_python2 -ppacemaker -ppacemaker-dev
	dh_installdeb -a
	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
