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

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

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

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

	<target name="-add.named" if="composite.name">
		<p2.composite.repository>
			<repository location="${user.dir}" name="${composite.name}" />
			<add>
				<repository location="${child.repository}" />
			</add>
		</p2.composite.repository>
	</target>

	<target name="-add" depends="-add.named" unless="composite.name">
		<p2.composite.repository>
			<repository location="${user.dir}" />
			<add>
				<repository location="${child.repository}" />
			</add>
		</p2.composite.repository>
	</target>

	<target name="-remove">
		<p2.composite.repository>
			<repository location="${user.dir}" />
			<remove>
				<repository location="${child.repository}" />
			</remove>
		</p2.composite.repository>
	</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>

	<target name="unspecified">
		<fail message="you must specify a target explicitly" />
	</target>
</project>
