#!/usr/bin/make -f

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

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Allows easy backporting on non-multiarch aware distributions
PREPROCESS_FILES := $(wildcard debian/*.install.in)
$(PREPROCESS_FILES:.in=): %: %.in
	sed 's,@DEB_HOST_MULTIARCH@/,$(DEB_HOST_MULTIARCH:=/),g' $< > $@

# The first driver stated hear will be used as the 
# default driver in dspam.conf
STORAGE = "hash_drv,mysql_drv,pgsql_drv,sqlite3_drv"

CONFIGURE = --sysconfdir=/etc/dspam \
            --disable-dependency-tracking \
            --enable-split-configuration \
            --enable-static \
            --enable-external-lookup \
            --enable-syslog \
            --with-logdir=/var/log/dspam/ \
            --with-dspam-home=/var/spool/dspam \
            --enable-domain-scale \
            --with-delivery-agent=/usr/bin/procmail \
            --enable-daemon \
            --with-mysql-includes=/usr/include/mysql \
            --with-pgsql-includes=`pg_config --includedir` \
            --with-storage-driver=${STORAGE} \
            --enable-debug

ifeq (,$(findstring disable_virtual_users,$(DEB_BUILD_OPTIONS)))
        CONFIGURE += --enable-virtual-users
endif
ifeq (,$(findstring disable_preferences_extension,$(DEB_BUILD_OPTIONS)))
        CONFIGURE += --enable-preferences-extension
endif
ifneq (,$(findstring verbose_debug,$(DEB_BUILD_OPTIONS)))
        CONFIGURE += --enable-verbose-debug
endif
ifeq (,$(findstring disable_clamav,$(DEB_BUILD_OPTIONS)))
        CONFIGURE += --enable-clamav
endif

# Needed to not depend on debhelper >= 9 (and thus ease backports)
CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)
LDFLAGS+=-Wl,-z,defs -Wl,--as-needed

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

%:
	dh $@ --with autoreconf

override_dh_auto_configure: $(PREPROCESS_FILES:.in=)
	dh_auto_configure -- --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE) $(CONFIGURE)

	# dh_install does not support renaming
	ln contrib/dspam_maintenance/dspam_maintenance.sh contrib/dspam_maintenance/dspam_maintenance

	# make sure we use the latest SQL schemas
	cat src/tools.mysql_drv/mysql_objects-4.1.sql > debian/sqlfiles/install/mysql
	cat src/tools.mysql_drv/virtual_users.sql >> debian/sqlfiles/install/mysql
	cat src/tools.pgsql_drv/pgsql_objects.sql > debian/sqlfiles/install/pgsql
	cat src/tools.pgsql_drv/virtual_users.sql >> debian/sqlfiles/install/pgsql

	# split the main configuration file into snippets 
	cp src/dspam.conf.in src/dspam.conf.in.orig
	./debian/split-config.sh

override_dh_makeshlibs:
	dh_makeshlibs -Nlibdspam7-drv-hash -Nlibdspam7-drv-pgsql -Nlibdspam7-drv-mysql -Nlibdspam7-drv-sqlite3

override_dh_strip:
	dh_strip -pdspam --dbg-package=dspam-dbg
	dh_strip -plibdspam7 -plibdspam7-drv-hash -plibdspam7-drv-mysql -plibdspam7-drv-pgsql -plibdspam7-drv-sqlite3 --dbg-package=libdspam7-dbg

override_dh_installinit:
	dh_installinit -- start 21 2 3 4 5 . stop 21 0 1 6 .

ifneq (,$(DEB_HOST_MULTIARCH))
override_dh_gencontrol:
	dh_gencontrol -- -Vmisc:Multi-Arch=same
endif

override_dh_auto_clean:
	[ ! -f Makefile ] || $(MAKE) maintainer-clean

	-rm -f contrib/dspam_maintenance/dspam_maintenance

	# Remove DB schemas
	-rm -f debian/sqlfiles/install/mysql
	-rm -f debian/sqlfiles/install/pgsql

	# Remove splitted configuration files
	-rm -f src/extlookup.conf
	-rm -f src/hash.conf
	-rm -f src/mysql.conf
	-rm -f src/pgsql.conf
	-mv src/dspam.conf.in.orig src/dspam.conf.in

	# Remove install files generated from install.in files
	-rm -f debian/libdspam7.install
	-rm -f debian/libdspam7-dev.install
	-rm -f debian/libdspam7-drv-hash.install
	-rm -f debian/libdspam-drv-mysql.install
	-rm -f debian/libdspam7-drv-pgsql.install
	-rm -f debian/libdspam7-drv-sqlite3.install
