#!/usr/bin/make -f

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

# Show command in Makefile
export VERBOSE=1

# The build system doesn't use CPPFLAGS, pass them to CFLAGS to enable the
# missing (hardening) flags.
export DEB_CFLAGS_MAINT_APPEND  := -Wall $(shell dpkg-buildflags --get CPPFLAGS)
# More hardening
export DEB_BUILD_MAINT_OPTIONS	:= hardening=+bindnow

DESTDIR=$(CURDIR)/debian/tmp

conf_args = \
		--localstatedir=/var \
		--with-socketpath=/var/run/ctdb/ctdbd.socket \
		--with-logdir=/var/log/ctdb \
		--enable-pmda

%:
	dh $@ --with systemd

override_dh_auto_configure: check-external-libs
	dh_auto_configure -- $(conf_args)

override_dh_auto_test:
	# skipping (runtime) tests

override_dh_systemd_enable:
	dh_systemd_enable -pctdb

override_dh_systemd_start:
	dh_systemd_start -pctdb --no-start --no-restart-on-upgrade

override_dh_installinit:
	# Install /etc/default/ctdb
	mkdir -p $(CURDIR)/debian/ctdb/etc/default
	install -m644 config/ctdb.sysconfig $(CURDIR)/debian/ctdb/etc/default/ctdb
	# Install /etc/init.d/ctdb
	mkdir -p $(CURDIR)/debian/ctdb/etc/init.d
	install -m755 config/ctdb.init $(CURDIR)/debian/ctdb/etc/init.d/ctdb
	# Install /lib/systemd/system/ctdb.service
	mkdir -p $(CURDIR)/debian/ctdb/lib/systemd/system
	install -m644 config/ctdb.service $(CURDIR)/debian/ctdb/lib/systemd/system/ctdb.service
	# Install dh scripts
	dh_installinit -pctdb --no-start --no-restart-on-upgrade --onlyscripts

override_dh_installdocs:
	cp config/events.d/README README.eventscripts
	cp config/notify.d.README README.notify.d
	dh_installdocs
ifeq ($(DEB_BUILD_ARCH_OS), hurd)
	dh_installdocs debian/README.hurd
endif
ifeq ($(DEB_BUILD_ARCH_OS), kfreebsd)
	dh_installdocs debian/README.kfreebsd
endif
	#patch doc
	sed -i 's|/etc/sysconfig/|/etc/default/|' $(CURDIR)/debian/ctdb/usr/share/doc/ctdb/web/*.html
	#upstream changelog 
	grep -A 10000 '%changelog' packaging/RPM/ctdb.spec.in > $(CURDIR)/debian/ctdb/usr/share/doc/ctdb/changelog

override_dh_strip:
	dh_strip --dbg-package=ctdb-dbg

check-external-libs:
	@if [ -e lib/popt -o -e lib/tdb -o -e lib/talloc -o -e lib/tevent ]; then \
              echo " *****************************************"; \
	      echo " **                                     **"; \
	      echo " **  tar contains external libraries!   **"; \
	      echo " **                                     **"; \
              echo " *****************************************"; \
	      echo "The tarball should be made by using"; \
	      echo "git-import-orig, uscan or"; \
	      echo "debian/uupdate-wrapper"; \
	      echo ""; \
	      exit 1; \
        fi

