#!/usr/bin/make -f

#export DH_VERBOSE=1

include /usr/share/dpkg/pkg-info.mk
RELEASE := $(DEB_DISTRIBUTION)
PHP_VERSION = $(shell basename $$(readlink -f /usr/bin/php-config) | sed 's/php-config//g')

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

%:
	dh $@ 

check_php_version:
	@if [ "x$(PHP_VERSION)" = "x" ]; then \
		echo "Variable \$$(PHP_VERSION) is empty! Giving up!" ;\
		exit 99 ;\
	else \
		echo "##########################" ;\
		echo "# Using PHP_VERSION: '$(PHP_VERSION)'" ;\
		echo "##########################" ;\
	fi

override_dh_auto_clean:

override_dh_auto_build: check_php_version
	# now let's build the stuff
	make -j$(NUMJOBS)
	mkdir $(CURDIR)/debian/tmp
	cp -a $(CURDIR)/deploy $(CURDIR)/debian/tmp/
	sed -e "s/@version@/$(PHP_VERSION)/g" debian/kopano-webapp-apache2.postinst.in > debian/kopano-webapp-apache2.postinst
	sed -e "s/@version@/$(PHP_VERSION)/g" debian/kopano-webapp-lighttpd.postinst.in > debian/kopano-webapp-lighttpd.postinst
	sed -e "s/@version@/$(PHP_VERSION)/g" debian/kopano-webapp-nginx.postinst.in > debian/kopano-webapp-nginx.postinst

override_dh_install:
	dh_install -X.js.map -X*debug.js -X.md
	# figure out package version
	echo $(DEB_VERSION) > \
		$(CURDIR)/debian/kopano-webapp-common/usr/share/kopano-webapp/version
	# remove additional license files
	rm $(CURDIR)/debian/kopano-webapp-common/usr/share/kopano-webapp/client/tinymce-plugins/powerpaste/License.txt
	sed -i "s/@version@/$(PHP_VERSION)/g" debian/kopano-webapp-nginx/etc/nginx/sites-available/kopano-webapp.conf

override_dh_fixperms:
	dh_fixperms -Xvar/lib/kopano-webapp/tmp
	chown www-data:www-data $(CURDIR)/debian/kopano-webapp-common/var/lib/kopano-webapp/tmp
	find $(CURDIR)/debian/kopano-webapp-common/usr/share/kopano-webapp -type f -exec chmod -x {} \;

override_dh_auto_test:
