#!/usr/bin/make -f

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

export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# getting DEB_VERSION from helper without manually parsing the changlog file
include /usr/share/dpkg/pkg-info.mk

DEB_BUILD_DIR=debian/build

INSTDIR=$(CURDIR)/debian/tmp
PHP_API_DATE=$(shell php-config --phpapi)
PHP_VERSION=$(shell echo $$(readlink -f /usr/bin/php-config) | sed 's/\/usr\/bin\/php-config//g')
PREPROCESS_FILES := $(wildcard debian/*.in)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CONFFLAG = ""
else
	CONFFLAG = "--enable-release"
endif

DEB_CONFIGURE_EXTRA_ARGS :=      \
    --host=$(DEB_HOST_GNU_TYPE) \
    --build=$(DEB_BUILD_GNU_TYPE) \
    --prefix=/usr \
    --localstatedir=/var \
    --sysconfdir=/etc \
	--with-systemunitdir=/usr/lib/systemd/system \
    --mandir=\$${prefix}/share/man \
    --infodir=\$${prefix}/share/info \
    --with-php-config=/usr/bin/php-config \
    --with-quotatemplate-prefix=/etc/kopano/quotamail \
    --disable-static \
    --enable-epoll \
    --with-distro=debian \
    --enable-python \
    PYTHON="python3" \
    $(CONFFLAG) \
    $(NULL)


$(PREPROCESS_FILES:.in=): %: %.in
	sed -e 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH:%=%)/g' \
		-e 's/@PHP_VERSION@/$(PHP_VERSION:%=%)/g' \
		$< > $@

%:
	dh $@ --builddirectory=$(DEB_BUILD_DIR) --with php,python3

override_dh_auto_configure: $(PREPROCESS_FILES:.in=)
	echo ${DEB_VERSION} > $(CURDIR)/revision
	[ -x ./bootstrap.sh ] && ./bootstrap.sh || true
	dh_auto_configure -- $(DEB_CONFIGURE_EXTRA_ARGS)

override_dh_auto_clean:
	rm -rf $(DEB_BUILD_DIR)
	rm -f revision
	[ ! -f Makefile ] || dh_auto_clean

override_dh_install:
	# Install docs
	$(MAKE) DESTDIR=$(INSTDIR) install -C $(DEB_BUILD_DIR)/swig/python/kopano
	# Don't ship any .la files
	find $(INSTDIR) -name '*.la' | xargs rm
	# Make sure docs end up in kopano-server
	mv $(INSTDIR)/usr/share/doc/kopano $(INSTDIR)/usr/share/doc/kopano-server

	# since this is not linked to an initscript anymore, install the file ourselves.
	mkdir -p $(INSTDIR)/etc/default
	install -o root -g root -m 644 debian/kopano-common.kopano.default $(INSTDIR)/etc/default/kopano

	# dbconfig-common
	mkdir -p $(INSTDIR)/usr/share/kopano/debian
	cp -a debian/sql/debian-db.cfg.template $(INSTDIR)/usr/share/kopano/debian

	# remove compiled python files
	find $(INSTDIR) -name \*.pyc -exec rm {} \;

	mv debian/tmp/usr/share/doc/kopano-server/example-config/*.cfg \
	   debian/tmp/usr/share/doc/kopano-server/example-config/autorespond \
           debian/tmp/etc/kopano/
	dh_install
	# remove obsolete zarafa Python things
	rm -rf debian/tmp/usr/lib/python3/dist-packages/zarafa*

	for p in server dagent search; do \
	  dh_apparmor --profile-name=usr.sbin.kopano-$${p} -pkopano-$${p}; \
	done
	# Don't ship upstream's profiles as example-config
	rm -r debian/tmp/usr/share/doc/kopano-server/example-config/apparmor.d/

	# Use upstream version
	rm debian/tmp/usr/lib/python3/dist-packages/kopano/lockfile.py

override_dh_installinit:
	dh_installinit --name kopano-dagent
	dh_installinit --name kopano-gateway
	dh_installinit --name kopano-ical
	dh_installinit --name kopano-monitor
	dh_installinit --name kopano-presence
	dh_installinit --name kopano-search
	dh_installinit --name kopano-server
	dh_installinit --name kopano-spooler

override_dh_compress:
	dh_compress -X.py -X.pl

override_dh_makeshlibs:
	dh_makeshlibs -O--builddirectory=$(DEB_BUILD_DIR) -O--parallel \
		-Xdbplugin \
		-Xldapplugin \
		-Xlibkcclient \
		-Xlibkccontacts \
		-Xunixplugin

override_dh_shlibdeps:
	dh_shlibdeps -a -l $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/kopano \
			-l $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)