<project name="run.xml" default="all" basedir=".">

	<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="lib/ant-contrib-1.0b3.jar" />

	<tstamp>
		<format property="buildTimestamp" pattern="yyyyMMddHHmm" />
	</tstamp>

	<property environment="env" />
	<property file="run.properties" />
	<property file="build/build.properties" />

	<target name="prepareBuildDirectory">
		<echo message="Preparing build directory..." />
		<delete dir="${buildDirectory}" />
		<mkdir dir="${buildDirectory}" />

		<antcall target="prepareRepositoriesDirectory" />
		<antcall target="prepareFeatures" />
		<antcall target="preparePlugins" />
	</target>

	<target name="prepareRepositoriesDirectory">
		<!-- base only has to be prepared, if it does not exist or if the user explicitly specifies to do so (skipBase=false) -->

		<echo message="Skipping repositories: ${skipRepositories}" />
		<if>
			<or>
				<equals arg1="${skipRepositories}" arg2="false" />
				<not>
					<available file="${repoBaseLocation}" />
				</not>
			</or>
			<then>
				<trycatch property="errorMessage" >
					<try>
						<mkdir dir="${repoBaseLocation}" />
						<antcall target="prepareRepositories" />
					</try>
					<catch>
						<echo message="Preparing repositories failed. Cleaning up..." />
						<delete dir="${repoBaseLocation}" />
						<fail message="Preparing repositories failed: ${errorMessage}" />
					</catch>
				</trycatch>
			</then>
		</if>
	</target>

	<target name="prepareRepositories">
		<echo message="Preparing repositories ..." />

		<!-- project updates -->
		<!-- TODO change to release update site -->
		<p2.mirror source="${downloadMirror}/eclipse/updates/3.7milestones" destination="file:${repoBaseLocation}/eclipse_updates_3_7">
			<iu id="org.eclipse.platform.ide" />
			<slicingOptions includeOptional="false" includeNonGreedy="false" followStrict="true" />
		</p2.mirror>

		<!-- indigo releases -->
		<p2.mirror source="${downloadMirror}/releases/indigo" destination="file:${repoBaseLocation}/releases_indigo">
			<iu id="org.eclipse.emf.sdk.feature.group" />
			<iu id="org.eclipse.gmf.runtime.notation.sdk.feature.group" />
			<iu id="org.eclipse.gmf.runtime.sdk.feature.group" />
			<iu id="org.eclipse.xtext.sdk.feature.group" />
			<!--<slicingOptions includeOptional="false" includeNonGreedy="false" followStrict="true" />-->
		</p2.mirror>
			
		<echo message="Preparing repositories done." />
	</target>

	<target name="preparePlugins">
		<delete dir="${buildDirectory}/plugins" />
		<mkdir dir="${buildDirectory}/plugins" />
		<!-- Copy plugins -->
		<echo>Copy plugins from ${repository} to ${buildDirectory}/plugins</echo>
		<copy todir="${buildDirectory}/plugins">
			<fileset dir="${buildWorkspace}/XtextUtils_repository/plugins" />
		</copy>
	</target>

	<target name="prepareFeatures">
		<delete dir="${buildDirectory}/features" />
		<mkdir dir="${buildDirectory}/features" />
		<!-- Copy features -->
		<echo>Copy features from ${repository} to ${buildDirectory}/features</echo>
		<copy todir="${buildDirectory}/features">
			<fileset dir="${buildWorkspace}/XtextUtils_repository/features" />
		</copy>

		<!-- remove the "-feature" suffix of all feature projects in the features folder -->
		<for param="currentFeatureDir" keepgoing="true">
			<path>
				<dirset dir="${buildDirectory}/features">
					<include name="*-feature" />
				</dirset>
			</path>
			<sequential>
				<propertyregex override="yes" property="renamedFeatureDir" input="@{currentFeatureDir}" regexp="(.*)-feature" select="\1" />
				<echo message="Moving @{currentFeatureDir} to ${renamedFeatureDir}" />
				<move todir="${renamedFeatureDir}">
					<fileset dir="@{currentFeatureDir}" />
				</move>
			</sequential>
		</for>
	</target>

	<target name="all">
		<echo message="Preparing build directory ${buildDirectory}" />
		<antcall target="prepareBuildDirectory" />

		<!-- build the XJI feature -->
		<ant antfile="build.xml" dir="build" target="all" inheritall="true">
			<property name="builder" value="${basedir}/build" />
			<property name="libs" value="${basedir}/lib" />
			<property name="topLevelElementType" value="feature" />
			<property name="topLevelElementId" value="de.itemis.xtext.utils.jface" />
		</ant>

		<!-- build the XGI feature -->
		<ant antfile="build.xml" dir="build" target="all" inheritall="true">
			<property name="builder" value="${basedir}/build" />
			<property name="libs" value="${basedir}/lib" />
			<property name="topLevelElementType" value="feature" />
			<property name="topLevelElementId" value="de.itemis.xtext.utils.gmf" />
		</ant>
	</target>
</project>
