<!-- $Id$ -->
<project name="jakarta-commons-tbm" default="build" basedir=".">

  <!-- patternset describing files to be copied from the doc directory -->
  <patternset id="patternset-doc"/>

  <!-- patternset describing test classes -->
  <patternset id="patternset-test-classes">
    <include name="**/Test*.class"/>
  </patternset>

  <!-- patternset describing bench classes -->
  <patternset id="patternset-bench-classes">          
    <include name="**/bench/**/*.class"/>
  </patternset>  

  <!-- patternset describing non test classes -->
  <patternset id="patternset-non-test-classes">
    <include name="**/*.class"/>
    <exclude name="**/Test*.class"/>
    <exclude name="**/bench/**"/>
  </patternset>

  <!-- patternset describing non test source files (*.java, *html, etc.) -->
  <patternset id="patternset-javadocable-sources">
    <include name="**/*"/>
    <exclude name="**/Test*.java"/>
  </patternset>

   <!-- Anakia props -->
   <property name="docs.src" value="./xdocs"/>

   <!-- Benchmark props -->
   <property name="dest.bench" value="./bench"/>
   <property name="bench.file" value="bench"/>

  <!-- ######################################################### -->

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

    <!-- read properties from the build.properties, if any -->
    <property name="component-propfile" value="${basedir}/build.properties"/>
    <property file="${component-propfile}"/>

    <!-- read properties from the commons build.properties, if any -->
    <property name="commons-propfile" value="${basedir}/../build.properties"/>
    <property file="${commons-propfile}"/>

    <!-- read properties from the ${user.home}/propfile, if any -->
    <property name="user-propfile" value="${user.home}/build.properties"/>
    <property file="${user-propfile}"/>

    <!-- command line classpath, if any -->
    <property name="cp" value=""/>

    <!-- now combine the classpaths -->
    <property name="classpath" value="${cp}:${junit.jar}:${commons-lang.jar}"/>

    <property name="name" value="commons-tbm"/>
    <property name="Name" value="Commons-TBM"/>
    <property name="Name-Long" value="Apache Commons Thread Benchmark"/>

    <!-- The current version number of this component -->
    <property name="component.package" value="org.apache.commons.tbm"/>
    <property name="component.version" value="1.0-dev"/>
    <filter  token="package"           value="${component.package}"/>
    <filter  token="version"           value="${component.version}"/>

<!--
    <property name="test.entry" value="org.apache.commons.tbm.TestAll"/>
    <property name="test.failonerror" value="true" />
    <property name="test.runner" value="junit.textui.TestRunner" />
-->
    <property name="workdir" value="${java.io.tmpdir}/buildtemp_${DSTAMP}${TSTAMP}"/>
    <property name="source"          value="${basedir}"/>
    <property name="source.src"      value="${basedir}/src"/>
    <property name="source.src.conf" value="${source.src}/conf"/>
    <property name="source.src.java" value="${source.src}/java"/>
    <property name="source.src.test" value="${source.src}/test"/>
    <property name="source.src.bench" value="${source.src}/bench"/>
    <property name="source.doc"      value="${basedir}/doc"/>
    <property name="dest"         value="${basedir}/dist"/>
    <property name="dest.src"     value="${dest}/src"/>
    <property name="dest.classes" value="${dest}/classes"/>
    <property name="dest.conf"    value="${dest}/conf"/>
    <property name="dest.doc"     value="${dest}/docs"/>
    <property name="dest.doc.api" value="${dest.doc}/api"/>
    <property name="dest.jardir"  value="${dest}"/>
    <property name="dest.jardir.jar" value="${dest.jardir}/${name}.jar"/>
    <property name="dest.jardir-bench.jar"
              value="${dest.jardir}/${name}-bench.jar"/>

    <!-- does this module have docs? -->
    <available property="available-doc" file="${source.doc}"/>
    <!-- does this module have conf src? -->
    <available property="available-src-conf" file="${source.src.conf}"/>
    <!-- does this module have java src? -->
    <available property="available-src-java" file="${source.src.java}"/>
    <!-- does this module have test src? -->  
    <available property="available-src-test" file="${source.src.test}"/>
    <!-- does this module have bench src? -->  
    <available property="available-src-bench" file="${source.src.bench}"/>

  </target>

  <!-- ######################################################### -->

  <target name="copy-javadoc-source" depends="init" if="available-src-java">
    <mkdir dir="${javadoc-source-dir}"/>
    <copy todir="${javadoc-source-dir}" filtering="no">
      <fileset dir="${source.src.java}">
        <patternset refid="patternset-javadocable-sources"/>
      </fileset>
    </copy>
  </target>

  <target name="copy-doc" depends="init" if="available-doc">
    <mkdir dir="${doc-source-dir}/${name}"/>
    <copy todir="${doc-source-dir}/${name}" filtering="no">
      <fileset dir="${source.doc}">
        <patternset refid="patternset-doc"/>
      </fileset>
    </copy>
  </target>

  <!-- ######################################################### -->

  <target name="clean" depends="init" description="removes generated files">
    <delete dir="${dest}"/>
    <delete>
      <fileset dir="." includes="velocity.log*"/>
    </delete>
  </target>

  <target name="clean-doc" depends="init,clean-javadoc">
    <delete dir="${dest.doc}"/>
  </target>

  <target name="clean-javadoc" depends="init">
    <delete dir="${dest.doc.api}"/>
  </target>

  <target name="clean-build" depends="init">
    <delete dir="${dest.classes}"/>
  </target>

  <target name="clean-dist" depends="init">
    <delete file="${dest.jardir.jar}"/>
  </target>

  <!-- ######################################################### -->

  <target name="doc" depends="build,doc-top,doc-copy,doc-javadoc,xdoc"
          description="generates javadocs and other documentation">
  </target>

  <target name="doc-top" depends="init">
    <mkdir  dir="${dest}"/>
    <copy todir="${dest}" file="../LICENSE"/>
  </target>

  <target name="doc-copy" depends="init" if="available-doc">
    <mkdir dir="${dest.doc}"/>
    <copy todir="${dest.doc}">
      <fileset dir="${source.doc}">
        <patternset refid="patternset-doc"/>
      </fileset>
    </copy>
  </target>

  <target name="doc-javadoc" depends="init" if="available-src-java">
    <!-- copy all the non-test sources out to the work directory
         and javadoc that -->
    <mkdir dir="${workdir}"/>
    <copy todir="${workdir}">
      <fileset dir="${source.src.java}">
        <patternset refid="patternset-javadocable-sources"/>
      </fileset>
    </copy>
    <copy todir="${workdir}">   
      <fileset dir="${source.src.bench}">
        <patternset refid="patternset-javadocable-sources"/>
      </fileset>
    </copy>
    <mkdir dir="${dest.doc.api}"/>
    <javadoc packagenames="org.*"
             sourcepath="${workdir}"
             classpath="${classpath}"
             destdir="${dest.doc.api}"
             windowtitle="${Name-Long}"
             doctitle="${Name-Long}"
             bottom="&lt;small&gt;Copyright &amp;copy; 2003 Apache Software Foundation. Documenation generated ${TODAY}&lt;/small&gt;."
             protected="true"
             version="true"
             author="true"
             overview="${source.src.java}/org/apache/commons/tbm/package.html"
             splitindex="false"
             nodeprecated="true"
             nodeprecatedlist="true"
             notree="true"
             noindex="false"
             nohelp="true"
             nonavbar="false"
             serialwarn="false">
    </javadoc>
    <delete dir="${workdir}"/>
  </target>

  <!-- ######################################################### -->

  <target name="build" depends="init,build-java" description="compiles source files"/>

  <target name="copy-src" depends="init">
    <mkdir dir="${dest.src}"/>
    <!-- the source code directory -->
    <copy todir="${dest.src}" filtering="yes">
      <fileset dir="${source.src.java}" defaultexcludes="no">
        <include name="**/*.java"/>
        <include name="**/package.html"/>
      </fileset>
    </copy>
    <copy todir="${dest.src}" filtering="yes">
      <fileset dir="${source.src.bench}" defaultexcludes="no">
        <include name="**/*.java"/>
        <include name="**/package.html"/>
      </fileset>
    </copy>

  </target>

  <target name="build-java" depends="copy-src" if="available-src-java">
    <mkdir dir="${dest.classes}"/>
    <javac destdir="${dest.classes}"
           srcdir="${dest.src}"
           classpath="${classpath}"
           debug="true"
           deprecation="true"
           optimize="true">
    </javac>
  </target>

  <target name="build-test" depends="init,build-java" if="available-src-test">
    <mkdir dir="${dest.src}"/>
    <!-- the source code directory -->
    <copy todir="${dest.src}" filtering="yes">
      <fileset dir="${source.src.test}" defaultexcludes="no">
        <include name="**/*.java"/>
      </fileset>
    </copy>

    <mkdir dir="${dest.classes}"/>
    <javac destdir="${dest.classes}"
           srcdir="${dest.src}"
           classpath="${classpath}"
           debug="true"
           deprecation="true"
           optimize="true"/>
    <copy todir="${dest.classes}" filtering="no">
      <fileset dir="${source.src.test}" defaultexcludes="no">
        <include name="testpool.jocl"/>
      </fileset>
    </copy>
  </target>

  <!-- ######################################################### -->

  <target name="test" depends="build-test" if="test.entry"
          description="runs (junit) unit tests">
    <!--
    <junit printsummary="yes" fork="on" haltonfailure="yes">
      <formatter type="plain" usefile="false"/>
      <test name="${test.entry}"/>
      <classpath>
        <pathelement location="${dest.classes}" />
        <pathelement path="${classpath}" />
        <pathelement path="${java.class.path}" />
      </classpath>
    </junit>
    -->

    <java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
      <arg value="${test.entry}"/>
      <sysproperty key="org.xml.sax.driver" value="org.apache.xerces.parsers.SAXParser" />
      <classpath>
        <pathelement location="${dest.classes}" />
        <pathelement path="${classpath}" />
        <pathelement path="${java.class.path}" />
      </classpath>
    </java>
  </target>

  <!-- ######################################################### -->

  <target name="dist" depends="dist-jar,bench-jar,doc"
          description="builds binary distribution"/>

  <target name="dist-jar" depends="build">
    <mkdir dir="${dest.jardir}"/>
    <mkdir dir="${workdir}"/>
    <copy todir="${workdir}">
      <fileset dir="${dest.classes}">
        <patternset refid="patternset-non-test-classes"/>
      </fileset>
    </copy>
    <jar jarfile="${dest.jardir.jar}">
      <fileset dir="${workdir}"/>
      <manifest>   
        <attribute name="Built-By" value="${user.name}"/>    
        <attribute name="Package" value="${package}"/>    
        <attribute name="Extension-Name" value="${name}"/>    
        <attribute name="Specification-Vendor" value="Apache Software Foundation"/>    
        <attribute name="Specification-Title" value="${title}"/>    
        <attribute name="Implementation-Version" value="${version}"/>    
        <attribute name="Implementation-Vendor" value="Apache Software Foundation"/>    
      </manifest>   
    </jar>
    <delete dir="${workdir}"/>
  </target>

  <target name="bench-jar" depends="build">
    <mkdir dir="${dest.jardir}"/>
    <mkdir dir="${workdir}"/>
    <copy todir="${workdir}">
      <fileset dir="${dest.classes}">
        <patternset refid="patternset-bench-classes"/>
      </fileset>
    </copy>
    <jar jarfile="${dest.jardir-bench.jar}">
      <fileset dir="${workdir}"/>
      <manifest>
        <attribute name="Built-By" value="${user.name}"/>
        <attribute name="Package" value="${package}"/>
        <attribute name="Extension-Name" value="${name}"/>     
        <attribute name="Specification-Vendor" value="Apache Software Foundation"/>
        <attribute name="Specification-Title" value="${title}"/>
        <attribute name="Implementation-Version" value="${version}"/>
        <attribute name="Implementation-Vendor" value="Apache Software Foundation"/>
      </manifest>
    </jar>
    <delete dir="${workdir}"/>
  </target>

  <!-- ######################################################### -->

  <target name="xdoc.fetch-stylesheet" unless="localstylesheet">
      <echo>   
       ####################################################################
       #
       #  Fetching the latest stylesheet from jakarta-site2
       #
       #  NOTE : As this build target is meant for developers, this requires
       #    a properly setup CVS.  But you are encouraged to use this to
       #    experiment with Anakia - if the fetch fails, it may be because
       #    you haven't yet logged into CVS. The way to do it, assuming you    
       #    have a resonable CVS client setup is
       #
       #  $ cvs -d :pserver:anoncvs@cvs.apache.org:/home/cvspublic login
       #  password: anoncvs
       #
       #  and that should solve it.
       #
       #  See http://jakarta.apache.org/site/cvsindex.html for more
       #      information, or http://www.cvshome.org/
       #
       #  Ant really is the bee's knees. http://jakarta.apache.org/ant/
       #
       ######################################################################
       </echo>
      <cvspass cvsroot=":pserver:anoncvs@cvs.apache.org:/home/cvspublic"
          password="anoncvs"
      />
      <cvs cvsRoot=":pserver:anoncvs@cvs.apache.org:/home/cvspublic"
          command="checkout -p jakarta-site2/xdocs/stylesheets/site.vsl"
          output="${docs.src}/stylesheets/site.vsl"
      />
  </target>

  <target name="xdoc" depends="init,xdoc.fetch-stylesheet"
          description="Generates HTML documentation from XML source">
      <mkdir      dir="${dest.doc}"/>
      <taskdef name="anakia" classname="org.apache.velocity.anakia.AnakiaTask">
          <classpath location="${velocity.jar}"/>
          <classpath location="${jdom.jar}"/> 
          <classpath location="${log4j.jar}"/>
          <classpath location="${xerces.jar}"/>
          <classpath location="${commons-collections.jar}"/>
      </taskdef>

      <echo>
       #######################################################       
       #
       #  Now using Anakia to transform our XML documentation
       #  to HTML.
       #
       #######################################################
       </echo>

      <anakia basedir="${docs.src}" destdir="${dest.doc}/"
           extension=".html" style="./site.vsl"
           projectFile="stylesheets/project.xml"
           excludes="**/stylesheets/** empty.xml"
           includes="**/*.xml"
           lastModifiedCheck="true"
           templatePath="xdocs/stylesheets">
      </anakia>

      <!--
      <copy todir="${dest.doc}/images" filtering="no">
          <fileset dir="${docs.src}/images">
              <include name="**/*.gif"/>
              <include name="**/*.jpeg"/>
              <include name="**/*.jpg"/>
          </fileset>
      </copy>
      -->
  </target>

  <target name="bench" depends="init" description="Perform benchmarks">
    <taskdef name="bench" classname="org.apache.commons.tbm.bench.BenchTask">
      <classpath location="${dest.jardir-bench.jar}"/>
      <classpath location="${dest.jardir.jar}"/>
      <classpath location="${commons-lang.jar}"/>
    </taskdef>
    <mkdir dir="${dest.bench}"/>
    <bench file="${dest.bench}/${bench.file}.xml" system="${bench.system}" options="${bench.options}">
      <test classname="org.apache.commons.tbm.bench.BenchNOP"/>
      <test classname="org.apache.commons.tbm.bench.datetime.DefaultTimeZone"/>
      <test classname="org.apache.commons.tbm.bench.datetime.SimpleDateFormat"/>
      <test classname="org.apache.commons.tbm.bench.datetime.CommonsLangFastDateFormat"/>
      <test classname="org.apache.commons.tbm.bench.datetime.ThreadLocalSimpleDateFormat"/>
      <test classname="org.apache.commons.tbm.bench.datetime.TimestampToString"/>
    </bench>
    <style in="${dest.bench}/${bench.file}.xml"
           destdir="${dest.bench}"
           out="${dest.bench}/${bench.file}.html"
           style="src/xsl/bench.xsl"/>
  </target>
</project>
