#!/usr/bin/make -f
#
# Copyright 2004,2005,2008 by Stefan Hornburg (Racke) <racke@linuxia.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.

BUILD=$(shell pwd)/debian/tmp
RELUP=$(shell dpkg-parsechangelog | perl -ne 'print $$1 if /^Version: (.*?)-(.*)$$/')

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

export SHELL = /bin/bash

# Common configuration options
NONSSL_CONFOPTS=--without-certdb --without-authpgsql

CFLAGS = `dpkg-buildflags --get CFLAGS`
LDFLAGS = `dpkg-buildflags --get LDFLAGS`
LDFLAGS += -lcrypt
CPPFLAGS = `dpkg-buildflags --get CPPFLAGS`

COMMON_CONFOPTS=--prefix=/usr --mandir=\$${prefix}/share/man \
		--with-authdaemonvar=/var/run/courier/authdaemon \
		CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" \
		--sysconfdir=/etc/courier \
		--with-pkgconfdir=/etc/courier \
		--libexecdir=\$${prefix}/lib/courier \
		--datadir=\$${prefix}/lib/courier \
		--localstatedir=/var/lib/courier \
		--sbindir=\$${exec_prefix}/sbin \
		--with-mailuser=daemon \
		--with-mailgroup=daemon \
		--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-authdaemonvar=/var/run/courier/authdaemon \
		--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

check:
	dh_testdir
	chmod +x debian/courier_perms

build: check
# create a list of files that currently exists
	if [ ! -f stamp-build ]; then \
		if [ -d /usr/include/postgresql/8.0/libpq_fe.h ]; then \
			./configure $(COMMON_CONFOPTS) \
			--with-postgresql-includes=/usr/include/postgresql/8.0 \
			&& $(MAKE) && touch stamp-build; \
		else \
			./configure $(COMMON_CONFOPTS) \
			--with-postgresql-includes=/usr/include/postgresql \
			&& $(MAKE) && touch stamp-build; \
		fi \
	fi

stamp-build: build

clean: check
	dh_testroot
	rm -f stamp-build stamp-install

# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_clean

install: check stamp-build
	dh_testroot
	dh_clean -k
	dh_installdirs

# Add here commands to install the package into debian/tmp.
	$(MAKE) DESTDIR=$(BUILD) install

	sed -i "/dependency_libs/ s/'.*'/''/" `find $(BUILD) -name '*.la'`

#	(cd $(BUILD)/usr/lib/courier/courier-authlib/changepwd; ln -fs ../../authsystem.passwd ./authsystem.passwd)

# Install authentification test program
#	mv $(BUILD)/usr/sbin/authtest $(BUILD)/usr/sbin/courierauthtest

# Additional manpages
	pod2man --center='Debian GNU/Linux Documentation' --release='Debian GNU/Linux '`cat /etc/debian_version` --section=8 debian/authenumerate.pod > $(BUILD)/usr/share/man/man8/authenumerate.8
#	pod2man --center='Debian GNU/Linux Documentation' --release='Debian GNU/Linux '`cat /etc/debian_version` --section=8 debian/courierauthtest.pod > $(BUILD)/usr/share/man/man8/courierauthtest.8

	dh_installdocs
# - change authentification default settings
	perl -pe 's/^authmodulelist=".*?"/authmodulelist="authpam"/' $(BUILD)/etc/courier/authdaemonrc.dist > $(BUILD)/etc/courier/authdaemonrc
# - change default LDAP server
	perl -pe 's/^(LDAP_SERVER\s+)ldap.example.com/$$1localhost/' $(BUILD)/etc/courier/authldaprc.dist > $(BUILD)/etc/courier/authldaprc
# - change default MySQL server
	perl -pe 's/^(MYSQL_SERVER\s+)mysql.example.com/$$1localhost/;s%^(#?\s*MYSQL_SOCKET\s+)/.*%$$1/var/run/mysqld/mysqld.sock%' $(BUILD)/etc/courier/authmysqlrc.dist > $(BUILD)/etc/courier/authmysqlrc
# - change default SQlite 3 path
	perl -pe 's%^(SQLITE_DATABASE\s+)/var/users.db%$$1/var/lib/courier/users.db%' $(BUILD)/etc/courier/authsqliterc.dist > $(BUILD)/etc/courier/authsqliterc
	mv $(BUILD)/etc/courier/authpgsqlrc.dist $(BUILD)/etc/courier/authpgsqlrc
	rm $(BUILD)/etc/courier/*.dist

# Lintian overrides
	mkdir -p $(BUILD)/usr/share/lintian/overrides
	for package in courier-authdaemon courier-authlib-ldap courier-authlib-mysql courier-authlib-postgresql courier-authlib-sqlite; do \
		cp debian/$$package.lintian $(BUILD)/usr/share/lintian/overrides/$$package; \
	done

# This seems to be necessary for building in fakeroot 
# environment (otherwise dh_strip fails)
# set all binaries to 755, by checking if user has x-bit
# debian/courier_perms will set the right permissions
	find $(BUILD) -perm -u+x -type f | xargs chmod u+rwx,go+rx

	dh_movefiles

# Check if all files have moved out
	if [ `find $(BUILD) -not -type d | wc -l` -ne 0 ]; then find $(BUILD) -not -type d; echo "File(s) found not belonging to any package, please contact maintainer"; exit 1; fi

	touch stamp-install

stamp-install: install

# Build architecture-independent files here.
binary-indep: stamp-build stamp-install
	dh_testdir
	dh_testroot
#	dh_installdebconf -i
	dh_installexamples -i
	dh_installmenu -i
	dh_installinit -i
	dh_installinfo -i
	dh_installchangelogs -i
	dh_strip -i
	dh_link -i
	dh_compress -i 
	dh_fixperms -i
	debian/courier_perms -i
	dh_installdeb -i
	dh_perl -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

calibdir = debian/courier-authlib/usr/lib/courier-authlib

# Build architecture-dependent files here.
binary-arch: stamp-build stamp-install
	dh_testdir
	dh_testroot
#	dh_installdebconf -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installinit -a
	dh_installcron -a
	dh_installinfo -a
	dh_installchangelogs -a
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	debian/courier_perms -a
	dh_perl -a
	dh_makeshlibs -pcourier-authlib -- -Pdebian/courier-authlib -e$(calibdir)/libcourier*.so
	dh_shlibdeps -a -l$(calibdir)
	dh_installdeb -a
	dh_gencontrol -- -VRELUP="$(RELUP)"
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch

.PHONY: check build clean binary-arch binary install
