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

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

# 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 libsieve/ && sh configure --with-dovecot=../ \
		--prefix=/usr \
		--libexecdir=\$${prefix}/lib \
		--enable-static=no )
	cp libsieve/ChangeLog libsieve/sieve.ChangeLog
	cp libsieve/README libsieve/sieve.README
	cp libsieve/NEWS libsieve/sieve.NEWS
	# Dovecot managesieve module
	(cd managesieve/ && sh configure --with-dovecot=../ \
		--with-dovecot-sieve=../libsieve/ \
		--prefix=/usr \
		--libexecdir=\$${prefix}/lib)
	cp managesieve/ChangeLog managesieve/managesieve.ChangeLog
	cp managesieve/README managesieve/managesieve.README
	cp managesieve/NEWS managesieve/managesieve.NEWS
	touch config-stamp

build: build-stamp
build-stamp:  config-stamp
	dh_testdir
	# Dovecot
	$(MAKE)
	# Dovecot sieve plugin
	$(MAKE) -C libsieve
	# Dovecot managesieve module
	$(MAKE) -C managesieve
	# 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 libsieve/Makefile ] || $(MAKE) -C libsieve distclean
	rm -f libsieve/sieve.README libsieve/sieve.NEWS libsieve/sieve.ChangeLog
	# Cleanup dovecot ManageSieve
	[ ! -f managesieve/Makefile ] || $(MAKE) -C managesieve distclean
	rm -f managesieve/managesieve.README managesieve/managesieve.NEWS managesieve/managesieve.ChangeLog
	# 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 libsieve install DESTDIR=$(CURDIR)/debian/dovecot-common
	$(MAKE) -C managesieve 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 -m644 debian/dovecot-common.ufw.profile debian/dovecot-common/etc/ufw/applications.d/dovecot-common
	mv $(CURDIR)/debian/dovecot-common/usr/share/doc/dovecot $(CURDIR)/debian/dovecot-common/usr/share/doc/dovecot-common
	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
	mkdir -p $(CURDIR)/debian/dovecot-postfix/etc/dovecot/conf.d/
	mkdir -p $(CURDIR)/debian/dovecot-postfix/etc/dovecot/auth.d/
	cp $(CURDIR)/debian/01-dovecot-postfix.conf $(CURDIR)/debian/dovecot-postfix/etc/dovecot/conf.d/
	cp $(CURDIR)/debian/01-dovecot-postfix.auth $(CURDIR)/debian/dovecot-postfix/etc/dovecot/auth.d/
	dh_testdir -pdovecot-postfix
	dh_testroot -pdovecot-postfix
	dh_installdocs -pdovecot-postfix
	dh_installchangelogs -pdovecot-postfix ChangeLog
	dh_compress -pdovecot-postfix
	dh_installdeb -pdovecot-postfix
	dh_gencontrol -pdovecot-postfix
	dh_md5sums -pdovecot-postfix
	dh_builddeb -pdovecot-postfix

# 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"start 24 2 3 4 5 . stop 76 1 ."
	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 
