#!/usr/bin/make -f

include /usr/share/dpkg/default.mk
UPSTREAM := $(DEB_VERSION_UPSTREAM)

%:
	dh $@

override_dh_auto_build:
	# Build static classloader for shipping
	phpabtpl composer.json > debian/autoload.php.tpl
	sed -i "s#'/usr/share/php#__DIR__ . '/..#" debian/autoload.php.tpl
	sed -i "s#/src##" debian/autoload.php.tpl
	phpab --output src/Autoload.php \
		--template debian/autoload.php.tpl src
	# Build classloader for tests
	mkdir --parents vendor mimic/bin mimic/share/php/data
	phpabtpl \
		--require-file ../mimic/share/php/PHPUnit/Autoload.php \
		--require-file ../tests/_files/deprecation-trigger/trigger_deprecation.php \
		--require-file ../tests/unit/Event/AbstractEventTestCase.php \
		--require-file ../tests/unit/TextUI/AbstractSourceFilterTestCase.php \
		--require-file ../tests/unit/Framework/MockObject/TestDoubleTestCase.php \
		--require-file ../tests/unit/Metadata/Parser/AttributeParserTestCase.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyArrayTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyBoolTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyCallableTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyFloatTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyInstancesOfTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyIntTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyIterableTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyNullTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyNumericTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyObjectTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyResourceTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyClosedResourceTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyScalarTest.php \
		--require-file ../tests/unit/Framework/Assert/assertContainsOnlyStringTest.php \
		--require-file ../tests/unit/Framework/Assert/assertDirectoryExistsTest.php \
		--require-file ../tests/unit/Framework/Assert/assertFileExistsTest.php \
		--require-file ../tests/unit/Framework/Assert/assertIsNumericTest.php \
		--require-file ../tests/unit/Framework/Assert/assertIsObjectTest.php \
		--require-file ../tests/unit/Framework/Assert/assertIsReadableTest.php \
		--require-file ../tests/unit/Framework/Assert/assertIsResourceTest.php \
		--require-file ../tests/unit/Framework/Assert/assertIsScalarTest.php \
		--require-file ../tests/unit/Framework/Assert/assertIsStringTest.php \
		--require-file ../tests/unit/Framework/Assert/assertIsWritableTest.php \
		--require-file ../tests/unit/Framework/Assert/assertMatchesRegularExpressionTest.php \
		--require-file ../tests/unit/Framework/Assert/assertNullTest.php \
		--require-file ../tests/unit/Framework/Assert/assertSameSizeTest.php \
		--require-file ../tests/unit/Framework/Assert/assertSameTest.php \
		--require-file ../tests/unit/TextUI/Output/Default/ResultPrinterTest.php \
		--require-file ../tests/_files/CoveredFunction.php \
		--require-file ../tests/_files/Generator.php \
		--require-file ../tests/_files/NamespaceCoveredFunction.php \
		--require-file ../tests/end-to-end/_files/listing-tests-and-groups/ExampleAbstractTestCase.php \
		> debian/autoload.tests.php.tpl
	phpab \
		--output vendor/autoload.php \
		--template debian/autoload.tests.php.tpl \
		tests/_files
	# Mimic install path
	cp phpunit mimic/bin
	cp -r src mimic/share/php/PHPUnit
	cp -r /usr/share/php/DeepCopy mimic/share/php
	cp -r /usr/share/php/PharIo mimic/share/php
	cp -r /usr/share/php/PhpParser mimic/share/php
	cp -r /usr/share/php/SebastianBergmann mimic/share/php
	cp -r /usr/share/php/staabm mimic/share/php
	cp -r /usr/share/php/TheSeer mimic/share/php

	# Mimic phpunit.xsd path
	ln -rs . mimic/share/php/data/PHPUnit

override_dh_auto_clean:

override_dh_auto_test:
	./mimic/bin/phpunit --display-skipped

execute_before_dh_installman:
	mkdir --parent $(CURDIR)/debian/tmp
	help2man \
		--help-option=\  \
		--version-string=$(UPSTREAM) \
		--source="phpunit $(UPSTREAM)" \
		--no-info \
		--include=$(CURDIR)/debian/phpunit.1.in \
		"echo -n Usage: && ./phpunit --help|tail -n+4|sed 's/^  phpunit \[options\] <directory>/  or: phpunit [options] <directory>/'" \
		> $(CURDIR)/debian/tmp/phpunit.1

execute_before_dh_installchangelogs:
	for i in $$(ls ChangeLog-*.md -r); do cat $$i >> CHANGELOG; done
