<?xml version="1.0"?>
<project name="sitemesh" default="jar">

    <property name="build.compiler" value="modern" />
    <property file="build.properties"/>

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

    <target name="jflex" unless="lexer.uptodate" description="Generate Lexer class">
        <echo message="Generating HTML lexer using JFlex"/>
        <mkdir dir="build/java"/>
        <java classpath="lib/jflex.jar" classname="JFlex.Main" fork="yes">
            <arg value="-d"/>
            <arg value="build/java/com/opensymphony/module/sitemesh/html/tokenizer"/>
            <arg value="src/java/com/opensymphony/module/sitemesh/html/tokenizer/lexer.flex"/>
        </java>
        <delete file="build/java/com/opensymphony/module/sitemesh/html/tokenizer/Lexer.java~"/>
    </target>

    <target name="hostedqa" depends="example">
        <taskdef resource="hostedqatasks" classpathref="cp"/>
        <upload file="dist/${name}-example.war" account="sitemesh" email="${hostedqa.email}" password="${hostedqa.password}" resourceId="18"/>
        <playsuite suiteId="16" clientConfigs="17" appConfigs="12" account="sitemesh" email="${hostedqa.email}" password="${hostedqa.password}"/>
    </target>

    <target name="compile" description="Compile Java">
        <!-- Determine if lexer needs regenerating -->
        <uptodate
            property="lexer.uptodate"
            srcfile="src/java/com/opensymphony/module/sitemesh/html/tokenizer/lexer.flex"
            targetfile="build/java/com/opensymphony/module/sitemesh/html/tokenizer/Lexer.java"/>
        <antcall target="jflex"/>

        <mkdir dir="build/classes"/>

        <javac destdir="build/classes"
            classpathref="cp"
            optimize="${compile.optimize}"
            debug="${compile.debug}"
            nowarn="${compile.nowarn}"
            deprecation="${compile.deprecation}"
            source="1.3"
            target="1.3"
            >
            <src path="src/java"/>
            <src path="build/java"/>
        </javac>
    </target>

    <target name="test" depends="compile" description="Run all unit tests">
        <mkdir dir="build/test-classes"/>
        <mkdir dir="build/test-results"/>
        <javac srcdir="src/test" destdir="build/test-classes" classpathref="cp" classpath="build/classes"/>
        <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask" classpathref="cp"/>
        <echo message="Running unit tests..."/>
        <junit printsummary="no" haltonfailure="yes" fork="yes" forkmode="once" dir=".">
            <classpath refid="cp"/>
            <classpath>
                <pathelement location="build/classes"/>
                <pathelement location="build/test-classes"/>
            </classpath>
            <formatter type="brief" usefile="no"/>
            <formatter type="xml"/>
            <batchtest todir="build/test-results">
                <fileset dir="src/test">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <target name="javadocs" description="Generates JavaDoc">
        <mkdir dir="dist/docs/api"/>

        <javadoc sourcepath="src/java"
            destdir="dist/docs/api"
            stylesheetfile="docs/api.css"
            packagenames="com.opensymphony.*"
            classpathref="cp"
            author="true"
            version="true"
            private="true"
            use="true"
            windowTitle="${Name} ${version} API"
            doctitle="${Name} ${version} API"
            footer="&lt;a href=&quot;http://www.opensymphony.com/sitemesh/&quot;&gt;www.opensymphony.com/sitemesh/&lt;/a&gt;">

            <link href="http://java.sun.com/j2se/1.4.2/docs/api/"/>
            <link href="http://java.sun.com/products/servlet/2.3/javadoc/"/>
            <link href="http://jakarta.apache.org/velocity/api/"/>
            <link href="http://jakarta.apache.org/velocity/tools/javadoc/"/>
            <link href="http://freemarker.sourceforge.net/docs/api/"/>
        </javadoc>
    </target>

    <target name="docs" depends="javadocs">
        <copy todir="dist/docs">
            <fileset dir="docs" excludes="*.css"/>
        </copy>

        <mkdir dir="dist/docs/dtd"/>
        <copy todir="dist/docs" file="README.txt"/>
        <copy todir="dist/docs" file="CHANGES.txt"/>
        <copy todir="dist/docs/dtd">
            <fileset dir="src/etc/dtd"/>
        </copy>

        <replace dir="dist/docs" token="@VERSION@" value="${version}"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="dist"/>
        <copy todir="build/classes">
             <fileset dir="src/java" excludes="**/*.java,**/*.flex"/>
        </copy>
        <jar jarfile="dist/${name}-${version}.jar" basedir="build/classes" excludes="**/servlets/**">
            <metainf dir="src/etc/tld/jsp1.2" includes="*.tld"/>
        </jar>
    </target>

    <target name="blank" depends="jar" description="Builds simple web-app to get started with SiteMesh">
        <mkdir dir="dist"/>
        <war warfile="dist/${name}-blank.war" webxml="src/etc/blank/WEB-INF/web.xml">
            <fileset dir="src/etc/blank">
                <exclude name="WEB-INF/web.xml"/>
            </fileset>
            <lib dir="dist">
                <include name="${name}-${version}.jar"/>
            </lib>
            <lib dir="src/etc/tld">
                <include name="sitemesh*.tld" />
            </lib>
        </war>
    </target>

    <target name="example" depends="jar" description="Builds sample web-app">
        <mkdir dir="build/example-temp"/>
        <copy todir="build/example-temp">
            <fileset dir="src/example-webapp"/>
        </copy>

        <mkdir dir="dist"/>

        <war warfile="dist/${name}-example.war" webxml="src/example-webapp/WEB-INF/web.xml">
            <fileset dir="build/example-temp">
                <exclude name="WEB-INF/web.xml"/>
            </fileset>
            <lib dir="dist">
                <include name="${name}-${version}.jar"/>
            </lib>
            <lib dir="lib">
                <include name="commons-*.jar"/>
                <include name="velocity-*.jar"/>
                <include name="freemarker.jar"/>
                </lib>
            <lib dir="src/etc/tld">
                  <include name="sitemesh*.tld" />
            </lib>
        </war>
    </target>

    <target name="dist" depends="clean, test, jar, docs, blank, example" description="Builds distribution archives">
        <zip zipfile="dist/${name}-${version}.zip">
            <zipfileset dir=".">
                <include name="build.xml"/>
                <include name="build.properties"/>
                <include name="LICENSE.txt"/>
                <include name="CHANGES.txt"/>
                <include name="README.txt"/>
                <include name="src/**"/>
                <include name="lib/**"/>
            </zipfileset>
            <zipfileset dir="build">
                <include name="${name}-${version}.jar"/>
                <include name="${name}-example.war"/>
                <include name="${name}-blank.war"/>
            </zipfileset>
            <zipfileset dir="dist/docs" prefix="docs"/>
        </zip>
    </target>

    <target name="clean" description="Removes all build artifacts">
        <delete dir="build"/>
        <delete dir="dist"/>
    </target>

</project>
