#!/usr/bin/make -f

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

STORAGE = "mysql_drv,pgsql_drv,sqlite3_drv,hash_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

# Without any effect for now due to http://bugs.debian.org/347650
export 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)
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
        CROSS = --build=$(DEB_HOST_GNU_TYPE)
else
        CROSS = --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif

%:
	dh $@ --with autoreconf

override_dh_auto_configure:
	dh_auto_configure -- $(CROSS) $(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-pgsql -Nlibdspam7-drv-mysql -Nlibdspam7-drv-sqlite3 -Ndspam

override_dh_strip:
	dh_strip --dbg-package=dspam-dbg

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

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
