#!/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_VERSION=7.2
PHP_API_DATE=$(shell php-config$(PHPVERSION) --phpapi)
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) \
    --with-distro=debian \
    --prefix=/usr \
    --localstatedir=/var \
    --sysconfdir=/etc \
    --mandir=\$${prefix}/share/man \
    --infodir=\$${prefix}/share/info \
    --with-php-config=/usr/bin/php-config$(PHPVERSION) \
    --with-quotatemplate-prefix=/etc/kopano/quotamail \
    --with-userscript-prefix=/etc/kopano/userscripts \
    --disable-static \
    --enable-epoll \
    --enable-unicode \
    --with-distro=debian \
    $(CONFFLAG) \
    $(NULL)


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

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

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:
	[ ! -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
	rm -f $(INSTDIR)/usr/lib/*/*.la
	rm -f $(INSTDIR)/usr/lib/*/kopano/*.la
	# 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 {} \;

	# add php:Depends
	echo "php:Depends=phpapi-$(PHP_API_DATE)" > debian/php$(PHP_VERSION)-mapi.substvars

	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/
	# remove magic.py, it's provided by python-magic
	rm $(INSTDIR)/usr/lib/python2.7/dist-packages/kopano_search/magic.py
	dh_install

	for p in server dagent search; do \
	  dh_apparmor --profile-name=usr.sbin.kopano-$${p} -pkopano-$${p}; \
	done

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

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)
