<?xml version="1.0"?>

<!-- build configuration -->
<project name="samskivert" default="compile" basedir=".">

  <!-- things you may want to change -->
  <property name="app.name"         value="samskivert"/>
  <property name="doc.packages"     value="com.samskivert.*"/>
  <property name="doc.overview"     value="com/samskivert/overview.html"/>
  <property name="copyright.holder" value="Michael Bayne"/>

  <!-- we want to access the environment -->
  <property environment="env"/>

  <!-- things you probably don't want to change -->
  <property name="src.dir"     value="src/java"/>
  <property name="deploy.dir"  value="dist"/>
  <property name="dist.jar"    value="${app.name}.jar"/>
  <property name="javadoc.dir" value="${deploy.dir}/docs"/>
  <property name="savedoc.dir" value="docs"/>

  <!-- declare our classpath business -->
  <path id="base.classpath">
    <pathelement location="${deploy.dir}/classes"/>
    <fileset dir="lib" includes="**/*.jar"/>
  </path>
  <path id="classpath">
    <path refid="base.classpath"/>
  </path>

  <!-- include JAVA_LIBS jars in our classpath if it exists -->
  <target name="check-java-libs" if="env.JAVA_LIBS">
    <!-- overwrite the classpath with one that contains JAVA_LIBS jars -->
    <path id="classpath">
      <path refid="base.classpath"/>
      <fileset dir="${env.JAVA_LIBS}" includes="**/*.jar"/>
    </path>
  </target>

  <!-- checks to see which packages are available -->
  <target name="check-available" depends="check-java-libs">
    <echo message="The packages required for building are listed below."/>
    <echo message="A package followed by 'true' indicates that the package"/>
    <echo message="is present. One followed by '${package.present}' indicates"/>
    <echo message="that it was not found. Jar files can be placed into the"/>
    <echo message="lib/ directory or placed in the directory referenced"/>
    <echo message="by your JAVA_LIBS environment variable."/>

    <echo message=""/>
    <echo message="------------------------------------------"/>
    <echo message="Standard extensions - http://java.sun.com/"/>
    <echo message="------------------------------------------"/>
    <available property="servlet2.2.present"
     classname="javax.servlet.Servlet" classpathref="classpath"/>
    <echo message="Servlet 2.2: ${servlet2.2.present}"/>

    <available property="sax.present"
     classname="org.xml.sax.SAXException" classpathref="classpath"/>
    <echo message="SAX: ${sax.present}"/>

    <available property="jaxp.present"
     classname="javax.xml.parsers.SAXParser" classpathref="classpath"/>
    <echo message="JAXP: ${jaxp.present}"/>

    <echo message=""/>
    <echo message="----------------------------------------------"/>
    <echo message="Jakarta libraries - http://jakarta.apache.org/"/>
    <echo message="----------------------------------------------"/>
    <available property="jakarta.commons-collections.present"
     classname="org.apache.commons.collections.FilterIterator"
     classpathref="classpath"/>
    <echo message="Commons Collections: ${jakarta.commons-collections.present}"/>

    <available property="jakarta.commons-digester.present"
     classname="org.apache.commons.digester.Digester"
     classpathref="classpath"/>
    <echo message="Commons Digester: ${jakarta.commons-digester.present}"/>

    <available property="jakarta.velocity.present"
     classname="org.apache.velocity.Template" classpathref="classpath"/>
    <echo message="Velocity: ${jakarta.velocity.present}"/>

    <echo message=""/>
    <echo message="-----------------------------------"/>
    <echo message="WebMacro - http://www.webmacro.org/"/>
    <echo message="-----------------------------------"/>
    <available property="webmacro.present"
     classname="org.webmacro.Template" classpathref="classpath"/>
    <echo message="WebMacro: ${webmacro.present}"/>
  </target>

  <!-- combines package availability into build controls -->
  <target name="compute-builds">
    <echo message="The packages that will be built are listed below. One"/>
    <echo message="followed by 'true' indicates that it will be built. One"/>
    <echo message="followed by '${build.package}' indicates that it will"/>
    <echo message="not be built. If a package is not being built, one or"/>
    <echo message="more of its dependencies could not be located."/>

    <echo message=""/>
    <property name="build.util" value="true"/>
    <!--<echo message="com.samskivert.util: ${build.util}"/>-->

    <property name="build.io" value="true"/>
    <!--<echo message="com.samskivert.io: ${build.io}"/>-->

    <condition property="build.jdbc">
      <and>
        <isset property="build.io"/>
        <isset property="build.util"/>
      </and>
    </condition>
    <!--<echo message="com.samskivert.jdbc: ${build.jdbc}"/>-->

    <condition property="build.net">
      <isset property="build.io"/>
    </condition>
    <!--<echo message="com.samskivert.net: ${build.net}"/>-->

    <condition property="build.servlet">
      <and>
        <isset property="build.io"/>
        <isset property="build.jdbc"/>
        <isset property="build.util"/>
        <isset property="servlet2.2.present"/>
      </and>
    </condition>
    <echo message="com.samskivert.servlet: ${build.servlet}"/>

    <condition property="build.swing">
      <isset property="build.util"/>
    </condition>
    <echo message="com.samskivert.swing: ${build.swing}"/>

    <property name="build.test" value="true"/>
    <echo message="com.samskivert.test: ${build.test}"/>

    <condition property="build.velocity">
      <and>
        <isset property="build.servlet"/>
        <isset property="build.util"/>
        <isset property="jakarta.commons-collections.present"/>
        <isset property="jakarta.velocity.present"/>
        <isset property="servlet2.2.present"/>
      </and>
    </condition>
    <echo message="com.samskivert.velocity: ${build.velocity}"/>

    <condition property="build.webmacro">
      <and>
        <isset property="build.servlet"/>
        <isset property="build.util"/>
        <isset property="servlet2.2.present"/>
        <isset property="webmacro.present"/>
      </and>
    </condition>
    <echo message="com.samskivert.webmacro: ${build.webmacro}"/>

    <condition property="build.xml">
      <and>
        <isset property="build.io"/>
        <isset property="build.util"/>
        <isset property="jakarta.commons-digester.present"/>
        <isset property="jaxp.present"/>
        <isset property="sax.present"/>
      </and>
    </condition>
    <echo message="com.samskivert.xml: ${build.xml}"/>
  </target>

  <!-- prepares the application directories -->
  <target name="prepare">
    <tstamp><format property="year" pattern="yyyy" /></tstamp>
    <mkdir dir="${deploy.dir}"/>
    <mkdir dir="${deploy.dir}/classes"/>
    <mkdir dir="${javadoc.dir}"/>
    <copy todir="${deploy.dir}/classes">
      <fileset dir="${src.dir}" includes="**/*.properties"/>
    </copy>
  </target>

  <!-- cleans out the installed application -->
  <target name="clean">
    <delete dir="${deploy.dir}"/>
  </target>

  <!-- build the java class files -->
  <target name="compile" depends="prepare,check-available,compute-builds">
    <javac srcdir="${src.dir}" destdir="${deploy.dir}/classes"
           debug="on" optimize="${build.optimize}" deprecation="off">
      <classpath refid="classpath"/>
      <exclude name="com/samskivert/io/**" unless="build.io"/>
      <exclude name="com/samskivert/jdbc/**" unless="build.jdbc"/>
      <exclude name="com/samskivert/net/**" unless="build.net"/>
      <exclude name="com/samskivert/servlet/**" unless="build.servlet"/>
      <exclude name="com/samskivert/swing/**" unless="build.swing"/>
      <exclude name="com/samskivert/test/**" unless="build.test"/>
      <exclude name="com/samskivert/util/**" unless="build.util"/>
      <exclude name="com/samskivert/velocity/**" unless="build.velocity"/>
      <exclude name="com/samskivert/webmacro/**" unless="build.webmacro"/>
      <exclude name="com/samskivert/xml/**" unless="build.xml"/>
    </javac>
  </target>

  <!-- build the javadoc documentation -->
  <target name="javadoc" depends="prepare">
    <javadoc sourcepath="${src.dir}"
             packagenames="${doc.packages}"
             windowtitle="${app.name} API"
             doctitle="${app.name} API"
             overview="${src.dir}/${doc.overview}"
             destdir="${javadoc.dir}"
             additionalparam="-breakiterator">
      <bottom>Copyright &#169; 2000-${year} ${copyright.holder}.
              All Rights Reserved.</bottom>
      <classpath refid="classpath"/>
      <link href="http://java.sun.com/j2se/1.4/docs/api/"/>
    </javadoc>
  </target>

  <!-- builds the javadocs and stuffs them in a directory where they won't
       be blown away when we do "clean" next time -->
  <target name="savedoc" depends="javadoc">
    <delete dir="${savedoc.dir}/api"/>
    <copy todir="${savedoc.dir}/api">
      <fileset dir="${javadoc.dir}" includes="**/*"/>
    </copy>
  </target>

  <!-- a target for rebuilding everything -->
  <target name="all" depends="clean,prepare,compile,javadoc,dist"/>

  <!-- builds our distribution files (war and jar) -->
  <target name="dist" depends="prepare,compile">
    <jar destfile="${deploy.dir}/${dist.jar}"
         basedir="${deploy.dir}/classes"/>
  </target>

  <!-- generate a class hierarchy diagram -->
  <target name="hiergen" depends="prepare,compile">
    <taskdef name="viztool" classname="com.samskivert.viztool.DriverTask"/>
    <viztool visualizer="com.samskivert.viztool.hierarchy.HierarchyVisualizer"
      pkgroot="com.samskivert"
      classes="com.samskivert.*"
      output="hierarchy.ps">
      <classpath refid="classpath"/>
    </viztool>
  </target>

</project>
