<?xml version="1.0" encoding="UTF-8"?>
<project name="Turtle shell" default="build" basedir=".">
    <property file="ants.properties"/>

    <property name="javadoc.home" value="docs" />
    <property name="target.home" value="bin" />
    <property name="build.home" value="build" />
    <property name="source.home" value="src" />
    <property name="custom.docs" value="src/manpages" />
    <property name="lib.home" value="lib" />

    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <pathelement location="${lib.home}/ant-contrib.jar" />
        </classpath>
    </taskdef>

    <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
      <classpath>
        <pathelement location="${lib.home}/junit.jar"/>
        <pathelement location="${lib.home}/ant-junit.jar"/>
      </classpath>
    </taskdef>

    <path id="classpath">
        <fileset dir="${target.home}">
            <include name="*.jar" />
        </fileset>
        <pathelement path="${build.home}" />
    </path>

    <target name="-init">
        <mkdir dir="${target.home}" />
        <mkdir dir="${build.home}" />
        <available property="lib.ant.present" file="${lib.ant}" />
    </target>

    <target name="-docsinit">
    	<mkdirs dir="${javadoc.home}" />
    </target>
    
    <target name="help">
        <echo><![CDATA[
${app.name} core build file:
build - compiles core
clean - cleans the current build
]]></echo>
    </target>
    
    <target name="-gencode">
        <!-- compile Turtle shell -->
        <delete file="${build.home}" quiet="true" />
        <mkdir dir="${build.home}" />

        <!-- do VERSION variable substitutions -->
        <copy todir="${build.home}">
            <fileset dir="${source.home}" />
            <filterchain>
                <replacetokens>
                    <token key="VERSION"
                           value="${app.version}"/>
                </replacetokens>
            </filterchain>
        </copy>

        <!-- compile all javacc grammars -->
        <for param="file">
            <path>
                <fileset dir="${build.home}" includes="**/*.jj" />
            </path>
            <sequential>
                <!-- the real work goes here -->
                <javacc
                    target="@{file}"
                    javacchome="${lib.home}" />
            </sequential>
        </for>
    </target>

    <target name="build" depends="-init,-gencode" description="Compiles plugins">
        <!-- compile Turtle shell -->
        <javac destdir="${build.home}"
            srcdir="${build.home}"
            classpathref="classpath"
            target="1.5"
            debug="true"
            optimize="true"
            deprecation="true"
            includeAntRuntime="false" />
        <jar destfile="${target.home}/turtle.jar">
            <manifest>
                <attribute name="Main-Class" value="org.turtleshell.Turtle" />
                <attribute name="Class-Path" value="turtle.jar" />
                <attribute name="Specification-Title" value="Turtleshell" />
                <attribute name="Specification-Version" value="${app.version}" />
                <attribute name="Specification-Vendor" value="http://home.cc.umanitoba.ca/~alvare/" />
                <attribute name="Implementation-Title" value="Turtleshell" />
                <attribute name="Implementation-Version" value="${app.version}" />
                <attribute name="Implementation-Vendor" value="http://home.cc.umanitoba.ca/~alvare/" />
            </manifest>
            <fileset dir="${build.home}" excludes="**/*.jj,**/*.jar,**/*.java,**/*.jpage,**/.*" />
        </jar>
    </target>

    <target name="clean">
        <tstamp>
                <format property="dt-stamp" pattern="yyyy-MM-dd-HH-mm" />
                <format property="d-stamp" pattern="yyyy-MM-dd" />
        </tstamp>
        <delete dir="${build.home}" quiet="true" />
    </target>

    <target name="docs" depends="-docsinit" description="Generates Turtle shell API Javadoc">
    	<delete dir="${javadoc.home}" quiet="true" />
    	<mkdir dir="${javadoc.home}" />
        <javadoc
                destdir="${javadoc.home}"
                access="${javadoc.access}"
                use="${javadoc.use}"
                notree="${javadoc.notree}"
                nonavbar="${javadoc.nonavbar}"
                noindex="${javadoc.noindex}"
                splitindex="${javadoc.splitindex}"
                author="${javadoc.author}"
                version="${javadoc.version}"
                nodeprecatedlist="${javadoc.nodeprecatedlist}"
                nodeprecated="${javadoc.nodeprecated}"
                classpathref="classpath"
                header="Turtle shell">
            <doctitle><![CDATA[Turtle shell]]></doctitle>
            <bottom><![CDATA[<i>Copyright &#169; 2011-${app.year} University of Manitoba.</i>]]></bottom>
            <packageset dir="${source.home}" />
            <classpath path="${java.class.path}" /><!-- to make ant.jar available for javadoc -->
            <group title="Turtle shell" />
    	</javadoc>
        <copy todir="${javadoc.home}">
            <fileset dir="${custom.docs}" />
            <filterchain>
                <replacetokens>
                    <token key="VERSION"
                           value="${app.version}"/>
                </replacetokens>
            </filterchain>
        </copy>
    </target>
</project>
