#!/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

dpkg_buildflags = DEB_BUILD_MAINT_OPTIONS="hardening=+all" dpkg-buildflags

PIGEONHOLE_DIR=pigeonhole

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

dbgpkg = $(filter %-dbg, $(shell dh_listpackages -a))
otherpkg = $(filter-out %-dbg, $(shell dh_listpackages -a))

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif

config-stamp: configure
	dh_testdir
	# Dovecot
	$(shell $(dpkg_buildflags) --export=configure) sh configure \
	            --with-ldap=plugin \
	            --with-ssl=openssl \
	            --with-sql=plugin \
	            --with-pgsql \
	            --with-mysql \
	            --with-sqlite \
	            --with-gssapi=plugin \
		        --with-solr \
	            --with-ioloop=best \
	            --host=$(DEB_HOST_GNU_TYPE) \
	            --build=$(DEB_BUILD_GNU_TYPE) \
	            --prefix=/usr \
	            --sysconfdir=/etc \
	            --libexecdir=\$${prefix}/lib \
	            --localstatedir=/var \
	            --mandir=\$${prefix}/share/man \
	            --infodir=\$${prefix}/share/info \
	            --with-moduledir=\$${prefix}/lib/dovecot/modules \
	            --disable-rpath \
	            --with-systemdsystemunitdir=auto \
	            --disable-static
	$(MAKE) dovecot-config
	# Pigeonhole
	(cd $(PIGEONHOLE_DIR)/ && \
	    touch stamp.h.in && \
	    $(shell $(dpkg_buildflags) --export=configure) sh configure \
	    --with-dovecot=../ \
		--prefix=/usr \
		--sysconfdir=/etc \
		--libexecdir=\$${prefix}/lib \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--disable-static )
	touch config-stamp

build-stamp:  config-stamp
	dh_testdir
	# Dovecot
	$(MAKE)
	# Pigeonhole
	$(MAKE) -C $(PIGEONHOLE_DIR)
	# DRAC
	$(MAKE) -C src/plugins/drac $(shell $(dpkg_buildflags) --export=configure)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp config-stamp
	# Cleanup dovecot itself
	[ ! -f Makefile ] || $(MAKE) distclean
	# Cleanup pigeonhole
	[ ! -f $(PIGEONHOLE_DIR)/Makefile ] || $(MAKE) -C $(PIGEONHOLE_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-core
	$(MAKE) -C $(PIGEONHOLE_DIR) install DESTDIR=$(CURDIR)/debian/dovecot-core
	rm `find $(CURDIR)/debian -name '*.la'`
	cp $(CURDIR)/src/plugins/drac/drac.so $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules
	install -o root -g root -m 0644 $(CURDIR)/doc/example-config/*.conf \
		$(CURDIR)/debian/dovecot-core/usr/share/dovecot/
	install -o root -g root -m 0644 $(CURDIR)/doc/example-config/*.conf.ext \
		$(CURDIR)/debian/dovecot-core/usr/share/dovecot/
	install -o root -g root -m 0744 -d $(CURDIR)/debian/dovecot-core/etc/dovecot/conf.d/
	install -o root -g root -m 0744 -d $(CURDIR)/debian/dovecot-core/usr/share/dovecot/conf.d/
	install -o root -g root -m 0644 $(CURDIR)/doc/example-config/conf.d/*.conf \
		$(CURDIR)/debian/dovecot-core/usr/share/dovecot/conf.d/
	install -o root -g root -m 0644 $(CURDIR)/doc/example-config/conf.d/*.conf.ext \
		$(CURDIR)/debian/dovecot-core/usr/share/dovecot/conf.d/
	install -o root -g root -m 0644 $(CURDIR)/$(PIGEONHOLE_DIR)/doc/example-config/conf.d/*.conf \
		$(CURDIR)/debian/dovecot-core/usr/share/dovecot/conf.d/
	install -D -m 0755 -o root -g root $(CURDIR)/debian/maildirmake.dovecot $(CURDIR)/debian/dovecot-core/usr/bin/maildirmake.dovecot
	mv $(CURDIR)/debian/dovecot-core/usr/share/doc/dovecot $(CURDIR)/debian/dovecot-core/usr/share/doc/dovecot-core
	cp $(PIGEONHOLE_DIR)/ChangeLog $(CURDIR)/debian/dovecot-core/usr/share/doc/dovecot-core/pigeonhole.ChangeLog
	cp $(PIGEONHOLE_DIR)/README $(CURDIR)/debian/dovecot-core/usr/share/doc/dovecot-core/pigeonhole.README
	cp $(PIGEONHOLE_DIR)/NEWS $(CURDIR)/debian/dovecot-core/usr/share/doc/dovecot-core/pigeonhole.NEWS

	# imapd
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/imap* $(CURDIR)/debian/dovecot-imapd/usr/lib/dovecot
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/lib??_imap* $(CURDIR)/debian/dovecot-imapd/usr/lib/dovecot/modules
	mv $(CURDIR)/debian/dovecot-core/usr/share/dovecot/conf.d/??-imap* $(CURDIR)/debian/dovecot-imapd/usr/share/dovecot/conf.d

	# pop3d
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/pop3* $(CURDIR)/debian/dovecot-pop3d/usr/lib/dovecot
	mv $(CURDIR)/debian/dovecot-core/usr/share/dovecot/conf.d/??-pop3* $(CURDIR)/debian/dovecot-pop3d/usr/share/dovecot/conf.d

	# lmtpd
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/lmtp* $(CURDIR)/debian/dovecot-lmtpd/usr/lib/dovecot
	mv $(CURDIR)/debian/dovecot-core/usr/share/dovecot/conf.d/??-lmtp* $(CURDIR)/debian/dovecot-lmtpd/usr/share/dovecot/conf.d

	# managesieved
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/managesieve* $(CURDIR)/debian/dovecot-managesieved/usr/lib/dovecot
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/settings/libmanagesieve* $(CURDIR)/debian/dovecot-managesieved/usr/lib/dovecot/modules/settings
	mv $(CURDIR)/debian/dovecot-core/usr/share/dovecot/conf.d/??-managesieve* $(CURDIR)/debian/dovecot-managesieved/usr/share/dovecot/conf.d

	# pgsql
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/auth/libdriver_pgsql* $(CURDIR)/debian/dovecot-pgsql/usr/lib/dovecot/modules/auth
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/dict/libdriver_pgsql* $(CURDIR)/debian/dovecot-pgsql/usr/lib/dovecot/modules/dict
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/libdriver_pgsql* $(CURDIR)/debian/dovecot-pgsql/usr/lib/dovecot/modules

	# mysql
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/auth/libdriver_mysql* $(CURDIR)/debian/dovecot-mysql/usr/lib/dovecot/modules/auth
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/dict/libdriver_mysql* $(CURDIR)/debian/dovecot-mysql/usr/lib/dovecot/modules/dict
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/libdriver_mysql* $(CURDIR)/debian/dovecot-mysql/usr/lib/dovecot/modules

	# sqlite
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/auth/libdriver_sqlite* $(CURDIR)/debian/dovecot-sqlite/usr/lib/dovecot/modules/auth
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/dict/libdriver_sqlite* $(CURDIR)/debian/dovecot-sqlite/usr/lib/dovecot/modules/dict
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/libdriver_sqlite* $(CURDIR)/debian/dovecot-sqlite/usr/lib/dovecot/modules

	# gssapi
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/auth/libmech_gssapi* $(CURDIR)/debian/dovecot-gssapi/usr/lib/dovecot/modules/auth

	# ldap
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/auth/libauthdb_ldap* $(CURDIR)/debian/dovecot-ldap/usr/lib/dovecot/modules/auth
	mv $(CURDIR)/debian/dovecot-core/usr/share/dovecot/*-ldap.conf.ext $(CURDIR)/debian/dovecot-ldap/usr/share/dovecot
	mv $(CURDIR)/debian/dovecot-core/usr/share/dovecot/conf.d/*-ldap.conf.ext $(CURDIR)/debian/dovecot-ldap/usr/share/dovecot/conf.d

	# sieve
	mv $(CURDIR)/debian/dovecot-core/usr/bin/sieve* $(CURDIR)/debian/dovecot-sieve/usr/bin
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/lib??_sieve* $(CURDIR)/debian/dovecot-sieve/usr/lib/dovecot/modules
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/libdovecot-sieve* $(CURDIR)/debian/dovecot-sieve/usr/lib/dovecot
	mv $(CURDIR)/debian/dovecot-core/usr/share/dovecot/conf.d/*-sieve.conf $(CURDIR)/debian/dovecot-sieve/usr/share/dovecot/conf.d
	mv $(CURDIR)/debian/dovecot-core/usr/share/man/man1/sieve* $(CURDIR)/debian/dovecot-sieve/usr/share/man/man1

	# solr
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/modules/lib??_fts_solr_* $(CURDIR)/debian/dovecot-solr/usr/lib/dovecot/modules

	# dev
	mv $(CURDIR)/debian/dovecot-core/usr/include/* $(CURDIR)/debian/dovecot-dev/usr/include
	mv $(CURDIR)/debian/dovecot-core/usr/lib/dovecot/dovecot-config $(CURDIR)/debian/dovecot-dev/usr/lib/dovecot
	rmdir $(CURDIR)/debian/dovecot-core/usr/include

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installman -i
	dh_link -i
	dh_strip -i
	dh_compress -i
	dh_fixperms -i
	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# 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-core/etc/pam.d/dovecot-core $(CURDIR)/debian/dovecot-core/etc/pam.d/dovecot
	dh_installinit -pdovecot-core --init-script=dovecot -u"defaults 20"
	dh_installman -a
	dh_installman -p dovecot-core debian/maildirmake.dovecot.1
	dh_bugfiles -a
	dh_lintian -a
	dh_installchangelogs -a ChangeLog
	dh_link -a
	dh_strip -a --dbg-package=dovecot-dbg
	dh_compress -a
	dh_fixperms -a
	chmod 0700 debian/dovecot-core/etc/dovecot/private
	dh_makeshlibs -a -n
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb $(foreach pkg,$(dbgpkg),-p$(pkg)) -- -Zxz
	dh_builddeb $(foreach pkg,$(otherpkg),-p$(pkg))

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