<?xml version="1.0"?>
<project name="jdiffchaser" default="all" basedir=".">

    <property file="jdiffchaser.properties"/>

    <property environment="env"/>

    <property name="conf"    value="conf"/>
    <property name="build" value="build"/>
    <property name="sources" value="src"/>
    <property name="dist" value="dist"/>
    <property name="release" value="release"/>
    <property name="lib" value="lib"/>
    <property name="testlib" value="testlib"/>
    <property name="samples.dir" value="samplestests"/>
    <property name="tools.dir" value="tools"/>
    <property name="docs.dir" value="docs"/>
    <property name="ontopprojects.dir" value="NativeRCtrlFrameOnTop"/>
    <property name="schema.dir" value="schema"/>
    <property name="sources.test" value="test"/>
    <property name="build.test" value="build_test"/>

    <fileset id="compile.classpath.files.1" dir="." >
        <include name="${lib}/*.jar"/>
    </fileset>

    <fileset id="compile.classpath.files.2" dir="." >
        <include name="dist/jdiffchaser-conf-${version}.jar"/>
    </fileset>

    <path id="compile.classpath">
    	<fileset refid="compile.classpath.files.1"/>
    	<fileset refid="compile.classpath.files.2"/>
    </path>

    <path id="run.classpath">            
    	<pathelement path="${build}"/>
        <path refid="compile.classpath"/>
    </path>
    
    <path id="test.classpath">
        <fileset dir="${testlib}">
                <include name="junit*.jar"/>
                <include name="javassist.jar"/>
        </fileset>
        <path refid="run.classpath"/>
        <pathelement path="${build.test}"/>
    </path>

    <description>
        This buildfile builds the jDiffChaser.
    </description>

    <target name="all" description="Builds dist/${ant.project.name}-${version}.jar">
        <echo message="Java home is ${env.JAVA_HOME}"/>
        <ant antfile="castorbuild.xml" target="all"/>
    	<antcall target="compile"/>
    	<antcall target="compile_test"/>
    	<antcall target="jar"/>
    </target>
    
    <!-- Common targets  -->
    <target name="clean" description="-> Cleans all" depends="clean_test">
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
        <delete dir="screenshots"/>
        <delete dir="sketchbook-screenshots1"/>
        <delete dir="sketchbook-screenshots2"/>
        <delete dir="${samples.dir}/sketchbook-sample/failed"/>
    </target>
    
    <target name="clean_test">
        <delete dir="${build.test}"/>
    </target>        

    <target name="compile">
        
        <echo message="Compiling project ${ant.project.name}"/>

        <!-- clean_output output="${build}" files="compile.classpath"/-->
        <mkdir  dir="${build}" />
        <javac	fork="yes"
        		executable="${env.JAVA_HOME}/bin/javac"
        		memoryInitialSize="128m"
        		memoryMaximumSize="512m"
        		debug="true"
        		deprecation="no"
        		destdir="${build}"
        		srcdir="${sources}"
        		verbose="no"
                        target="${java.target}"
                        source="${java.source}">
			<classpath refid="compile.classpath"/>
        </javac>
        <!-- copying resource files : .au .png .jpg .jpeg .gif -->
        <copy todir="${build}">
            <fileset dir="${sources}">
                <include name="**/*.au"/>
                <include name="**/*.png"/>
                <include name="**/*.jpg"/>
                <include name="**/*.jpeg"/>
                <include name="**/*.gif"/>
                <include name="**/*.xml"/>
            </fileset>
        </copy>
    </target>

    <target name="sourcetest_available">
        <available file="${sources.test}" type="dir" property="sourcetest.dir.exits"/>
    </target>

    <target name="compile_test" depends="compile,sourcetest_available" if="sourcetest.dir.exits">
        <mkdir  dir="${build.test}" />
        <javac	fork="yes"
                        executable="${env.JAVA_HOME}/bin/javac"
                        memoryInitialSize="128m"
                        memoryMaximumSize="512m"
                        debug="true"
                        deprecation="no"
                        destdir="${build.test}"
                        srcdir="${sources.test}"
                        verbose="no"
                        target="${java.target}"
                        source="${java.source}">
                        <classpath refid="test.classpath"/>
                        <classpath path="${build}"/>
        </javac>
    </target>

    <target name="jar">
        <property name="main.class" value="no.main.class.defined"/>
            <tstamp>
                    <format locale="en" pattern="yyDDD-1" property="build.time" unit="millisecond"/>
            </tstamp>
            <property name="jar.version" value="${impl.version}, Build ${build.time}"/>

            <mkdir dir="${dist}" />
            <jar jarfile="${dist}/${ant.project.name}-${version}.jar"
                basedir="${build}"
                compress="true"
                index="true" >
                    <manifest>
                <attribute name="Main-Class"                    value="${main.class}"/>
                            <attribute name="Built-By"                      value="${user.name}"/>
                            <section name="org/jdiffchaser/">
                                    <attribute name="Component"                 value="${ant.project.name}"/>
                                    <attribute name="Specification-Version"     value="${impl.version}"/>
                                    <attribute name="Specification-Title"       value="${ant.project.name}"/>
                                    <attribute name="Specification-Vendor"      value="jdiffchaser team"/>
                                    <attribute name="Implementation-Title"      value="org.jdiffchaser"/>
                                    <attribute name="Implementation-Version"    value="${jar.version}"/>
                                    <attribute name="Implementation-Vendor"     value="jdiffchaser team"/>
                            </section>
                    </manifest>
            </jar>
    </target>
    
    <target name="set-os">
        <condition property="windows">
            <os family="windows"/>
        </condition>
        <condition property="macosx">
            <and>
                <os family="mac"/>
                <os family="unix"/>
            </and>
        </condition>        
    </target>

    <target name="set-mac-args" depends="set-os" if="macosx">
        <property name="library.path" value="./tools/nativelibs/macosx"/>
        <property name="operating.system" value="osx"/>
    </target>

    <target name="set-win-args" depends="set-os" if="windows">
        <property name="library.path" value="./tools/nativelibs/Win32"/>
        <property name="operating.system" value="win"/>
    </target>    
    
    <target name="run-recorder-sketchbook-sample" depends="compile,set-win-args,set-mac-args" description="Runs the sketchbook sample recorder">      
        <echo message="library.path is : ${library.path}"/>
        <echo message="Java home is ${env.JAVA_HOME}"/>
        <parallel>
            <sequential>
                <java   classname="org.jdiffchaser.testing.DefaultRecorder"
                        fork="yes"
                        spawn="false"
                        jvm="${env.JAVA_HOME}/bin/java" >
                    <classpath refid="run.classpath"/>
                    <jvmarg value="-Xmx128M"/>
                    <jvmarg value="-Djava.library.path=${library.path}"/>
                    <arg value="org.jdiffchaser.samples.sketchbook.version1.SketchBSample"/>
                    <arg value="./scenarios/sketchbook-sample"/>
                    <arg value="4256"/>
                </java>
            </sequential>
            <sequential>
                <java fork="yes" classname="org.jdiffchaser.scenarihandling.RemoteControlFrame"
                                 jvm="${env.JAVA_HOME}/bin/java">
                    <classpath refid="run.classpath"/>
                    <jvmarg value="-Djava.library.path=${library.path}"/>
                    <arg value="4256"/>
                </java>
            </sequential>            
        </parallel>
    </target>
    
    <target name="run-localplayer-sketchbook-sample" depends="compile,set-win-args,set-mac-args" description="run Default Scenario Player for sketchbook sample">
       <echo message="library.path is : ${library.path}"/>
       <echo message="Java home is ${env.JAVA_HOME}"/>
       <java fork="yes" classname="org.jdiffchaser.testing.DefaultPlayer">
            <classpath refid="run.classpath"/>
            <jvmarg value="-Djava.library.path=${library.path}"/>
            <jvmarg value="-Xmx128M"/>
            <arg value="org.jdiffchaser.samples.sketchbook.version1.SketchBSample"/>
            <arg value="./screenshots"/>
        </java>
    </target>      
    
    <target name="run-guitests-sketchbook-sample" depends="compile,set-win-args,set-mac-args"  description="run all scenarios for sketchbook sample">
        <echo message="library.path is : ${library.path}"/>
        <parallel>
            <sequential>    
                <java fork="yes" classname="org.jdiffchaser.testing.DefaultRemoteServer">
                    <classpath refid="run.classpath"/>
                    <jvmarg value="-Djava.library.path=${library.path}"/>
                    <arg value="3511"/>
                    <arg value="org.jdiffchaser.samples.sketchbook.version1.SketchBSample"/>
                    <arg value="${basedir}/sketchbook-screenshots1"/>
                </java>
            </sequential>
            <sequential>                
                <java fork="yes" classname="org.jdiffchaser.testing.DefaultRemoteServer">
                    <classpath refid="run.classpath"/>
                    <jvmarg value="-Djava.library.path=${library.path}"/>
                    <arg value="3512"/>
                    <!-- note that the below class is different from the one we launch on 3511    -->
                    <!-- this is for demo purpose: in real life, the classname could be the same  -->
                    <!-- on both DefaultRemoteServer, in that case the version would be different -->
                    <arg value="org.jdiffchaser.samples.sketchbook.version2.SketchBSample"/>
                    <arg value="${basedir}/sketchbook-screenshots2"/>
                </java>
            </sequential>            
            <sequential>                
                <java fork="yes" classname="org.jdiffchaser.testing.Controller">
                    <classpath refid="run.classpath"/>
                    <jvmarg value="-Djava.library.path=${library.path}"/>
                    <arg value="./samplestests/sketchbook-sample/tests-sketchbook-sample-${operating.system}.xml"/>
                </java>   
            </sequential>            
        </parallel>     
    </target> 

    <target name="compile-selected-files">
        <fail unless="files">Must set property 'files'</fail>
        <echo message="Compiling files ${files} ..." />
        <mkdir  dir="${build}" />
        <javac	fork="yes"
        		executable="${env.JAVA_HOME}/bin/javac"
        		memoryInitialSize="128m"
        		memoryMaximumSize="512m"
        		debug="true"
        		deprecation="no"
        		destdir="${build}"
        		srcdir="${sources}"
        		includes="${files}"
        		verbose="no"
                        target="${java.target}"
                        source="${java.source}">
			<classpath refid="compile.classpath"/>
		</javac>
    </target>

    <target name="run-single" depends="compile">
        <fail unless="runclass">Must set property 'runclass'</fail>
        <fail message="JAVA_HOME not set" unless="env.JAVA_HOME"/>
        <echo message="Running ${runclass} ..." />
        <java fork="yes" classname="${runclass}" jvm="${env.JAVA_HOME}/bin/java">
            <classpath refid="run.classpath"/>
        </java>
    </target>  
    
    <target name="test-single" depends="compile_test">
        <fail unless="runclass">Must set property 'runclass'</fail>
        <echo message="Running tests on ${runclass} ..." />
        <junit dir="${basedir}" fork="true" printsummary="no" showoutput="no">
            <classpath refid="test.classpath"/>
            <formatter type="brief" usefile="false"/>
	        <test name="${runclass}"/>
        </junit>
    </target>    
    
    <target name="create-release">
        <property name="version-name" value="${ant.project.name}-${version}"/>
        <property name="release-file" value="${release}/${version-name}.zip"/>
        <delete file="${release-file}" quiet="true"/>
        <mkdir dir="${release}" />   
        <zip destfile="${release-file}" >
             <zipfileset dir="${sources}" prefix="${version-name}/${sources}"/>
             <zipfileset dir="${sources.test}" prefix="${version-name}/${sources.test}"/>
             <zipfileset dir="${lib}" prefix="${version-name}/${lib}"/>
             <zipfileset dir="${testlib}" prefix="${version-name}/${testlib}"/>
             <zipfileset dir="${conf}" prefix="${version-name}/${conf}"/>
             <zipfileset dir="${schema.dir}" prefix="${version-name}/${schema.dir}" includes="*.xsd"/>
             <zipfileset dir="${tools.dir}" prefix="${version-name}/${tools.dir}"/>
             <zipfileset dir="${ontopprojects.dir}" prefix="${version-name}/${ontopprojects.dir}"/>
             <zipfileset dir="${samples.dir}" prefix="${version-name}/${samples.dir}"/>
             <zipfileset dir="${basedir}" prefix="${version-name}">
                <include name="*.xml"/>
                <include name="*.txt"/>
                <include name="*.properties"/>
             </zipfileset>
             <zipfileset dir="${docs.dir}" prefix="${version-name}/${docs.dir}">
                <include name="user-manual/*.pdf"/>
                <include name="images/jDiffChaserLogo.svg"/>
             </zipfileset>
        </zip>
    </target>      
    
</project>

