#!/usr/bin/make -f
# -*- makefile -*-

export DH_OPTIONS
SHELL := /bin/bash
BUILD_ROOT=$(CURDIR)/debian/tmp
DIST := $(shell lsb_release -i | awk '{print tolower($$3)}')
RELEASE := $(shell lsb_release -r | awk '{print $$2}')
    
%:
	dh $@

install::
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	echo $(CURDIR)
	echo $(BUILD_ROOT)
	mkdir -p $(BUILD_ROOT)
	rake install PARAMS_FILE= DESTDIR=$(BUILD_ROOT) 
	rake terminus PARAMS_FILE= DESTDIR=$(BUILD_ROOT) 
	dh_installdocs -p puppetdb documentation

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install -i
	dh_installdocs -i
	dh_installlogcheck
	dh_installman
	if [[ ${DIST} == debian ]] ; then \
		if [[ $$(echo "${RELEASE}>=7" | bc -l) -eq 1 || ${RELEASE} == testing || ${RELEASE} == unstable ]] ; then \
			sed -i '/notifempty/a\    su puppetdb puppetdb' $(CURDIR)/debian/puppetdb.logrotate ; \
		fi ; \
	elif [[ ${DIST} == ubuntu ]] ; then \
		if [[ $$(echo "${RELEASE}>=13.04" | bc -l) -eq 1 ]] ; then \
			sed -i '/notifempty/a\    su puppetdb puppetdb' $(CURDIR)/debian/puppetdb.logrotate ; \
		fi ; \
	fi
	dh_installinit -ppuppetdb
	dh_installlogrotate -i -ppuppetdb
	dh_compress -i
	dh_fixperms -i -X puppetdb-ssl-setup -X puppetdb-foreground -X puppetdb-import -X puppetdb-export -X puppetdb-anonymize -X puppetdb -X etc
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

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

clean::
	dh_clean
	rm -rf debian/patches
	rm -rf .pc
