#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

export DH_VERBOSE = 1

%:
	dh $@ --with phpcomposer

override_dh_auto_build:
	mkdir --parents vendor/app
	phpabtpl composer.json > debian/autoload.php.tpl
	phpab \
		--output vendor/autoload.php \
		--template debian/autoload.php.tpl \
		app \
		libs \
		libs/picodb/lib \
		libs/jsonrpc/src
	ln -s ../../app/functions.php vendor/app/functions.php
	printf '%s\n' "$(DEB_VERSION)" > $(CURDIR)/app/version.txt
	@echo '  [*]' Building and minimizing the CSS
	$(CURDIR)/cli css
	@echo '  [*]' Building and minimizing the JavaScript
	$(CURDIR)/cli js
	@echo '  [*]' Generating manpage
	ln -s ../cli debian/kanboard-cli
	help2man --name="administrative interface for kanboard" \
		--section=8 \
		--no-info \
		--help-option=list \
		--version-string=$(DEB_VERSION) \
		--output=debian/kanboard-cli.8 \
		debian/kanboard-cli

override_dh_auto_test: export LOG_DRIVER := file
override_dh_auto_test: export LOG_FILE := /dev/null
override_dh_auto_test:
	mkdir --parents vendor
	phpabtpl \
		--basedir vendor \
		--require symfony/stopwatch \
		--require-file autoload.php \
		> debian/autoload.tests.php.tpl
	phpab \
		--output vendor/autoload.tests.php \
		--template debian/autoload.tests.php.tpl \
		tests/units
	phpunit --configuration tests/units.sqlite.xml --bootstrap vendor/autoload.tests.php

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog

execute_after_dh_install:
	rm -f $(CURDIR)/debian/kanboard/usr/share/kanboard/libs/*/LICENSE
	rm -f $(CURDIR)/debian/kanboard/usr/share/kanboard/libs/*/README.*

execute_after_dh_fixperms:
	chown www-data:www-data \
		$(CURDIR)/debian/kanboard/var/cache/kanboard \
		$(CURDIR)/debian/kanboard/var/lib/kanboard/data \
		$(CURDIR)/debian/kanboard/var/lib/kanboard/files \
		$(CURDIR)/debian/kanboard/var/lib/kanboard/plugins
	find $(CURDIR)/debian/kanboard -type f -perm /111 \
		\( -name '*.php' -o -name '*.dat' \) -print0 \
		| xargs -0 chmod -x
