#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
export DH_OPTIONS=-v

SHELL := sh -e
WHITELIST_DIR="$(CURDIR)/database/whitelists/"
MAINDIR="$(CURDIR)"
INSTDIR="$(CURDIR)/debian/tmp"

#%:
#	dh  $@

# Use override_dh_* targets to customize this.
# ---------------------------------------------------
# Followings are the old debian/rules
#
# #! /usr/bin/make -f
# 
# export DEB_BUILD_OPTIONS
# export DH_OPTIONS
# 

DH_VERBOSE=1

LIBDIR=/usr/lib/postfix-cluebringer

# SHELL := sh -e
# WHITELIST_DIR="$(CURDIR)/database/whitelists/"
# MAINDIR="$(CURDIR)"
# INSTDIR="$(CURDIR)/debian/tmp"
# 
#override_dh_clean: unpatch
#	dh_testroot
#	dh_testdir
#	dh_clean

### Additional work for the clean-target:
clean::
	rm -rf database/policyd-db.*
	rm -fr ${INSTDIR}
	rm -fr ${CURDIR}/debian/postfix-cluebringer-pgsql
	rm -fr ${CURDIR}/debian/postfix-cluebringer-mysql
	rm -fr ${CURDIR}/debian/postfix-cluebringer-sqlite3
	rm -fr ${CURDIR}/debian/postfix-cluebringer-webui
	rm -fr ${CURDIR}/debian/postfix-cluebringer
	rm -fr ${CURDIR}/debian/files


### The 'build' target needs to be completely overridden, but the 'patch' step is now
### obsoleted through the use of quilt:
#override_dh_build:

build:
	dh_testdir

	for db in pgsql mysql sqlite; do \
		for i in core.tsql access_control.tsql quotas.tsql amavis.tsql checkhelo.tsql checkspf.tsql greylisting.tsql; do \
			$(CURDIR)/database/convert-tsql $$db $(CURDIR)/database/$$i ;\
		done | grep -v '^#' > "$(CURDIR)/database/policyd-db.$$db" ;\
		cd $(WHITELIST_DIR) ;\
		./parse-greylisting-whitelist >> "$(MAINDIR)/database/policyd-db.$$db" ;\
		./parse-checkhelo-whitelist >> "$(MAINDIR)/database/policyd-db.$$db" ;\
		cd $(MAINDIR) ;\
	done

	mkdir -p $(INSTDIR)/usr/sbin
	mkdir -p $(INSTDIR)/usr/share/postfix-cluebringer-webui
	mkdir -p $(INSTDIR)/usr/share/doc/postfix-cluebringer/database
	mkdir -p $(INSTDIR)/usr/share/doc/postfix-cluebringer/templates
	mkdir -p $(INSTDIR)/usr/share/doc/postfix-cluebringer-webui/templates

	mkdir -p $(INSTDIR)${LIBDIR}

	install -m 755 cbpadmin cbpolicyd $(INSTDIR)/usr/sbin
	install -d -m 755 ${INSTDIR}/etc/cluebringer
	# band aid until we can fix this properly:
	install -d -m 755 ${CURDIR}/debian/postfix-cluebringer/etc/cluebringer
	install -d -m 755 ${CURDIR}/debian/postfix-cluebringer-webui/etc/cluebringer
	install -m 644 debian/cluebringer.conf-debian ${CURDIR}/debian/postfix-cluebringer/etc/cluebringer/cluebringer.conf
	install -m 644 debian/cluebringer.conf-debian $(INSTDIR)/etc/cluebringer/cluebringer.conf
	install -m 644 debian/cluebringer.conf-debian $(INSTDIR)/usr/share/doc/postfix-cluebringer/templates/cluebringer.conf
	install -m 644 debian/cluebringer.conf-webui $(INSTDIR)/usr/share/doc/postfix-cluebringer-webui/templates/cluebringer-webui.conf
	install -m 644 debian/cluebringer.conf-webui $(CURDIR)/debian/postfix-cluebringer-webui/etc/cluebringer/cluebringer-webui.conf
	install -m 644 database/policyd-db.* $(INSTDIR)/usr/share/doc/postfix-cluebringer/database/

	cp -R cbp $(INSTDIR)${LIBDIR}
	cp -R webui $(INSTDIR)/usr/share/postfix-cluebringer-webui

#override_dh_install: build
install: build
	dh_testroot
	dh_testdir
	dh_installexamples
	dh_installdirs
	dh_install --sourcedir=$(INSTDIR) --list-missing

#override_dh_binary-indep: install
binary-indep: install
	dh_testroot -i
	dh_testdir -i
	dh_installdocs -i
	dh_installchangelogs -i CHANGELOG
	dh_installdebconf -i
	dh_installinit -i
	dh_installman -i
	dh_lintian -i
	dh_compress -i -X.conf
	dh_fixperms -i
	dh_perl -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

#override_dh_binary-arch: install
binary-arch: install
	# Nothing to do here

#override_dh_binary: binary-arch binary-indep
binary: binary-arch binary-indep

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