<?xml version="1.0" encoding="UTF-8"?>
<project name="Mirror Repository" default="mirror">
	<property name="framework.location" location="/shared/common/buckminster-3.6" />

	<fail message="repository property must be defined" unless="repository" />

	<target name="mirror">
		<antcall target="-call.operation">
			<param name="operation.name" value="mirror" />
		</antcall>
	</target>

	<target name="-mirror">
		<p2.mirror>
			<repository location="${user.dir}" append="true" />
			<source>
				<repository location="${repository}" />
			</source>
		</p2.mirror>
	</target>

	<target name="-call.operation">
		<condition property="call.operation.target" value="-${operation.name}" else="-call.operation.indirectly">
			<isset property="eclipse.running" />
		</condition>
		<antcall target="${call.operation.target}" />
	</target>

	<target name="-call.operation.indirectly" depends="-locate.framework.launcher">
		<tempfile property="temporary.workspace" destdir="${java.io.tmpdir}" prefix="temporary-workspace-" />
		<java jvm="${java.home}/bin/java" jar="${framework.launcher}" fork="true" dir="${user.dir}" failonerror="true">
			<syspropertyset>
				<propertyref builtin="commandline" />
			</syspropertyset>
			<arg value="-data" />
			<arg value="${temporary.workspace}" />
			<arg value="-application" />
			<arg value="org.eclipse.ant.core.antRunner" />
			<arg value="-buildfile" />
			<arg value="${ant.file}" />
			<arg value="${operation.name}" />
		</java>

		<!--
			somewhat more complicated than simple <delete dir="${temporary.workspace}" />
			but this way it does not produce the "Deleting directory" message
		-->
		<delete dir="${temporary.workspace}" includeemptydirs="true">
			<or>
				<type type="dir" />
				<type type="file" />
			</or>
		</delete>
	</target>

	<target name="-locate.framework.launcher" unless="framework.launcher">
		<pathconvert property="framework.launcher">
			<first count="1">
				<sort>
					<fileset dir="${framework.location}/plugins">
						<filename name="org.eclipse.equinox.launcher_*.jar" />
					</fileset>
					<reverse xmlns="antlib:org.apache.tools.ant.types.resources.comparators">
						<date />
					</reverse>
				</sort>
			</first>
		</pathconvert>
	</target>
</project>
