<?xml version="1.0" encoding="UTF-8"?>

<project name="phpBB" description="The phpBB forum software" default="all" basedir="../">
	<!-- a few settings for the build -->
	<property name="newversion" value="3.0.13-dev" />
	<property name="prevversion" value="3.0.12" />
	<property name="olderversions" value="3.0.2, 3.0.3, 3.0.4, 3.0.5, 3.0.6, 3.0.7, 3.0.7-PL1, 3.0.8, 3.0.9, 3.0.10, 3.0.11" />
	<!-- no configuration should be needed beyond this point -->

	<property name="oldversions" value="${olderversions}, ${prevversion}" />
	<property name="versions" value="${oldversions}, ${newversion}" />

	<!-- These are the main targets which you will probably want to use -->
	<target name="all" depends="clean,prepare,composer,test,docs,package" />
	<target name="build" depends="clean,prepare,composer,test,docs" />

	<target name="prepare">
		<mkdir dir="build/logs" />
		<mkdir dir="build/api" />
		<mkdir dir="build/codebrowser" />
		<mkdir dir="build/coverage" />
		<mkdir dir="build/cpd" />
		<mkdir dir="build/dependencies" />
		<mkdir dir="build/new_version" />
		<mkdir dir="build/new_version/files" />
		<mkdir dir="build/new_version/patches" />
		<mkdir dir="build/new_version/release_files" />
		<mkdir dir="build/new_version/update" />
		<mkdir dir="build/old_versions" />
		<mkdir dir="build/save" />
	</target>

	<target name="clean">
		<delete dir="build/logs" />
		<delete dir="build/api" />
		<delete dir="build/codebrowser" />
		<delete dir="build/coverage" />
		<delete dir="build/cpd" />
		<delete dir="build/dependencies" />
		<delete dir="build/new_version" />
		<delete dir="build/old_versions" />
		<delete dir="build/save" />
	</target>

	<!--
		This target basically just runs composer in the phpBB tree to ensure
		all dependencies are loaded. Additional development dependencies are
		loaded because testing framework may depend on them.
	-->
	<target name="composer">
		<exec dir="phpBB"
			command="php ../composer.phar install --dev"
			checkreturn="true"
			passthru="true" />
	</target>

	<target name="test" depends="clean,prepare,composer">
		<exec dir="."
			command="phpBB/vendor/bin/phpunit
			--log-junit build/logs/phpunit.xml
			--coverage-clover build/logs/clover.xml
			--coverage-html build/coverage"
			passthru="true" />
	</target>

	<target name="test-slow" depends="clean,prepare,composer">
		<exec dir="."
			command="phpBB/vendor/bin/phpunit
			--log-junit build/logs/phpunit.xml
			--configuration phpunit.xml.all
			--group slow
			--coverage-clover build/logs/clover-slow.xml
			--coverage-html build/coverage-slow"
			passthru="true" />
	</target>

	<target name="docs">
		<!-- only works if you setup phpdoctor:
                     git clone https://github.com/peej/phpdoctor.git
                     and then create an executable phpdoctor in your path containing
                     #!/bin/sh
                     php -f /path/to/phpdoctor/phpdoc.php $@
                -->
		<exec dir="build"
			command="phpdoctor phpdoc-phpbb.ini"
			passthru="true" />
	</target>

	<target name="old-version-diffs">
		<foreach list="${oldversions}" param="version" target="old-version-diff" />
	</target>

	<!-- This target retrieves an old version from the git tag release-${version}
	     and creates a diff between that release and the new one -->
	<target name="old-version-diff">
		<echo msg="Retrieving version ${version}" />
		<mkdir dir="build/old_versions/release-${version}" />

		<phingcall target="export">
			<property name="revision" value="release-${version}" />
			<property name="dir" value="build/old_versions/release-${version}" />
		</phingcall>

		<phingcall target="clean-diff-dir">
			<property name="dir" value="build/old_versions/release-${version}" />
		</phingcall>

		<exec dir="build/old_versions" command="LC_ALL=C diff -crNEBwd release-${version} release-${newversion} >
			../new_version/patches/phpBB-${version}_to_${newversion}.patch" escape="false" />
	</target>

	<target name="prepare-new-version">
		<!-- select the currently checked out commit (HEAD) for packaging -->
		<mkdir dir="build/new_version/phpBB3" />
		<phingcall target="export">
			<property name="revision" value="HEAD" />
			<property name="dir" value="build/new_version/phpBB3" />
		</phingcall>

		<!-- copy into directory for diffs -->
		<exec dir="build" command="cp -rp new_version/phpBB3 old_versions/release-${newversion}" />
		<!-- and clean up -->
		<phingcall target="clean-diff-dir">
			<property name="dir" value="build/old_versions/release-${newversion}" />
		</phingcall>

		<!-- create an empty config.php file (not for diffs) -->
		<touch file="build/new_version/phpBB3/config.php" />

	</target>

	<target name="package" depends="clean,prepare,prepare-new-version,old-version-diffs">
		<exec dir="build" command="php -f package.php '${versions}' > logs/package.log" escape="false" />
		<exec dir="build" escape="false"
			command="diff -crNEBwd old_versions/release-${prevversion}/language new_version/phpBB3/language >
				save/phpbb-${prevversion}_to_${newversion}_language.patch" />
		<exec dir="build" escape="false"
			command="diff -crNEBwd old_versions/release-${prevversion}/styles/prosilver new_version/phpBB3/styles/prosilver >
				save/phpbb-${prevversion}_to_${newversion}_prosilver.patch" />
		<exec dir="build" escape="false"
			command="diff -crNEBwd old_versions/release-${prevversion}/styles/subsilver2 new_version/phpBB3/styles/subsilver2 >
				save/phpbb-${prevversion}_to_${newversion}_subsilver2.patch" />

		<exec dir="build" escape="false"
			command="git shortlog --summary --numbered release-${prevversion}...HEAD >
				save/phpbb-${prevversion}_to_${newversion}_git_shortlog.txt" />
		<exec dir="build" escape="false"
			command="git diff --stat release-${prevversion}...HEAD >
				save/phpbb-${prevversion}_to_${newversion}_git_diffstat.txt" />

		<phingcall target="checksum-dir">
			<property name="dir" value="build/new_version/release_files" />
		</phingcall>
	</target>

	<target name="checksum-dir">
		<foreach param="filename" absparam="absfilename" target="checksum-file">
			<fileset dir="${dir}">
				<type type="file" />
			</fileset>
		</foreach>
	</target>

	<target name="checksum-file">
		<echo msg="Creating checksum file for ${absfilename}" />
		<php function="dirname" returnProperty="dir"><param value="${absfilename}"/></php>
		<exec dir="${dir}" command="sha256sum ${filename} > ${filename}.sha256" />
	</target>

	<target name="announcement" depends="prepare">
		<echo msg="Writing download links and checksums for email announcement to save/announcement_email_${newversion}.txt" />
		<exec dir="build" escape="false"
			command="php -f build_announcement.php email '${newversion}' 'new_version/release_files' sha256 >
				save/announcement_email_${newversion}.txt"  />

		<echo msg="Writing download links and checksums for bbcode announcement to save/announcement_bbcode_${newversion}.txt" />
		<exec dir="build" escape="false"
			command="php -f build_announcement.php bbcode '${newversion}' 'new_version/release_files' sha256 >
				save/announcement_bbcode_${newversion}.txt"  />
	</target>

	<target name="changelog" depends="prepare">
		<exec dir="build" escape="false"
			command="php -f build_changelog.php '${newversion}' >
				save/changelog_${newversion}.html"  />
	</target>

	<!--
		This target can be called using phingcall to retrieve a clean
		checkout of a commit from git. It will only export the phpBB directory.
		The properties revision and dir are required.
		This target will remove directories that are not needed in distribution
		and set correct permissions.
	-->
	<target name="export">
		<exec dir="phpBB"
			command="git archive ${revision} | tar -xf - -C ../${dir}"
			checkreturn="true" />

		<!-- Install composer dependencies, if there are any. -->
		<exec dir="${dir}"
			command='php -r "\$j = json_decode(file_get_contents(\"composer.json\")); echo isset(\$j->require);"'
			checkreturn="true"
			outputProperty='composer-has-dependencies' />
		<if>
			<equals arg1="${composer-has-dependencies}" arg2="1" trim="true" />
			<then>
				<!-- We have non-dev composer dependencies -->
				<exec dir="."
					command="git ls-tree ${revision} composer.phar"
					checkreturn="true"
					outputProperty='composer-ls-tree-output' />
				<if>
					<equals arg1="${composer-ls-tree-output}" arg2="" trim="true" />
					<then>
						<fail message="There are composer dependencies, but composer.phar is missing." />
					</then>
					<else>
						<!-- Export the phar, install dependencies, delete phar. -->
						<exec dir="."
							command="git archive ${revision} composer.phar | tar -xf - -C ${dir}"
							checkreturn="true" />
						<exec dir="${dir}"
							command="php composer.phar install --no-dev"
							checkreturn="true"
							passthru="true" />
						<delete file="${dir}/composer.phar" />
					</else>
				</if>
			</then>
			<else>
				<!-- We do not have composer dependencies, do not ship composer files -->
				<delete file="${dir}/composer.json" />
				<delete file="${dir}/composer.lock" />
			</else>
		</if>

		<delete file="${dir}/config.php" />
		<delete dir="${dir}/develop" />
		<delete dir="${dir}/install/data" />

		<echo msg="Setting permissions for checkout of ${revision} in ${dir}" />
		<!-- set permissions of all files to 644, directories to 755 -->
		<exec dir="${dir}" command="find . -type f|xargs chmod 644" escape="false" />
		<exec dir="${dir}" command="find . -type d|xargs chmod 755" escape="false" />
		<!-- set permissions of some directories to 777 -->
		<chmod mode="0777" file="${dir}/cache" />
		<chmod mode="0777" file="${dir}/store" />
		<chmod mode="0777" file="${dir}/files" />
		<chmod mode="0777" file="${dir}/images/avatars/upload" />
	</target>

	<target name="clean-diff-dir">
		<delete dir="${dir}/cache" />
		<delete dir="${dir}/docs" />
		<delete dir="${dir}/files" />
		<delete dir="${dir}/install" />
		<delete dir="${dir}/store" />
		<delete dir="${dir}/vendor" />
	</target>

</project>
