<?xml version="1.0"?>

<project name="Releaser" default="info" basedir="../../">
	<property name="core_config_file" value="web/intranet/common/config.php" />
	<property name="version_code" value="01.01" />
	<if>
		<not><isset property="app"/></not>
		<then>
			<property name="app" value="core" />
		</then>
	</if>
	<property name="application" value="${app}" />


	<target name="info" description="Show help">
		<echo>

		</echo>
	</target>

	<target name="release_minor" depends="ask_extra_params,-find_next_minor_version,make_db_patch,install_make_schema,make_cleanup_file" description="Build DB schema for new MINOR version release">
		<echo msg="New minor version released. Version name ${version_name}, version code: ${version_code}"/>
	</target>

	<target name="release_major" depends="ask_extra_params,-find_next_major_version,make_db_patch,install_make_schema,make_cleanup_file" description="Build DB schema for new MAJOR version release">
		<echo msg="New major version released. Version name ${version_name}, version code: ${version_code}"/>
	</target>


	<target name="make_db_patch">
		<combinesql application="${application}" version="${version_code}" versionname="${version_name}" delete="1" />
	</target>

	<target name="install_make_schema">
		<buildschema application="${application}" />
		<writeversion application="${application}" version="${version_code}" versionname="${version_name}" />
	</target>

	<target name="-find_next_minor_version" hidden="true">
		<readversion application="${application}" from="files" property="cur_version" />
		<nextversion type="minor" property="version_code" curversion="${cur_version}" />
		<echo msg="Next minor version is ${version_code}"/>
	</target>

	<target name="-find_next_major_version" hidden="true">
		<readversion application="${application}" from="files" property="cur_version" />
		<nextversion type="major" property="version_code" curversion="${cur_version}" />
		<echo msg="Next major version is ${version_code}"/>
	</target>


	<target name="ask_extra_params">
		<if>
			<and>
				<not><isset property="changelog_updated" /></not>
				<not><equals arg1="core" arg2="${application}" /></not>
			</and>
			<then>
				<propertyprompt propertyName="changelog_updated" promptText="Have you updated the change log (y/n)?" promptCharacter=":" />
			</then>
			<else>
				<property name="changelog_updated" value="y" />
			</else>
		</if>
		<if>
			<not><or>
				<equals arg1="y" arg2="${changelog_updated}" />
				<equals arg1="yes" arg2="${changelog_updated}" />
			</or></not>
			<then>
				<fail message="Please update the change log"/>
			</then>
		</if>

		<if>
			<not><isset property="version_name" /></not>
			<then>
				<propertyprompt propertyName="version_name" promptText="New version name (such as 8.0.3)" promptCharacter=":" />
			</then>
		</if>
		<if>
			<not><isset property="previous_version_tag" /></not>
			<then>
				<propertyprompt propertyName="previous_version_tag" promptText="Previous version git tag (such as 8.0.2)" promptCharacter=":" />
			</then>
		</if>
	</target>


	<target name="make_cleanup_file">
		<if>
			<or>
				<not><isset property="previous_version_tag"/></not>
				<equals arg1="" arg2="${previous_version_tag}"/>
			</or>
			<then>
				<echo msg="Previous tag name not given, cannot create cleanup file" />
			</then>
			<else>
				<if>
					<equals arg1="${application}" arg2="core" />
					<then>
						<property name="git_diff_dir" value="." />
					</then>
					<else>
						<if>
							<isset property="custom" />
							<then>
								<property name="git_diff_dir" value="web/custom/${application}" />
							</then>
							<else>
								<property name="git_diff_dir" value="web/intranet/${application}" />
							</else>
						</if>
					</else>
				</if>

				<exec command="git diff ${previous_version_tag} HEAD --summary" outputProperty="git_diff" dir="${git_diff_dir}" />
				<makecleanup application="${application}" diff="${git_diff}" version="${version_code}"/>
			</else>
		</if>
	</target>


	<taskdef name="readversion" classname="web.intranet.setup.classes.Phing.ReadVersionTask" />
	<taskdef name="nextversion" classname="web.intranet.setup.classes.Phing.NextVersionTask" />
	<taskdef name="combinesql" classname="web.intranet.setup.classes.Phing.CombineSQLTask" />
	<taskdef name="buildschema" classname="web.intranet.setup.classes.Phing.BuildSchemaFileTask" />
	<taskdef name="writeversion" classname="web.intranet.setup.classes.Phing.WriteVersionTask" />
	<taskdef name="makecleanup" classname="web.intranet.setup.classes.Phing.MakeCleanupTask" />
</project>
