#!/usr/bin/make -f

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

SHELL=/bin/bash -O extglob

COMMIT = $(shell head -1 buildinfo.commit)
ID= $(shell lsb_release -is)
RELEASE = $(shell lsb_release -rs)
ARCH = $(shell arch)
VERSION_FULL = $(shell head -1 debian/changelog |sed -e 's/.* (\(.*\)) .*/\1/g')
VERSION_SHORT= $(shell echo $(VERSION_FULL)|sed -e 's/.*\:\(.*\)\-.*/\1/g')
BUILDINFO=BUILDINFO: $(VERSION_SHORT) $(COMMIT) $(ID)/$(RELEASE)/$(ARCH) dovecot=$(VERSION_FULL)

export DEB_BUILD_MAINT_OPTIONS=hardening=+all optimize=+lto
export VERSION=$(shell echo $(VERSION_FULL)|sed -e 's/.*\:\(.*\)/\1/g')
export UNIT_PATH=$(shell systemd-path systemd-system-unit)

# 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

%:
	dh $@

override_dh_auto_configure:
	env KRB5CONFIG=krb5-config.mit dh_auto_configure -- \
		    --enable-experimental-imap4rev2 \
		    --enable-experimental-mail-utf8 \
		    --with-cdb \
		    --with-pam \
		    --with-libunwind \
		    --with-stemmer \
		    --with-icu \
		    --with-bzlib \
		    --with-libcap \
		    --with-systemd \
	            --with-ldap=plugin \
	            --with-sql=plugin \
	            --with-lua=plugin \
	            --with-pgsql \
	            --with-mysql \
	            --with-sqlite \
	            --with-gssapi=plugin \
	            --with-solr \
		    --with-flatcurve \
		    --with-lz4 \
		    --with-ztsd \
		    --with-cdb \
		    --with-sodium \
		    --with-apparmor \
		    --with-retpoline=thunk-inline \
	            --libexecdir=\$${prefix}/lib \
	            --with-moduledir=\$${prefix}/lib/dovecot/modules \
	            --with-rundir=\$${runstatedir}/dovecot
	$(MAKE) dovecot-config dovecot-version.h
	
override_dh_auto_build:
	sed -i -e "s|DOVECOT_BUILD_INFO DOVECOT_VERSION_FULL|DOVECOT_BUILD_INFO \"$(BUILDINFO)\"|" dovecot-version.h
	dh_auto_build --parallel

override_dh_install:
	chmod 0700 debian/dovecot-core/etc/dovecot/private
	rm -rf debian/tmp/usr/share/doc/dovecot/html
	rm -rf debian/tmp/usr/share/doc/dovecot/example-config
	rm -f  debian/tmp/usr/share/doc/dovecot/dovecot-openssl.cnf
	# replace dovecot config with our version
	sed -e "s/@CONFIG_VERSION@/$(VERSION_SHORT)/g" debian/dovecot.conf.in > \
		debian/tmp/etc/dovecot/dovecot.conf
	dh_install
	grep ABI_VERSION config.h \
		| perl -ne '/"(.+)"/ && print $$1' \
		| tr '[A-Z]' '[a-z]' > debian/dovecot-dev/usr/share/dovecot/dovecot-abi
	
	sed -i "s@LIBDOVECOT='\(.*\)'@LIBDOVECOT=\'\1 -Wl,-R/usr/lib/$(DEB_HOST_MULTIARCH)/dovecot\'@" \
	  $(CURDIR)/debian/dovecot-dev/usr/lib/dovecot/dovecot-config

override_dh_installpam:
	dh_installpam -pdovecot-core --name=dovecot

override_dh_installinit:
	dh_installinit -pdovecot-core --name=dovecot -u"defaults 20" --no-start

override_dh_gencontrol:
	dh_gencontrol -- -Vdovecot:ABI-Version=$(shell cat $(CURDIR)/debian/dovecot-dev/usr/share/dovecot/dovecot-abi)

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

override_dh_installsystemd:
	dh_installsystemd -pdovecot-core dovecot.service
	dh_installsystemd -pdovecot-core --no-enable dovecot.socket
