#!/usr/bin/make -f

#export DH_VERBOSE=1

export CONFIG_SHELL=/bin/bash

derives_from_ubuntu := $(shell (dpkg-vendor --derives-from ubuntu && echo yes) || echo no)
ifeq ($(derives_from_ubuntu),yes)
DO_TOKYOCABINET = no
export DH_OPTIONS := -Nbogofilter-tokyocabinet
else
DO_TOKYOCABINET = yes
endif

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
TESTS_TARGET = check
else
TESTS_TARGET =
endif

autotools-stamp:
	dh_autotools-dev_updateconfig
	touch autotools-stamp

configure-indep: configure-indep-stamp
configure-indep-stamp: autotools-stamp
	dh_testdir
	mkdir -p obj-doc
	cd obj-doc && ../configure
	touch configure-indep-stamp

build-indep: build-indep-stamp
build-indep-stamp: configure-indep-stamp
	cd obj-doc/doc && $(MAKE)
	touch build-indep-stamp

configure: configure-stamp
configure-stamp: autotools-stamp
	dh_testdir
	mkdir obj-db obj-sqlite
	cd obj-db && ../configure --with-database=db \
		--prefix=/usr --mandir=\$${prefix}/share/man --sysconfdir=/etc \
		AWK=awk $(shell dpkg-buildflags --export=configure)
	cd obj-sqlite && ../configure --with-database=sqlite \
		--program-suffix=-sqlite --prefix=/usr --mandir=\$${prefix}/share/man \
		--sysconfdir=/etc AWK=awk $(shell dpkg-buildflags --export=configure) && \
		sed -i 's/^INTEGRITY_TESTS.*/INTEGRITY_TESTS=t.lock1/' src/tests/Makefile
	if [ $(DO_TOKYOCABINET) = yes ]; then \
		mkdir obj-tokyocabinet; \
		cd obj-tokyocabinet && ../configure \
			--with-database=tokyocabinet --program-suffix=-tokyocabinet \
			--prefix=/usr --mandir=\$${prefix}/share/man --sysconfdir=/etc AWK=awk \
			$(shell dpkg-buildflags --export=configure); \
	fi
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	cd obj-db && $(MAKE)
	cd obj-sqlite && $(MAKE)
	if [ $(DO_TOKYOCABINET) = yes ]; then \
		cd obj-tokyocabinet && $(MAKE); \
	fi
	touch build-stamp

clean:
	dh_testdir
	rm -f build-stamp configure-stamp build-indep-stamp configure-indep-stamp \
		autotools-stamp config.h.in debian/*debhelper.log \
		debian/*.substvars debian/bogofilter.substvars \
		debian/files debian/bogofilter-bdb.substvars \
		debian/bogofilter-sqlite.substvars
	rm -rf debian/bogofilter debian/bogofilter-bdb \
		debian/bogofilter-sqlite debian/bogofilter-common \
		obj-db obj-sqlite obj-doc
	if [ $(DO_TOKYOCABINET) = yes ]; then \
		rm -rf debian/bogofilter-tokyocabinet obj-tokyocabinet \
			debian/bogofilter-tokyocabinet.substvars; \
	fi
	dh_autotools-dev_restoreconfig

check:
	dh_testdir
	cd obj-db && $(MAKE) check
	cd obj-sqlite && $(MAKE) check
	if [ $(DO_TOKYOCABINET) = yes ]; then \
		cd obj-tokyocabinet && $(MAKE) check; \
	fi

PACKAGES = bdb sqlite
ifeq ($(DO_TOKYOCABINET),yes)
	PACKAGES += tokyocabinet
endif
PROGRAMS = bf_compact bf_copy bf_tar bogofilter bogolexer bogotune \
		   bogoupgrade bogoutil

binary-arch: build $(TESTS_TARGET)
	dh_testdir
	dh_testroot
	dh_installdirs
	# rename binaries: util becomes util-pkg
	test -e obj-bdb || ln -s obj-db obj-bdb
	for p in ${PACKAGES}; do \
		for i in ${PROGRAMS}; do \
			(set -e; cp obj-$${p}/src/$${i} debian/bogofilter-$${p}/usr/bin/$${i}-$${p}) \
		done; \
		dh_installdocs -pbogofilter-$${p}; \
		dh_installchangelogs -pbogofilter-$${p}; \
	done
	rm -f obj-bdb
	dh_link
	dh_fixperms
	dh_compress
	dh_strip
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	for p in ${PACKAGES}; do \
		dh_builddeb -pbogofilter-$${p}; \
	done

binary-indep: build-indep
	dh_testdir
	dh_testroot
	dh_installdirs -pbogofilter-common -pbogofilter
	dh_installman -pbogofilter-common
	dh_installchangelogs -pbogofilter-common
	cp bogofilter.cf.example debian/bogofilter-common/etc/bogofilter.cf
	dh_installdocs -pbogofilter-common
	# rename this to .html, to not confuse doc-base tools
	if [ -e debian/bogofilter-common/usr/share/doc/bogofilter-common/bogofilter-faq-bg.xhtml ]; then \
		mv debian/bogofilter-common/usr/share/doc/bogofilter-common/bogofilter-faq-bg.xhtml \
		   debian/bogofilter-common/usr/share/doc/bogofilter-common/bogofilter-faq-bg.html; \
	fi
	dh_compress -X.pl
	dh_installdeb -pbogofilter-common -pbogofilter
	# installdocs copies copyright/changelog in doc/bogofilter
	# but we really want it to be a symlink to doc/bogofilter-common
	rm -rf debian/bogofilter/usr/share/doc/bogofilter
	dh_link -pbogofilter
	dh_installexamples -pbogofilter-common
	chmod 755 debian/bogofilter-common/usr/share/doc/bogofilter-common/examples/contrib/bogominitrain.pl
	mv debian/bogofilter-common/usr/share/doc/bogofilter-common/examples/contrib/*example \
		debian/bogofilter-common/usr/share/doc/bogofilter-common/examples
	dh_fixperms -pbogofilter-common
	dh_gencontrol -pbogofilter-common -pbogofilter
	dh_md5sums -pbogofilter-common
	dh_builddeb -pbogofilter-common -pbogofilter

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-indep-stamp

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