#!/usr/bin/make -f

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# enable hardening 
export DEB_BUILD_HARDENING=1

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

configure: configure-stamp
configure-stamp: 
	dh_testdir
	test -x ./configure || ./autogen.sh
	./configure --with-ocf-root=/usr/lib/ocf --with-initdir=/etc/init.d \
		    --prefix=/usr --sysconfdir=/etc --localstatedir=/var \
  		    --mandir=/usr/share/man --libdir=/usr/lib \
		    --enable-upstart --disable-fatal-warnings
	touch configure-stamp

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp  
	$(MAKE)
	touch $@

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp  
	touch $@

clean: 
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) clean
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_clean 

install: install-indep install-arch
install-indep:

install-arch:
	dh_testdir
	dh_testroot
	dh_prep -s
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	for file in `find debian/tmp -name '*.la'`; do \
		sed -i "/dependency_libs/ s/'.*'/''/" $$file ; \
	done
	chrpath -r "" debian/tmp/usr/lib/stonith/plugins/stonith2/ipmilan.so || true
	dh_install --sourcedir=$(CURDIR)/debian/tmp --list-missing

	# install logd.cf configuration file
	mkdir -p $(CURDIR)/debian/cluster-glue/etc/
	cp -ax $(CURDIR)/logd/logd.cf $(CURDIR)/debian/cluster-glue/etc/

binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installman
	dh_lintian
	dh_installinit --name=logd -pcluster-glue -u 'defaults 20 32'
	dh_python2
	dh_python2 /usr/lib/stonith/plugins/stonith2
	dh_strip
	dh_compress
	dh_fixperms
	dh_perl
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch configure
