#!/usr/bin/make -f

# Uncomment to enable verbose build.
#DH_VERBOSE = 1

# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*  TODO, figure out if this is needed.
DPKG_EXPORT_BUILDFLAGS = 1

# Enable all build hardening flags.
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Export the DPKG default build flags.
DPKG_EXPORT_BUILDFLAGS = 1

# Include the default build flags.  <https://wiki.debian.org/Hardening#dpkg-buildflags>
include /usr/share/dpkg/buildflags.mk

# Append the CPPFLAGS to the standard CFLAGS variables.  <https://wiki.debian.org/Hardening#Notes_for_packages_using_CMake>
CFLAGS += $(CPPFLAGS)

# The build captures $SHELL, but it needs to be hard-wired to "/bin/sh".  This is necessary for reproducible builds.
export CONFIG_SHELL=/bin/sh

# TODO, figure out if this is needed.
include /usr/share/dpkg/default.mk

# main packaging script based on dh7 syntax
%:
	dh $@

COMMON_CONFOPTS=--with-authdaemonvar=/run/courier/authdaemon \
		--with-pkgconfdir=/etc/courier \
		--libdir=\$${prefix}/lib \
		--libexecdir=\$${prefix}/lib/courier \
		--datadir=\$${prefix}/lib/courier \
		--localstatedir=/var/lib/courier \
		--with-mailuser=courier \
		--with-mailgroup=courier \
		--without-socks \
		--with-authpam \
		--with-authpipe \
		--without-authpwd \
		--with-authmysql \
		--with-mysql-includes=/usr/include/mysql \
		--with-mysql-libs=/usr/lib \
		--with-authmysqlrc=/etc/courier/authmysqlrc \
		--with-authpgsql \
		--with-pgsql-includes=`pg_config --includedir` \
		--with-pgsql-libs=/usr/lib \
		--with-authpgsqlrc=/etc/courier/authpgsqlrc \
		--without-authshadow \
		--with-authldap \
		--with-authldaprc=/etc/courier/authldaprc \
		--with-authsqlite="" \
		--with-authsqliterc=/etc/courier/authsqliterc \
		--with-authcram \
		--with-db=gdbm \
		--without-fcgi \
		--with-htmllibdir=/usr/share/sqwebmail \
		--with-ispell=/usr/bin/ispell \
		--enable-userdb \
		--enable-syslog=1 \
		--disable-root-check \
		--with-locking-method=fcntl \

override_dh_auto_configure:
	dh_auto_configure -- PS=/bin/ps $(COMMON_CONFOPTS)

override_dh_install:
	pod2man --center='Debian GNU/Linux Documentation' \
		--release='Debian GNU/Linux '`cat /etc/debian_version` \
		--section=8 debian/authenumerate.pod \
		--verbose \
		> debian/tmp/usr/share/man/man8/authenumerate.8

# Change default module list
	perl -pe 's/^authmodulelist=".*?"/authmodulelist="authpam"/' debian/tmp/etc/courier/authdaemonrc.dist > debian/tmp/etc/courier/authdaemonrc
	rm -v debian/tmp/etc/courier/authdaemonrc.dist

# Change default LDAP server to localhost
	perl -pe 's/^(LDAP_SERVER\s+)ldap.example.com/$$1localhost/' debian/tmp/etc/courier/authldaprc.dist > debian/tmp/etc/courier/authldaprc
	rm -v debian/tmp/etc/courier/authldaprc.dist

# Change default MySQL server to localhost
	perl -pe 's/^(MYSQL_SERVER\s+)mysql.example.com/$$1localhost/;s%^(#?\s*MYSQL_SOCKET\s+)/.*%$$1/var/run/mysqld/mysqld.sock%' debian/tmp/etc/courier/authmysqlrc.dist > debian/tmp/etc/courier/authmysqlrc
	rm -v debian/tmp/etc/courier/authmysqlrc.dist

# Change default SQlite 3 path to /var/lib/courier/users.db
	perl -pe 's%^(SQLITE_DATABASE\s+)/var/users.db%$$1/var/lib/courier/users.db%' debian/tmp/etc/courier/authsqliterc.dist > debian/tmp/etc/courier/authsqliterc
	rm -v debian/tmp/etc/courier/authsqliterc.dist

	mv -v debian/tmp/etc/courier/authpgsqlrc.dist debian/tmp/etc/courier/authpgsqlrc

	dh_install
	dh_missing --fail-missing
