#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#DH_VERBOSE = 1

# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# The below options are disabled as it causes issues with the build;
# Ensure that we link against all needed libraries (cf. Policy 10.2)
#export DEB_LDFLAGS_MAINT_APPEND=-Wl,-z,defs

# Because we use --libexecdir in dh_auto_configure and thus specify
# /usr/lib, we need to set DEB_HOST_MULTIARCH and tac said flag on to
# the end so that triplet paths are supported
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH) 

# main packaging script based on dh7 syntax
%:
	dh $@ --with autoreconf,python2,systemd --fail-missing --link-doc=pacemaker

override_dh_autoreconf:
	dh_autoreconf ./autogen.sh

override_dh_auto_configure:
	dh_auto_configure -- --prefix=/usr --libexecdir=/usr/lib/$(DEB_HOST_MULTIARCH) --localstatedir=/var \
	    --sysconfdir=/etc --libdir=/usr/lib/$(DEB_HOST_MULTIARCH) --includedir=/usr/include/$(DEB_HOST_MULTIARCH) \
	    --docdir=/usr/share/doc/pacemaker --disable-fatal-warnings --enable-dependency-tracking --enable-glib-malloc \
	    --with-snmp --with-esmtp --enable-snmp-subagent --with-mibsdir=/usr/share/snmp/mibs \
	    --with-corosync --with-cs-quorum

DOC = debian/tmp/usr/share/doc/pacemaker

override_dh_auto_build:
	dh_auto_build
	$(MAKE) doxygen
	rm doc/api/html/*.md5

override_dh_auto_test:
	-dh_auto_test

override_dh_auto_install:
	dh_auto_install
# clear dependency_libs field in .la files
	for file in `find debian/tmp -name '*.la'`; do \
		sed -i "/dependency_libs/ s/'.*'/''/" $$file ; \
	done
# install default file
	mkdir -p $(CURDIR)/debian/pacemaker/etc/default
	cp -ax $(CURDIR)/mcp/pacemaker.sysconfig $(CURDIR)/debian/pacemaker/etc/default/pacemaker
# remove redundant COPYING files
	rm -v $(DOC)/COPYING $(DOC)/COPYING.LIB

override_dh_installinit:
	dh_installinit -a -- defaults 20 32

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog

override_dh_strip:
	dh_strip --dbg-package=pacemaker-dbg
	
override_dh_python2:
	dh_python2 -ppacemaker -ppacemaker-dev
	
override_dh_compress:
# remove python compiled bytecode and object files
	find $(CURDIR)/debian/pacemaker/ -name '*.pyo' | xargs rm -rf
	find $(CURDIR)/debian/pacemaker/ -name '*.pyc' | xargs rm -rf
# make non-scripts non-executable
	chmod -x $(CURDIR)/debian/pacemaker/usr/share/pacemaker/*.dtd
	chmod -x $(CURDIR)/debian/pacemaker/usr/share/pacemaker/*.xsl
	chmod -x $(CURDIR)/debian/pacemaker/usr/share/pacemaker/*.rng
	dh_compress
