#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

SIEVE_DIR=sieve
MANAGESIEVE_DIR=managesieve

# 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)

config-stamp: configure
	dh_testdir
	# Dovecot
	sh configure --with-ldap \
	            --with-ssl=openssl \
	            --with-db \
	            --with-pgsql \
	            --with-mysql \
	            --with-sqlite \
	            --with-gssapi \
	            --with-ioloop=best \
	            --enable-header-install \
	            --host=$(DEB_HOST_GNU_TYPE) \
	            --build=$(DEB_BUILD_GNU_TYPE) \
	            --prefix=/usr \
	            --sysconfdir=/etc/dovecot \
	            --libexecdir=\$${prefix}/lib \
	            --localstatedir=/var \
	            --mandir=\$${prefix}/share/man \
	            --infodir=\$${prefix}/share/info \
	            --with-moduledir=\$${prefix}/lib/dovecot/modules \
	            --disable-rpath \
	            --disable-static
	$(MAKE) dovecot-config
	# Dovecot sieve plugin
	(cd $(SIEVE_DIR)/ && sh configure --with-dovecot=../ \
		--prefix=/usr \
		--libexecdir=\$${prefix}/lib \
		--enable-static=no )
	# Dovecot managesieve module
	(cd $(MANAGESIEVE_DIR)/ && sh configure --with-dovecot=../ \
		--with-dovecot-sieve=../$(SIEVE_DIR)/ \
		--prefix=/usr \
		--libexecdir=\$${prefix}/lib)
	touch config-stamp

build: build-stamp
build-stamp:  config-stamp
	dh_testdir
	# Dovecot
	$(MAKE)
	# Dovecot sieve plugin
	$(MAKE) -C $(SIEVE_DIR)
	# Dovecot managesieve module
	$(MAKE) -C $(MANAGESIEVE_DIR)
	# DRAC
	$(MAKE) -C src/plugins/drac
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp config-stamp
	# Cleanup dovecot itself
	[ ! -f Makefile ] || $(MAKE) distclean
	# Cleanup dovecot sieve plugin
	[ ! -f $(SIEVE_DIR)/Makefile ] || $(MAKE) -C $(SIEVE_DIR) distclean
	# Cleanup dovecot ManageSieve
	[ ! -f $(MANAGESIEVE_DIR)/Makefile ] || $(MAKE) -C $(MANAGESIEVE_DIR) distclean
	# Cleanup DRAC
	rm -f src/plugins/drac/drac.so
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	$(MAKE) install DESTDIR=$(CURDIR)/debian/dovecot-common
	$(MAKE) -C $(SIEVE_DIR) install DESTDIR=$(CURDIR)/debian/dovecot-common
	$(MAKE) -C $(MANAGESIEVE_DIR) install DESTDIR=$(CURDIR)/debian/dovecot-common
	cp $(CURDIR)/src/plugins/drac/drac.so $(CURDIR)/debian/dovecot-common/usr/lib/dovecot/modules
	mv $(CURDIR)/debian/dovecot-common/etc/dovecot/dovecot-example.conf $(CURDIR)/debian/dovecot-common/usr/share/dovecot/dovecot.conf
	install -o root -g root -m 0644 $(CURDIR)/doc/dovecot-ldap-example.conf $(CURDIR)/debian/dovecot-common/usr/share/dovecot/dovecot-ldap.conf
	install -o root -g root -m 0644 $(CURDIR)/doc/dovecot-sql-example.conf $(CURDIR)/debian/dovecot-common/usr/share/dovecot/dovecot-sql.conf
	install -D -m 0755 -o root -g root $(CURDIR)/debian/maildirmake.dovecot $(CURDIR)/debian/dovecot-common/usr/bin/maildirmake.dovecot
	install -m 0755 -o root -g root $(CURDIR)/debian/expire-tool.sh $(CURDIR)/debian/dovecot-common/usr/lib/dovecot/expire-tool.sh
	mv $(CURDIR)/debian/dovecot-common/usr/share/doc/dovecot $(CURDIR)/debian/dovecot-common/usr/share/doc/dovecot-common
	cp $(SIEVE_DIR)/ChangeLog $(CURDIR)/debian/dovecot-common/usr/share/doc/dovecot-common/sieve.ChangeLog
	cp $(SIEVE_DIR)/README $(CURDIR)/debian/dovecot-common/usr/share/doc/dovecot-common/sieve.README
	cp $(SIEVE_DIR)/NEWS $(CURDIR)/debian/dovecot-common/usr/share/doc/dovecot-common/sieve.NEWS
	cp $(MANAGESIEVE_DIR)/ChangeLog $(CURDIR)/debian/dovecot-common/usr/share/doc/dovecot-common/managesieve.ChangeLog
	cp $(MANAGESIEVE_DIR)/README $(CURDIR)/debian/dovecot-common/usr/share/doc/dovecot-common/managesieve.README
	cp $(MANAGESIEVE_DIR)/NEWS $(CURDIR)/debian/dovecot-common/usr/share/doc/dovecot-common/managesieve.NEWS
	mv $(CURDIR)/debian/dovecot-common/usr/lib/dovecot/imap* $(CURDIR)/debian/dovecot-imapd/usr/lib/dovecot
	mv $(CURDIR)/debian/dovecot-common/usr/lib/dovecot/modules/imap/ $(CURDIR)/debian/dovecot-imapd/usr/lib/dovecot/modules/
	mv $(CURDIR)/debian/dovecot-common/usr/lib/dovecot/pop3* $(CURDIR)/debian/dovecot-pop3d/usr/lib/dovecot
	mv $(CURDIR)/debian/dovecot-common/usr/lib/dovecot/modules/pop3/ $(CURDIR)/debian/dovecot-pop3d/usr/lib/dovecot/modules/
	mv $(CURDIR)/debian/dovecot-common/usr/include/* $(CURDIR)/debian/dovecot-dev/usr/include
	mv $(CURDIR)/debian/dovecot-common/usr/lib/dovecot/dovecot-config $(CURDIR)/debian/dovecot-dev/usr/lib/dovecot
	rmdir $(CURDIR)/debian/dovecot-common/usr/include
	rm $(CURDIR)/debian/dovecot-common/etc/dovecot/dovecot-ldap-example.conf
	rm $(CURDIR)/debian/dovecot-common/etc/dovecot/dovecot-sql-example.conf

# Build architecture-independent files here.
binary-indep: build install
	# nothing to do here

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installpam -a
	mv $(CURDIR)/debian/dovecot-common/etc/pam.d/dovecot-common $(CURDIR)/debian/dovecot-common/etc/pam.d/dovecot
	dh_installinit -pdovecot-common --init-script=dovecot -u"defaults 20"
	dh_installcron -p dovecot-common --name=dovecot
	dh_installman -a
	dh_installman -p dovecot-common debian/maildirmake.dovecot.1 debian/dovecot.1 debian/dovecot.8 debian/dovecotpw.1
	dh_installchangelogs -a ChangeLog
	dh_link -a
	dh_strip -a --dbg-package=dovecot-dbg
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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