<?xml-stylesheet type="text/xsl" href="file://C:\ant\etc\antex\antprettybuild\antprettybuild-3.1.1.xsl"?>
<project name="rusticiTranslator" default="launch" basedir="../" xmlns:dn="antlib:org.apache.ant.dotnet">

	<description>
        This script automates translation of a C# svn branch to a Java branch
    </description>

	<property file="${basedir}/RusticiLogicTranslator/launch.properties" />
	<property file="${user.home}/${ant.project.name}.build.properties" />
	<property file="${user.home}/build.properties" />
	<property file="${basedir}/RusticiLogicTranslator/build.properties" />

	<!-- load the ant-dotnet task, see http://ant.apache.org/antlibs/dotnet/index.html -->
	<taskdef uri="antlib:org.apache.ant.dotnet" resource="org/apache/ant/dotnet/antlib.xml" classpath="${builder.ant.lib}/ant-dotnet-1.0.jar" />

	<!-- Load AntForm tasks -->
	<taskdef name="antform" classname="com.sardak.antform.AntForm" classpath="${builder.ant.lib}/antform.jar"/>
	<taskdef name="antmenu" classname="com.sardak.antform.AntMenu" classpath="${builder.ant.lib}/antform.jar"/>

	<target name="launch">
		<antform title="C# to Java Translation" save="${basedir}/RusticiLogicTranslator/launch.properties">
			<label>Enter a scratch directory to use for building</label>
			<fileSelectionProperty label="build directory : " property="build.dir" directoryChooser="true" />
			<separator />
			<label>Enter the locations of the branches you want to translate from / to</label>
			<textProperty label="ScormEngineNet branch : " property="csharp.project.full" />
			<textProperty label="ScormEngineJava branch : " property="java.project.full" />
			<separator />
			<booleanProperty label="Commit Java code after translation " property="is.commit.javacode" />
			<booleanProperty label="Refresh and Rebuild cs2j executable " property="is.rebuild.cs2j" />
			<controlbar>
				<button label="Cancel" type="cancel" target="cancel" newproject="false"/>
				<button label="Reset" type="reset" />
				<button label="OK" type="ok"/>
			</controlbar>
		</antform>
		<antcall target="ok" />
	</target>

	<target name="cancel">
		<echo message="Translation cancelled by user" />
		<property name="is.run.cancelled" value="true" />
	</target>

	<target name="ok" unless="is.run.cancelled">
		<!--antcall target="dumpProperties" inheritall="false" / -->
		<antcall target="svntranslate" inheritall="false" />
	</target>

	<target name="svntranslate" description="translate a C# branch to a Java branch" 
		depends="clean,init,buildtranslator,checkoutCSharpJavaCode,translateCSharpToJava,setAddedFiles,commitAdded,setRemovedFiles,commitRemoved,commitJavaCode">
	</target>

	<!-- is.rebuild.cs2j can be set through launch window. rebuild iff var is set and not equal to false -->
	<target name="-svn.rebuild.cs2j.check">
		<condition property="internal.is.rebuild.cs2j">
			<and>
			    <isset property="is.rebuild.cs2j" />
				<not>
				    <equals arg1="${is.rebuild.cs2j}" arg2="false" />
				</not>
			</and>
		</condition>
	</target>

	<target name="buildtranslator" depends="-svn.rebuild.cs2j.check" if="internal.is.rebuild.cs2j">
		<antcall target="refreshtranslator" />
		<ant antfile="build.xml" target="clean" />
		<ant antfile="build.xml" target="buildTranslator" inheritall="true" />
	</target>

	<target name="refreshtranslator" if="is.cs2j.svnwc">
		<dn:dotnetexec executable="${svn.exe}">
			<arg value="update" />
			<arg value="${svntxexe.svnwc.dir}" />
		</dn:dotnetexec>
	</target>

	<target name="checkoutCSharpJavaCode" unless="is.donot.checkout.code">
		<mkdir dir="${csharpcode.dir}" />
		<delete failonerror="true" dir="${csharpcode.dir}" />
		<mkdir dir="${javacode.svnwc.dir}" />
		<delete failonerror="true" dir="${javacode.svnwc.dir}" />
		<mkdir dir="${javacode.old.svnwc.dir}" />
		<delete failonerror="true" dir="${javacode.old.svnwc.dir}" />
		<dn:dotnetexec executable="${svn.exe}">
			<arg value="export" />
			<arg value="${csharpcode.branch.url}" />
			<arg value="${csharpcode.dir}" />
		</dn:dotnetexec>
		<dn:dotnetexec executable="${svn.exe}">
			<arg value="checkout" />
			<arg value="${javacode.branch.url}" />
			<arg value="${javacode.svnwc.dir}" />
		</dn:dotnetexec>
	</target>

	<target name="checkoutCS2JProject" unless="is.donot.checkout.code">
		<mkdir dir="${cs2j.dir}" />
		<delete failonerror="true" dir="${cs2j.dir}" />
		<dn:dotnetexec executable="${svn.exe}">
			<arg value="export" />
			<arg value="${cs2j.branch.url}" />
			<arg value="${cs2j.dir}" />
		</dn:dotnetexec>
	</target>

	<target name="makeCopyJavaCode">
		<copy todir="${javacode.old.svnwc.dir}">
			<fileset dir="${javacode.svnwc.dir}" defaultexcludes="false" />
		</copy>
	</target>
	
	<target name="translateCSharpToJava" depends="buildtranslator,checkoutCS2JProject,checkoutCSharpJavaCode,makeCopyJavaCode">
		<!-- Delete existing java files, they will be recreated by the translation if neccessary -->
		<delete dir="${javacode.svnwc.dir}">
			<include name="**/*.java" />
		</delete>
		<ant antfile="build.xml" target="translateCS2J">
			<!-- There are no cheats -->
			<property name="cheats.dir" value="${cheats.dir}" />
			<property name="cs.app.dir" value="${csharpcode.dir}" />
			<property name="java.output.dir" value="${javacode.svnwc.dir}" />
			<property name="cs.tx.dir" value="${csharpcode.tx.dir}" />
		</ant>
	</target>

	<!-- is.commit.javacode can be set through launch window. rebuild iff var is set and not equal to false -->
	<target name="-svn.commit.javacode.check">
		<condition property="internal.is.commit.javacode">
			<and>
			    <isset property="is.commit.javacode" />
				<not>
				    <equals arg1="${is.commit.javacode}" arg2="false" />
				</not>
			</and>
		</condition>
	</target>

	<target name="commitJavaCode" depends="-svn.commit.javacode.check" if="internal.is.commit.javacode">
		<dn:dotnetexec executable="${svn.exe}">
			<arg value="commit" />
			<arg value="--message" />
			<arg value="${svn.commit.msg.java}" />
			<arg value="${javacode.svnwc.dir}" />
		</dn:dotnetexec>
	</target>

	<target name="dumpProperties" description="output all set properties, a useful sanity check">
		<echoproperties />
	</target>

	<target name="init" depends="">
		<mkdir dir="${svntx.build.dir}" />
	</target>

	<target name="clean" depends="" description="clean out build area">
		<!-- create dir if it doesn't exist, so delete only fails if there is a real problem -->
		<mkdir dir="${svntx.build.dir}"/>
		<delete failonerror="true" dir="${svntx.build.dir}" />
	</target>

	<target name="setAddedFiles">
		<pathconvert property="svn.added.files" pathsep='" "' setonempty="false">
			<fileset dir="${javacode.svnwc.dir}" includes="**/*.java" id="java.added.fileset">
				<present present="srconly" targetdir="${javacode.old.svnwc.dir}" />
			</fileset>
		</pathconvert>
	</target>

	<target name="commitAdded" if="svn.added.files">
		<dn:dotnetexec executable="${svn.exe}">
			<arg value="add" />
			<arg value='"${svn.added.files}"' />
		</dn:dotnetexec>
	</target>

	<target name="setRemovedFiles">
		<pathconvert property="svn.removed.files" pathsep='" "' setonempty="false">
			<fileset dir="${javacode.old.svnwc.dir}" includes="**/*.java" id="java.removed.fileset">
				<present present="srconly" targetdir="${javacode.svnwc.dir}" />
			</fileset>
		</pathconvert>
	</target>

	<!-- We only remove files from subversion if there are files to remove and is.commit.javacode is set -->
	<target name="-svn.remove.files.check" depends="-svn.commit.javacode.check">
		<condition property="is.svn.remove.files">
			<and>
				<isset property="svn.removed.files"/>
				<isset property="internal.is.commit.javacode"/>
			</and>
		</condition>
	</target>

	<target name="commitRemoved" depends="-svn.remove.files.check" if="is.svn.remove.files">
		<dn:dotnetexec executable="${svn.exe}">
			<arg value="remove" />
			<arg value='"${svn.removed.files}"' />
		</dn:dotnetexec>
		<dn:dotnetexec executable="${svn.exe}">
			<arg value="commit" />
			<arg value="--message" />
			<arg value="${svn.commit.removed.msg.java}" />
			<arg value="${javacode.old.svnwc.dir}" />
		</dn:dotnetexec>
	</target>

</project>