<project name="trick" default="dist" basedir=".">
    <description>
        Build file for Trick JAVA resources
    </description>
    <!-- set global properties for this build -->
    <property environment="env"/>
    <property name="lib"                location="${env.TRICK_HOME}/bin/java/lib" />
    <property name="src"                location="src"/>
    <property name="build"              location="build"/>
    <property name="build.classes"      location="build/classes" />
    <property name="dist"               location="${env.TRICK_HOME}/bin/java/dist"/>
    <property name="docs"               location="docs" />
    <property name="apidocs"            location="docs/api" />
    <property name="test"               location="test" />
    <property name="test.src"           location="test/src" />
    <property name="build.test"         location="build/test" />
    <property name="test.reports"       location="test/reports" />
    <property name="resources"          location="${env.TRICK_HOME}/bin/java/resources" />

    <!--############################################
    ## CLEAN
    ## Remove items from previous build
    #############################################-->
    <target name="clean" description="clean up" >
        <!-- Delete the ${build} and ${dist} directory trees -->
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
        <delete dir="${test.reports}"/>
        <delete dir="${resources}"/>
    </target>

    <!--############################################
    ## CHECK JAVA VERSION
    ## Test to see if we have the right version of JVM.
    #############################################-->
	<property name="java_minimum_version" value="1.6" />
	<target name="get-java">
	    <condition property="java.supported">
	        <or>
                    <contains string="${java.version}" substring="${java_minimum_version}" />
                    <contains string="${java.version}" substring="1.7" />
                    <contains string="${java.version}" substring="1.8" />
                </or>
	    </condition>
	</target>

    <target name="check-java" depends="get-java" unless="java.supported">
        <fail message="Your version of Java (${java.version}) is not supported. Trick requires a minimum of Java ${java_minimum_version}."/>
    </target>

    <!--############################################
    ## INIT
    ## Initialization of variables for this build
    #############################################-->
    <target name="init" depends="check-java">
        <!-- Create the time stamp -->
        <tstamp/>
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build.classes}"/>
    </target>

    <!--############################################
    ## COMPILE
    ## Compile src/java to ${build}
    #############################################-->
    <target name="compile" depends="init" description="compile the source" >
    	
        <!-- Compile the java code from ${src} into ${build} -->
        <javac srcdir="${src}" destdir="${build.classes}" debug="true" debuglevel="lines,vars,source">
            <compilerarg value="-Xlint:unchecked"/>
            <compilerarg value="-Xlint:deprecation"/>
            <compilerarg value="-g"/>
        </javac>
    </target>

    <!--############################################
    ## DIST
    ## Create distribution directory and build
    ## trick jar
    #############################################-->
    <target name="dist" depends="compile,resources" description="generate the distribution" >
        <!-- Create the distribution directory -->
        <mkdir dir="${dist}"/>
        <jar jarfile="${dist}/trick.jar">
            <fileset dir="${build.classes}" />
            <fileset dir="${src}" excludes="**/*.java"/>
        </jar>
    </target>

    <target name="resources" description="copy resources to destination file" >
        <mkdir dir="${resources}"/>
        <copy file="src/trick/common/resources/trick_icon.png" todir="${resources}"/>
    </target>

    <!--############################################
    ## TEST
    ## Compile & Run JUnit tests
    ## Generate test reports in test/reports
    #############################################-->
    <target name="pre-compile-test" depends="dist" >
        <mkdir dir="${build.test}" />
        <mkdir dir="${test.reports}" />
    </target>

    <target name="test" depends="pre-compile-test" description="compile and run the JUnit tests" >
        <javac srcdir="${test.src}" destdir="${build.test}" classpath="${build.classes}" debug="true" debuglevel="lines,vars,source"/>

        <junit printsummary="yes" failureproperty="junit.failure" showoutput="yes" >
            <classpath>
                <pathelement location="${build.test}" />
            </classpath>
                <classpath location="${dist}/trick.jar" />
            <batchtest todir="${test.reports}" >
                <fileset dir="${test.src}" >
                    <include name="**/*Test.java" />
                </fileset>
                <formatter type="xml" />
            </batchtest>
        </junit>

        <junitreport todir="${test.reports}">
            <fileset dir="${test.reports}" includes="*.xml" />
            <report format="frames" todir="${test.reports}/html" />
        </junitreport>
        <fail if="junit.failure" message="Unit test(s) failed.  See reports!"/>
    </target>

    <!--############################################
    ## JAVADOC targets
    ## Targets for building javadocs, running Sun's
    ## doccheck tool, and cleanup of docs
    #############################################-->
    <target name="javadoc">
        <mkdir dir="${apidocs}"/>
        <javadoc packagenames="trick.*" sourcepath="${src}" Windowtitle="Trick GUI API"
                destdir="${apidocs}">
            <link href="http://docs.oracle.com/javase/6/docs/api/"/>
            <link href="http://www.jdocs.com/swingx/1.0/overview-summary.html"/>
            <link href="http://www.jfree.org/jfreechart/api/javadoc/"/>
            <link href="http://junit.sourceforge.net/javadoc/"/>
            <classpath>
                <fileset dir="${lib}">
                    <include name="**/*.jar"/>  <!-- build against all libraries in ${lib} directory structure -->
                </fileset>
            </classpath>
        </javadoc>
    </target>

    <target name="doccheck">
        <mkdir dir="${apidocs}/doccheck"/>
        <javadoc packagenames="trick.*" sourcepath="${src}" destdir="${apidocs}/doccheck">
            <doclet name="com.sun.tools.doclets.doccheck.DocCheck" path="${lib}/doccheck.jar"/>
            <classpath>
                <fileset dir="${lib}">
                    <include name="**/*.jar"/>  <!-- build against all libraries in ${lib} directory structure -->
                </fileset>
            </classpath>
        </javadoc>
    </target>

    <target name="clean_docs">
        <delete dir="${apidocs}" />
    </target>

</project>
