<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="PDE-Build" default="launch" basedir=".">

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

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

	<target name="prepareLauncherAndProvisionerDirectory">
		<if>
			<not>
				<or>
					<available file="${launcher}" />
					<available file="${provisioner}" />
				</or>
			</not>
			<then>
				<mkdir dir="${launcher}" />
				<trycatch>
					<try>
						<antcall target="prepareLauncherAndProvisioner" />
					</try>
					<catch>
						<echo message="Preparing launcher failed. Cleaning up..." />
						<delete dir="${launcher}" />
						<echo message="Done." />
					</catch>
				</trycatch>
			</then>
		</if>
	</target>

	<target name="prepareLauncherAndProvisioner">
		<echo message="Preparing launcher and provisioner ..." />

		<property name="download.eclipse.src" value="${downloadMirror}/eclipse/downloads/drops/${launcherBuildType}-${launcherBuildMilestone}-${launcherBuildID}" />
		<property name="eclipse.sdk.filename" value="eclipse-SDK-${launcherBuildMilestone}-${launcheros}-${launcherws}-${launcherarch}.${launcherArchiveFormat}" />

		<!-- download launcher if not already contained in downloads directory -->
		<if>
			<not>
				<available file="${downloadDirectory}/${eclipse.sdk.filename}" property="downloadLauncherNeccessary" />
			</not>
			<then>
				<get src="${download.eclipse.src}/${eclipse.sdk.filename}" dest="${downloadDirectory}/${eclipse.sdk.filename}" />
			</then>
		</if>

		<!-- use unzip executable of choice -->
		<if>
			<equals arg1="${launcherArchiveFormat}" arg2="zip" />
			<then>
				<unzip src="${downloadDirectory}/${eclipse.sdk.filename}" dest="${launcher}/" />
				<unzip src="${downloadDirectory}/${eclipse.sdk.filename}" dest="${provisioner}/" />
			</then>
			<else>
				<untar src="${downloadDirectory}/${eclipse.sdk.filename}" dest="${launcher}/" compression="gzip" />
				<untar src="${downloadDirectory}/${eclipse.sdk.filename}" dest="${provisioner}/" compression="gzip" />
			</else>
		</if>

		<!-- if we are on macosx or linux, ensure builder is executable -->
		<if>
			<or>
				<equals arg1="${launcheros}" arg2="macosx" />
				<equals arg1="${launcheros}" arg2="linux" />
			</or>
			<then>
				<chmod file="${launcherLocation}/eclipse" perm="ugo+rx" />
				<chmod file="${provisionerLocation}/eclipse" perm="ugo+rx" />
			</then>
		</if>

		<echo message="Preparing launcher and provisioner done." />
	</target>

	<target name="launch">
		<if>
			<not>
				<available file="${downloadDirectory}" property="downloadDirectoryExists" />
			</not>
			<then>
				<mkdir dir="${downloadDirectory}" />
			</then>
		</if>

		<echo message="Cleaning up report directory ${reportDirectory}" />
		<delete dir="${reportDirectory}" />
		<mkdir dir="${reportDirectory}" />

		<echo message="Preparing Launcher and Provisioner" />
		<antcall target="prepareLauncherAndProvisionerDirectory" />

		<echo message="Starting PDE build with launcher: ${launcherLocation}" />
		<java classname="org.eclipse.equinox.launcher.Main" fork="true" failonerror="true">
			<arg value="-application" />
			<arg value="org.eclipse.ant.core.antRunner" />
			<arg value="-clean" />
			<arg value="-nosplash" />
			<arg value="-consoleLog" />
			<arg value="-nl" />
			<arg value="en_US" />
			<arg value="-data" />
			<arg value="${launcherWorkspace}" />
			<arg value="-buildfile" />
			<arg value="run.xml" />
			<classpath>
				<fileset dir="${launcherLocation}/plugins">
					<include name="org.eclipse.equinox.launcher_*.jar" />
				</fileset>
			</classpath>
		</java>
	</target>
</project>
