<?xml version="1.0"?>

<!-- 
  =======================================================================
    Ant own build file                                                      
  ======================================================================= 
-->
<project name="Ant" default="main" basedir=".">

  <!-- Give user a chance to override without editing this file 
       (and without typing -D each time it compiles it) -->
  <property file=".ant.properties" />
  <property file="${user.home}/.ant.properties" />

  <property name="Name" value="Ant"/>
  <property name="name" value="ant"/>
  <property name="version" value="1.4alpha"/>

  <property name="debug" value="false" />
  <property name="deprecation" value="false" />
  <property name="optimize" value="true" />
  <property name="junit.fork" value="false" />

  <!-- 
       ===================================================================
         Set the properties related to the source tree
       =================================================================== 
  -->
  <property name="src.dir" value="src"/>
  <property name="java.dir" value="${src.dir}/main"/>
  <property name="script.dir" value="${src.dir}/script"/>
  <property name="lib.dir" value="lib"/>
  <property name="docs.dir" value="docs"/>
  <property name="tests.dir" value="src/testcases"/>
  <property name="tests.etc.dir" value="src/etc/testcases"/>
  
  <property name="ant.package" value="org/apache/tools/ant"/>
  <property name="optional.package" value="${ant.package}/taskdefs/optional"/>

  <property name="manifest" value="src/etc/manifest"/>


  <!-- 
       ===================================================================
         Set the properties for the build area
       =================================================================== 
  -->
  <property name="build.dir" value="build"/>
  <property name="bootstrap.dir" value="bootstrap"/>
  <property name="build.classes" value="${build.dir}/classes"/>
  <property name="build.lib" value="${build.dir}/lib"/>
  <property name="build.javadocs" value="${build.dir}/javadocs"/>
  <property name="build.tests" value="${build.dir}/testcases"/>

  <path id="classpath">
  </path>

  <path id="tests-classpath">
    <pathelement location="${build.classes}" />
    <pathelement location="${build.tests}" />
    <path refid="classpath" />
  </path>

 <!-- 
       ===================================================================
         Set up properties for the distribution area
       =================================================================== 
  -->
  <property name="dist.name" value="jakarta-${name}-${version}"/>
  <property name="dist.base" value="distribution"/>
  <property name="defaultdist.dir" value="dist"/>

  <target name="setup-distproperties">

    <property name="dist.dir" value="${defaultdist.dir}"/>
    <property name="dist.bin" value="${dist.dir}/bin"/>
    <property name="dist.lib" value="${dist.dir}/lib"/>
    <property name="dist.docs" value="${dist.dir}/docs"/>
    <property name="dist.javadocs" value="${dist.dir}/docs/api"/>

    <property name="src.dist.dir" value="dist-src"/>
    <property name="src.dist.src" value="${src.dist.dir}/src"/>
    <property name="src.dist.docs" value="${src.dist.dir}/docs"/>
    <property name="src.dist.lib" value="${src.dist.dir}/lib"/>
  </target>

  <!-- 
       ===================================================================
         Check to see what optional dependencies are available              
       =================================================================== 
  -->
  <target name="check_for_optional_packages">
    <available property="jdk1.2+" classname="java.lang.ThreadLocal" />
    <available property="jdk1.3+" classname="java.lang.StrictMath" />
    <available property="bsf.present" 
               classname="com.ibm.bsf.BSFManager" 
               classpathref="classpath" />
    <available property="netrexx.present" 
               classname="netrexx.lang.Rexx"
               classpathref="classpath" />
    <available property="xslp.present" 
               classname="com.kvisco.xsl.XSLProcessor" 
               classpathref="classpath" />
    <available property="xalan.present" 
               classname="org.apache.xalan.xslt.XSLTProcessorFactory" 
               classpathref="classpath" />
    <available property="ejb.ejbc.present" 
               classname="weblogic.ejbc" 
               classpathref="classpath" />
    <available property="ejb.DDCreator.present" 
               classname="weblogic.ejb.utils.DDCreator" 
               classpathref="classpath" />
    <available property="ejb.wls.present" 
               classname="weblogic.Server" 
               classpathref="classpath" />
    <available property="junit.present" 
               classname="junit.framework.TestCase" 
               classpathref="classpath" />
    <available property="netcomp.present" 
               classname="com.oroinc.net.ftp.FTPClient" 
               classpathref="classpath" />
    <available property="starteam.present" 
               classname="com.starbase.util.Platform" 
               classpathref="classpath" />
    <available property="antlr.present" 
               classname="antlr.Tool" 
               classpathref="classpath"/>
    <available property="vaj.present" 
               classname="com.ibm.ivj.util.base.Workspace"  
               classpathref="classpath"/>
    <available property="stylebook.present" 
               classname="org.apache.stylebook.Engine"  
               classpathref="classpath"/>
    <available property="jakarta.regexp.present" 
               classname="org.apache.regexp.RE"  
               classpathref="classpath"/>
    <available property="jakarta.oro.present" 
               classname="org.apache.oro.text.regex.Perl5Matcher"  
               classpathref="classpath" />
    <available property="jmf.present" 
               classname="javax.sound.sampled.Clip"  
               classpathref="classpath"/>
    <available property="icontract.present"
               classname="com.reliablesystems.iContract.IContracted"
               classpathref="classpath"/>
  </target>

  <!-- 
       ===================================================================
         Prepare the build              
       =================================================================== 
  -->
  <target name="prepare">
    <tstamp />
  </target>

  <!-- 
       ===================================================================
         Build the code           
       =================================================================== 
  -->
  <target name="build" depends="prepare, check_for_optional_packages">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${build.classes}"/>
    <mkdir dir="${build.lib}"/>
    
    <javac srcdir="${java.dir}"
           destdir="${build.classes}"
           debug="${debug}"
           deprecation="${deprecation}"
           optimize="${optimize}" >
      <classpath refid="classpath" />
      <exclude name="${ant.package}/util/regexp/JakartaRegexpMatcher.java" 
               unless="jakarta.regexp.present" />
      <exclude name="${ant.package}/util/regexp/JakartaOroMatcher.java" 
               unless="jakarta.oro.present" />

      <exclude name="${optional.package}/IContract.java" unless="icontract.present" />
      <exclude name="${optional.package}/Script.java" unless="bsf.present" />
      <exclude name="${optional.package}/StyleBook.java" unless="stylebook.present" />
      <exclude name="${optional.package}/NetRexxC.java" unless="netrexx.present" />
      <exclude name="${optional.package}/XslpLiaison.java" unless="xslp.present" />
      <exclude name="${optional.package}/XalanLiaison.java" unless="xalan.present" />
      <exclude name="${optional.package}/ejb/Ejbc*.java" unless="ejb.ejbc.present" />
      <exclude name="${optional.package}/ejb/DDCreator*.java" unless="ejb.DDCreator.present" />
      <exclude name="${optional.package}/ejb/WLRun.java" unless="ejb.wls.present" />
      <exclude name="${optional.package}/ejb/WLStop.java" unless="ejb.wls.present" />
      <exclude name="${optional.package}/ejb/EjbJar.java" unless="jdk1.2+" />
      <exclude name="${optional.package}/ejb/*DeploymentTool.java" unless="jdk1.2+" />
      <exclude name="${optional.package}/Javah.java" unless="jdk1.2+" />
      <exclude name="${optional.package}/junit/*" unless="junit.present" />
      <exclude name="${optional.package}/net/*.java" unless="netcomp.present" />
      <exclude name="${optional.package}/scm/AntStarTeam*.java" unless="starteam.present" />
      <exclude name="${optional.package}/ANTLR.java" unless="antlr.present" />
      <exclude name="${optional.package}/ide/VAJ*.java" unless="vaj.present" />
      <exclude name="${optional.package}/perforce/*.java" unless="jakarta.oro.present" />
      <exclude name="${optional.package}/sound/*.java" unless="jmf.present" />
      <exclude name="${optional.package}/junit/XMLResultAggregator.java"
               unless="xalan.present" />
      <exclude name="${optional.package}/junit/AggregateTransformer.java"
               unless="xalan.present" />
    </javac>
    
    <copy todir="${build.classes}">
      <fileset dir="${java.dir}">
        <include name="**/*.properties" />
      </fileset>
    </copy>

    <filter token="VERSION" value="${version}" />
    <filter token="DATE" value="${TODAY}" />
    <filter token="TIME" value="${TSTAMP}" />
    <copy    todir="${build.classes}" 
             overwrite="true"
             filtering="on">
      <fileset dir="${java.dir}">
        <include name="**/version.txt" />
        <include name="**/defaultManifest.mf" />
      </fileset>
    </copy>

    <copy todir="${build.classes}/${optional.package}/junit">
       <fileset dir="${java.dir}/${optional.package}/junit">
         <include name="html/**" />
         <include name="xsl/**" />
       </fileset>
    </copy>

  </target>
  
  <!-- 
       =================================================================== 
         Create the ant jars: ant.jar and optional.jar                                                 
       =================================================================== 
  -->
  <target name="jars" depends="build">

    <jar jarfile="${build.lib}/${name}.jar" 
         basedir="${build.classes}" 
         manifest="${manifest}">
      <exclude name="org/apache/tools/ant/taskdefs/optional/**" />
    </jar>

    <jar jarfile="${build.lib}/optional.jar" 
         basedir="${build.classes}" 
         manifest="${manifest}">
      <include name="org/apache/tools/ant/taskdefs/optional/**" />
    </jar>

  </target>

  <!--
       ===================================================================
         Create the essential distribution that can run ant
       ===================================================================
  -->
  <target name="dist-lite" depends="jars, setup-distproperties">

    <mkdir dir="${dist.dir}"/>
    <mkdir dir="${dist.bin}"/>
    <mkdir dir="${dist.lib}"/>

    <copy todir="${dist.lib}">
      <fileset dir="${build.lib}"/>
    </copy>
    
    <copy todir="${dist.bin}">
      <fileset dir="${script.dir}/" />
    </copy>

    <chmod dir="${dist.dir}" perm="go-rwx" />
    <chmod perm="u+x">
       <fileset dir="${dist.bin}">
         <include name="**/ant" />
         <include name="**/antRun" />
       </fileset>
     </chmod>

    <fixcrlf srcdir="${dist.bin}" cr="add" includes="*.bat" />
    <fixcrlf srcdir="${dist.bin}" cr="remove" includes="*.sh" />
    <fixcrlf srcdir="${dist.bin}" cr="remove" includes="ant" />
    <fixcrlf srcdir="${dist.bin}" cr="remove" includes="antRun" />
  </target>

 <!--
       ===================================================================
         Create the complete distribution
       ===================================================================
  -->
  <target name="dist">
    <antcall target="internal_dist">
      <param name="dist.dir" value="${dist.name}" />
    </antcall>
  </target>  
  
  <target name="internal_dist" depends="dist-lite,javadocs">
    <mkdir dir="${dist.docs}"/>
    <mkdir dir="${dist.javadocs}"/>

    <copy todir="${dist.lib}">
      <fileset dir="${lib.dir}">
        <include name="*.jar" />
        <include name="*.zip" />
      </fileset>
    </copy>

    <copy todir="${dist.lib}" file="${lib.dir}/optional/README"/>
    
    <copy todir="${dist.docs}"
          filtering="on">
      <fileset dir="${docs.dir}"/>
    </copy>

    <copy todir="${dist.javadocs}">
      <fileset dir="${build.javadocs}"/>
    </copy>

    <copy todir="${dist.dir}">
      <fileset dir=".">
        <include name="README"/>
        <include name="LICENSE"/>
        <include name="TODO"/>
        <include name="WHATSNEW"/>
      </fileset>
    </copy>

    <chmod dir="${dist.dir}" perm="go-rwx" />

  </target>


  <!--
       ===================================================================
         Target to create bootstrap build
       ===================================================================
  -->
  <target name="bootstrap">
    <antcall target="dist-lite">
      <param name="dist.dir" value="${bootstrap.dir}" />
    </antcall>
  </target>


  <!--
       ===================================================================
         Create the source distribution
       ===================================================================
  -->
  <target name="src-dist" depends="setup-distproperties">

    <mkdir dir="${src.dist.dir}" />
 
    <copy todir="${src.dist.lib}">
      <fileset dir="${lib.dir}">
        <include name="*.jar" />
        <include name="*.zip" />
        <include name="optional/README"/>
      </fileset>
    </copy>

    <copy todir="${src.dist.src}">
      <fileset dir="${src.dir}"/>
    </copy>

    <copy todir="${src.dist.docs}">
      <fileset dir="${docs.dir}"/>
    </copy>

    <copy todir="${src.dist.dir}">
      <fileset dir=".">
        <include name="README"/>
        <include name="LICENSE"/>
        <include name="TODO"/>
        <include name="WHATSNEW"/>
        <include name="build.bat"/>
        <include name="build.sh"/>
        <include name="bootstrap.bat"/>
        <include name="bootstrap.sh"/>
        <include name="build.xml"/>
      </fileset>
    </copy>

    <fixcrlf srcdir="${src.dist.dir}" includes="*.sh" cr="remove"/>
    <fixcrlf srcdir="${src.dist.dir}" includes="ant" cr="remove"/>
    <fixcrlf srcdir="${src.dist.dir}" includes="antRun" cr="remove"/>
    <fixcrlf srcdir="${src.dist.dir}" includes="*.bat" cr="add"/>
    <chmod perm="+x">
      <fileset dir="${src.dist.dir}">
        <include name="*.sh" />
      </fileset>
    </chmod>

    <fixcrlf srcdir="${src.dist.src}" includes="**/*.java" cr="remove"/>
    
  </target>

  <!--
       ===================================================================
         Create the binary distribution
       ===================================================================
  -->
  <target name="distribution">
    <mkdir dir="${dist.base}"/>

    <antcall target="internal_dist">
      <param name="dist.dir" value="${dist.name}" />
    </antcall>
    <zip zipfile="${dist.base}/${dist.name}-bin.zip" 
         basedir="${dist.name}/.." 
         includes="${dist.name}/**"
         excludes="${dist.name}/lib/optional.jar"/>
    <tar longfile="gnu"
         tarfile="${dist.base}/${dist.name}-bin.tar">
      <tarfileset dir="${dist.name}/.." mode="755" username="ant" group="ant">
        <include name="${dist.name}/bin/ant"/>
        <include name="${dist.name}/bin/antRun"/>
      </tarfileset> 
      <tarfileset dir="${dist.name}/.." username="ant" group="ant"> 
        <include name="${dist.name}/**"/>
        <exclude name="${dist.name}/bin/ant"/>
        <exclude name="${dist.name}/bin/antRun"/>
        <exclude name="${dist.name}/lib/optional.jar"/>
      </tarfileset> 
    </tar>
    <gzip zipfile="${dist.base}/${dist.name}-bin.tar.gz" 
          src="${dist.base}/${dist.name}-bin.tar"/>
    <delete file="${dist.base}/${dist.name}-bin.tar"/>
    <copy file="${dist.name}/lib/optional.jar" tofile="${dist.base}/${dist.name}-optional.jar"/>
    <delete dir="${dist.name}" />


    <antcall target="src-dist">
      <param name="src.dist.dir" value="${dist.name}" />
    </antcall>
    <zip zipfile="${dist.base}/${dist.name}-src.zip" 
         basedir="${dist.name}/.." 
         includes="${dist.name}/**"/>
    <tar longfile="gnu"
         tarfile="${dist.base}/${dist.name}-src.tar" >
      <tarfileset dir="${dist.name}/.." mode="755" username="ant" group="ant">
        <include name="${dist.name}/bootstrap.sh"/>
        <include name="${dist.name}/build.sh"/>
      </tarfileset> 
      <tarfileset dir="${dist.name}/.." username="ant" group="ant"> 
        <include name="${dist.name}/**"/>
        <exclude name="${dist.name}/bootstrap.sh"/>
        <exclude name="${dist.name}/build.sh"/>
      </tarfileset>
    </tar> 
    <gzip zipfile="${dist.base}/${dist.name}-src.tar.gz" 
          src="${dist.base}/${dist.name}-src.tar"/>
    <delete file="${dist.base}/${dist.name}-src.tar"/>
    <delete dir="${dist.name}" />
  </target>

  <!--
       ===================================================================
         Cleans up build and distribution directories
       ===================================================================
  -->
  <target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${dist.base}" />
    <delete dir="${defaultdist.dir}" />
    <delete> 
      <fileset dir="." includes="**/*~" defaultexcludes="no"/>
    </delete>
  </target> 

  <!--
       ===================================================================
         Cleans everything
       ===================================================================
  -->
  <target name="allclean" depends="clean">
    <delete file="${bootstrap.dir}/bin/antRun" />
    <delete file="${bootstrap.dir}/bin/antRun.bat" />
  </target> 

  <!--
       ===================================================================
         Installs ant
       ===================================================================
  -->
  <target name="install" if="ant.install">
    <antcall target="dist">
      <param name="dist.dir" value="${ant.install}" />
    </antcall>
  </target>

  <target name="install-lite" if="ant.install">
    <antcall target="dist-lite">
      <param name="dist.dir" value="${ant.install}" />
    </antcall>
  </target>

  <!-- 
       ===================================================================
         Creates the API documentation                             
       =================================================================== 
  -->
  
  <target name="javadoc_check">
    <uptodate property="javadoc.notrequired" targetfile="${build.javadocs}/packages.html" >
        <srcfiles dir= "${java.dir}" includes="**/*.java"/>
    </uptodate>
  </target>

  <target name="javadocs" depends="prepare, javadoc_check" 
          unless="javadoc.notrequired"
          description="Creates the API documentation">
    <mkdir dir="${build.javadocs}"/>
    <javadoc packagenames="org.apache.*"
             sourcepath="${java.dir}"
             destdir="${build.javadocs}"
             author="true"
             version="true"
             windowtitle="${Name} API"
             doctitle="${Name}"
             bottom="Copyright &#169; 2000 Apache Software Foundation. All Rights Reserved.">
      <group title="Ant Core" packages="org.apache.tools.ant*" />
      <group title="Core Tasks" packages="org.apache.tools.ant.taskdefs*" />
      <group title="Optional Tasks" packages="org.apache.tools.ant.taskdefs.optional*" />
    </javadoc>
  </target>
  
  <!-- 
       =================================================================== 
         Compile testcases                                                   
       =================================================================== 
  -->
  <target name="compile-tests" depends="build" if="junit.present">
    <mkdir dir="${build.tests}"/>

    <javac srcdir="${tests.dir}"
           destdir="${build.tests}"
           debug="${debug}"
           deprecation="${deprecation}" >
      <classpath refid="tests-classpath" />

      <exclude name="org/apache/tools/ant/taskdefs/optional/ANTLRTest.java" 
               unless="antlr.present" />
      <exclude name="org/apache/tools/ant/util/regexp/JakartaRegexpMatcherTest.java" 
               unless="jakarta.regexp.present" />
      <exclude name="org/apache/tools/ant/util/regexp/JakartaOroMatcherTest.java" 
               unless="jakarta.oro.present" />
    </javac>
  </target>

  <target name="dump-info">
    <echo message="java.vm.info=${java.vm.info}" />
    <echo message="java.vm.name=${java.vm.name}" />
    <echo message="java.vm.vendor=${java.vm.vendor}" />
    <echo message="java.vm.version=${java.vm.version}" />
    <echo message="os.arch=${os.arch}" />
    <echo message="os.name=${os.name}" />
    <echo message="os.version=${os.version}" />
    <echo message="file.encoding=${file.encoding}" />
    <echo message="user.language=${user.language}" />
  </target>

  <!-- 
       =================================================================== 
         Run testcase                                                        
       =================================================================== 
  -->
  <target name="test" depends="run-tests"/>

  <target name="run-tests" depends="dump-info,compile-tests" if="junit.present">


    <junit printsummary="no" haltonfailure="yes" fork="${junit.fork}">
      <jvmarg value="-classic"/>
      <classpath refid="tests-classpath"/>

      <sysproperty key="build.tests" value="${build.tests}"/>

      <formatter type="plain" usefile="false" />

      <batchtest>
        <fileset dir="${tests.dir}">
          <include name="**/*Test*" />
          <!-- abstract class, not a testcase -->
          <exclude name="org/apache/tools/ant/taskdefs/TaskdefsTest.java" />
          <exclude name="org/apache/tools/ant/util/regexp/RegexpMatcherTest.java" />

          <!-- these tests need to be localised before being ran???? -->
          <exclude name="org/apache/tools/ant/taskdefs/optional/PvcsTest.java" />

          <!-- these depend on order -->
          <exclude name="org/apache/tools/ant/taskdefs/GUnzipTest.java" />
          <exclude name="org/apache/tools/ant/taskdefs/GzipTest.java" />

          <!-- only run this test if ANTLR is installed -->
          <exclude name="org/apache/tools/ant/taskdefs/optional/ANTLRTest.java" 
                   unless="antlr.present" />
          <exclude name="org/apache/tools/ant/util/regexp/JakartaRegexpMatcherTest.java" 
                   unless="jakarta.regexp.present" />
          <exclude name="org/apache/tools/ant/util/regexp/JakartaOroMatcherTest.java" 
                   unless="jakarta.oro.present" />
        </fileset>
      </batchtest>

      <test name="org.apache.tools.ant.taskdefs.GzipTest" />
      <test name="org.apache.tools.ant.taskdefs.GUnzipTest" />
    </junit>

    <!-- clean up again -->
    <delete dir="${tests.etc.dir}/taskdefs/optional/antlr/antlr.tmp" />
    <delete dir="${tests.etc.dir}/taskdefs/taskdefs.tmp" />
    <delete dir="${tests.etc.dir}/taskdefs.tmp" />
    <delete file="${tests.etc.dir}/taskdefs/tmp.jar" />
    <delete dir="${tests.etc.dir}/types/copytest" />
  </target>

  <target name="run-single-test" if="testcase" depends="compile-tests">

    <junit printsummary="no" haltonfailure="yes" fork="${junit.fork}">
      <jvmarg value="-classic"/>
      <sysproperty key="build.tests" value="${build.tests}"/>
      <classpath refid="tests-classpath"/>
      <formatter type="plain" usefile="false" />
      <test name="${testcase}" />
    </junit>
  </target>

  <!-- 
       =================================================================== 
         Main target - runs dist-lite by default                                               
       =================================================================== 
  -->
  <target name="main">
    <antcall target="dist-lite"/>
  </target>
  
</project>
