<project name="os" default="compile" basedir=".">

    <path id="cp">
        <fileset dir="lib">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <path id="junit.cp">
        <pathelement location="${build.test}"/>
        <pathelement location="${build.java-test}"/>
        <path refid="cp"/>
    </path>

    <property name="compile.version" value="1.5"/>

    <property file="build.properties"/>

    <property name="lib" value="lib"/>
    <property name="lib.core" value="${lib}/core"/>
    <property name="lib.build" value="${lib}/build"/>

    <property name="src" value="src"/>
    <property name="src.java" value="${src}/java"/>
    <property name="test" value="${src}/test"/>
    <property name="src.test" value="${test}/java"/>

    <property name="build" value="build"/>
    <property name="build.test" value="${build}/classes"/>
    <property name="build.java-test" value="${build}/classes"/>
    <property name="build.java" value="${build}/classes"/>
    <property name="build.clover" value="${build}/clover"/>
    <property name="dist" value="dist"/>

    <property name="docs" value="docs"/>

    <available property="junit.available" classname="junit.framework.TestCase"/>
    <available property="clover.available" classname="org.apache.tools.ant.taskdefs.CloverCompilerAdapter"/>

    <target name="init">
        <tstamp/>
    </target>

    <target name="junit-check" depends="init" unless="junit.available">
        <fail message="Cannot run test cases. Please copy lib/build/junit-3.8.1.jar to ${ant.home}/lib"/>
    </target>

    <target name="clover-check" depends="init" unless="clover.available">
        <fail message="Cannot run coverage tests. Please copy lib/build/clover.jar to ${ant.home}/lib"/>
    </target>

    <target name="clean" depends="init">
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
    </target>

    <target name="compile" depends="init">
        <mkdir dir="${build.java}"/>
        <javac srcdir="${src.java}" destdir="${build.java}" classpathref="cp" debug="on" source="${compile.version}"
               target="${compile.version}"/>
        <copy filtering="no" todir="${build.java}">
            <fileset dir="${src.java}">
                <exclude name="**/*.java"/>
                <exclude name="**/package.html"/>
            </fileset>
        </copy>

        <mkdir dir="${build.test}"/>
        <javac srcdir="${src.test}" destdir="${build.test}" classpath="${build.java}" source="${compile.version}"
               target="${compile.version}" classpathref="cp" debug="on"/>
        <copy filtering="no" todir="${build.test}">
            <fileset dir="${src.test}">
                <exclude name="**/*.java"/>
                <exclude name="**/package.html"/>
            </fileset>
        </copy>

        <mkdir dir="${build.java-test}"/>
        <javac srcdir="${src.java}" destdir="${build.java-test}" source="${compile.version}" target="${compile.version}"
               classpathref="cp" debug="on"/>
        <copy filtering="no" todir="${build.java-test}">
            <fileset dir="${src.java}">
                <exclude name="**/*.java"/>
                <exclude name="**/package.html"/>
            </fileset>
        </copy>
    </target>

    <!-- <taskdef classpathref="${ant.home}/lib/clover.jar" name="clover-setup"/> -->

    <target name="test" depends="junit-check,clover-check,compile">
        <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask"/>

        <mkdir dir="${build.clover}"/>

        <clover-setup initString="${build.clover}/coverage.db">
            <files>
                <exclude name="src/test/**/*.java"/>
            </files>
        </clover-setup>

        <mkdir dir="${dist}/docs/junit"/>
        <junit printsummary="no" haltonfailure="no" haltonerror="yes" fork="yes" showoutput="true" forkmode="once">
            <jvmarg value="-Djava.awt.headless=true"/>
            <classpath>
                <path refid="junit.cp"/>
            </classpath>

            <formatter type="brief" useFile="false"/>
            <formatter type="xml"/>

            <batchtest todir="${dist}/docs/junit">
                <fileset dir="${src.test}">
                    <exclude name="**/Abstract*.java"/>
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <target name="perf-test" depends="junit-check,compile">
        <path id="perf.cp">
            <pathelement location="${build.java}"/>
            <fileset dir="${lib.build}">
                <include name="*.jar"/>
            </fileset>
        </path>
        <java classname="org.ognl.test.Performance" classpathref="perf.cp">
            <arg line="-iterations 100000"/>
        </java>
    </target>

    <target name="javadocs">
        <mkdir dir="${dist}/docs/api"/>
        <javadoc sourcepath="${src.java}"
                 destdir="${dist}/docs/api"
                 packagenames="${package}.*"
                 classpathref="cp"
                 author="true"
                 version="true"
                 overview="${src.java}/overview.html"
                 windowTitle="${fullname} API - ${version}"
                 doctitle="${fullname} API (${version})"
                 footer="&lt;a href=&quot;http://www.opensymphony.com/${name}/&quot; target=&quot;_top&quot;&gt;${fullname} Project Page&lt;/a&gt;"
                 use="true"
                 verbose="false">
            <link href="http://java.sun.com/j2se/${compile.version}/docs/api/"/>
            <link href="http://java.sun.com/j2ee/${compile.version}/docs/api/"/>
        </javadoc>
        <!-- <copy overwrite="yes" file="${docs}/main.css" tofile="${docs}/api/stylesheet.css"/> -->
    </target>

    <target name="docs.impl">
        <copy todir="${dist}/docs">
            <fileset dir="${docs}"/>
        </copy>
    </target>

    <target name="docs" depends="javadocs, clover.report, junit.report, docs.impl">
    </target>

    <target name="clover.report" depends="test">
        <clover-report>
            <current outfile="${dist}/docs/clover">
                <format type="html"/>
            </current>
        </clover-report>
    </target>

    <target name="clover.historical" depends="clover.report">
        <clover-historypoint historyDir="${build.clover}"/>

        <clover-report>
            <historical outfile="${dist}/docs/clover" historyDir="${build.clover}">
                <format type="html"/>
            </historical>
        </clover-report>
    </target>

    <target name="junit.report" depends="test">
        <junitreport todir="${dist}/docs/junit">
            <fileset dir="${dist}/docs/junit">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="${dist}/docs/junit"/>
        </junitreport>
    </target>

    <target name="website" depends="javadocs, docs.impl"/>

</project>
