#!/usr/bin/make -f

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

SHELL=/bin/bash -O extglob

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# LP: 1636781 - strip incompatible default linker option
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
  export DEB_LDFLAGS_MAINT_STRIP = -Wl,-Bsymbolic-functions
endif

# Macros to help move files to the module packages
PIGEONHOLE_DIR=$(CURDIR)/debian/dovecot-sieve
PKG_DIR=$(CURDIR)/debian/dovecot-PKG
MOVE=mv $(PIGEONHOLE_DIR)/$(1) $(PKG_DIR:PKG=$(2))/$(dir $(1));

%:
	dh $@ --with=systemd,autotools-dev

override_dh_auto_configure:
	sh configure \
	        --with-dovecot=/usr/lib/dovecot \
		--prefix=/usr \
		--sysconfdir=/etc \
		--libexecdir=\$${prefix}/lib \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--disable-static \
		--with-ldap=plugin \
		--with-unfinished-features

override_dh_auto_build:
	dh_auto_build --parallel

override_dh_auto_clean:
	dh_auto_clean

override_dh_auto_install:
	# Install everything under dovecot-managesieved
	$(MAKE) install DESTDIR=$(PIGEONHOLE_DIR)
	rm `find $(CURDIR)/debian -name '*.la'` ||:

override_dh_install:
	dh_install

        # dev
	#mkdir -p $(CURDIR)/debian/dovecot-sieve-dev/usr/lib/dovecot/modules/doveadm
	#mkdir -p $(CURDIR)/debian/dovecot-sieve-dev/usr/lib/dovecot/modules/settings
	#mkdir -p $(CURDIR)/debian/dovecot-sieve-dev/usr/lib/dovecot/modules/sieve
	#mv $(CURDIR)/debian/dovecot-sieve/usr/lib/dovecot/modules/*.a \
	#	$(CURDIR)/debian/dovecot-sieve-dev/usr/lib/dovecot/modules/
	#mv $(CURDIR)/debian/dovecot-sieve/usr/lib/dovecot/modules/doveadm/*.a \
	#	$(CURDIR)/debian/dovecot-sieve-dev/usr/lib/dovecot/modules/doveadm/
	#mv $(CURDIR)/debian/dovecot-sieve/usr/lib/dovecot/modules/settings/*.a \
	#	$(CURDIR)/debian/dovecot-sieve-dev/usr/lib/dovecot/modules/settings/
	#mv $(CURDIR)/debian/dovecot-sieve/usr/lib/dovecot/modules/sieve/*.a \
	#	$(CURDIR)/debian/dovecot-sieve-dev/usr/lib/dovecot/modules/sieve/

	mkdir -p $(CURDIR)/debian/dovecot-sieve-dev/usr/share/aclocal
	mv $(CURDIR)/debian/dovecot-sieve/usr/share/aclocal/* \
		$(CURDIR)/debian/dovecot-sieve-dev/usr/share/aclocal/
	rmdir $(CURDIR)/debian/dovecot-sieve/usr/share/aclocal

	mkdir -p $(CURDIR)/debian/dovecot-sieve-dev/usr/include
	mv $(CURDIR)/debian/dovecot-sieve/usr/include/* \
		$(CURDIR)/debian/dovecot-sieve-dev/usr/include/
	rmdir $(CURDIR)/debian/dovecot-sieve/usr/include

	# managesieved
	mkdir -p $(CURDIR)/debian/dovecot-managesieved/usr/lib/dovecot/modules/settings
	mv $(CURDIR)/debian/dovecot-sieve/usr/lib/dovecot/managesieve* \
		$(CURDIR)/debian/dovecot-managesieved/usr/lib/dovecot

	mv $(CURDIR)/debian/dovecot-sieve/usr/lib/dovecot/modules/settings/libmanagesieve* \
		$(CURDIR)/debian/dovecot-managesieved/usr/lib/dovecot/modules/settings

	mkdir -p $(CURDIR)/debian/dovecot-managesieved/usr/share/dovecot/conf.d 
	mv $(CURDIR)/debian/dovecot-sieve/usr/share/doc/dovecot-core/example-config/conf.d/*-managesieve*.conf* \
		$(CURDIR)/debian/dovecot-managesieved/usr/share/dovecot/conf.d

	# relocate config files in sieve package
	mkdir -p $(CURDIR)/debian/dovecot-sieve/usr/share/dovecot/conf.d
	mv $(CURDIR)/debian/dovecot-sieve/usr/share/doc/dovecot-core/example-config/conf.d/??-sieve*.conf* \
		$(CURDIR)/debian/dovecot-sieve/usr/share/dovecot/conf.d/

	mv $(CURDIR)/debian/dovecot-sieve/usr/share/doc/dovecot-core/example-config/sieve-ldap.conf \
		$(CURDIR)/debian/dovecot-sieve/usr/share/dovecot/

	#grep ABI_VERSION config.h \
	#	| perl -ne '/"(.+)\(.+\)"/ && print $$1' \
	#	| tr '[A-Z]' '[a-z]' > $(PKG_DIR:PKG=dev)/usr/share/dovecot/dovecot-abi
	
#override_dh_gencontrol:
	dh_gencontrol -- -Vdovecot:ABI-Version=$(shell cat /usr/share/dovecot/dovecot-abi)

override_dh_strip:
	dh_strip --dbg-package=dovecot-pigeonhole-dbg

override_dh_makeshlibs:
	# Do not add an ldconfig trigger; none of the dovecot shared libraries
	# are public.
	dh_makeshlibs -n

override_dh_installdocs:
	dh_installdocs

.PHONY: override_dh_auto_configure override_dh_auto_build \
	override_dh_auto_clean override_dh_auto_install \
	override_dh_gencontrol override_dh_installinit \
	override_dh_strip override_dh_installpam \
	override_dh_makeshlibs override_dh_installdocs \
	override_dh_install override_dh_systemd_enable
