<?xml version="1.0"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<project name="Tomcat 6.0" default="extras" basedir=".">


  <!-- ===================== Initialize Property Values =================== -->

  <!-- We read customizable properties from the file "build.properties.default" -->
  <!-- and also from "build.properties" if it exists.                           -->
  <!-- The values in "build.properties" have stronger preference.               -->
  <!-- If you want to customize your build, you can either change the values    -->
  <!-- directly in the default file, or create a new build.properties and set   -->
  <!-- the values there. This way you don't have to change a file which is part -->
  <!-- of the original project source code.                                     -->
  <!-- See "build.properties.default" in the top level directory for some       -->
  <!-- property values you may customize.                                       -->
  <property file="${user.home}/build.properties"/>
  <property file="build.properties"/>

  <property file="build.properties.default"/>

  <!-- Project Properties -->
  <property name="project"               value="apache-tomcat" />
  <property name="name"                  value="Apache Tomcat" />
  <tstamp>
    <format property="year" pattern="yyyy" locale="en"/>
  </tstamp>
  <property name="version.major"         value="6" />
  <property name="version.minor"         value="0" />
  <property name="version.build"         value="0" />
  <property name="version.patch"         value="0" />
  <property name="version.suffix"        value="-dev" />

  <property name="version"               value="${version.major}.${version.minor}.${version.build}${version.suffix}" />
  <property name="version.number"        value="${version.major}.${version.minor}.${version.build}.${version.patch}" />
  <property name="version.major.minor"   value="${version.major}.${version.minor}" />

  <property name="final.name"            value="${project}-${version}" />
  <property name="final-src.name"        value="${project}-${version}-src" />

  <!-- Build Defaults -->
  <property name="tomcat.output"     value="${basedir}/output"/>
  <property name="tomcat.build"      value="${tomcat.output}/build"/>
  <property name="tomcat.classes"    value="${tomcat.output}/classes"/>
  <property name="tomcat.dist"       value="${tomcat.output}/dist"/>
  <property name="tomcat.extras"     value="${tomcat.output}/extras"/>
  <property name="tomcat.manifests"  value="${tomcat.output}/manifests"/>
  <property name="tomcat.deployer"   value="${tomcat.output}/deployer"/>
  <property name="tomcat.release"    value="${tomcat.output}/release"/>

  <!-- constant to declare a file binary for md5sum -->
  <property name="md5sum.binary-prefix" value=" *" />

  <!-- Can't be lower - jsp uses templates -->
  <property name="compile.source" value="1.5"/>

  <!-- JAR artifacts -->
  <property name="bootstrap.jar" value="${tomcat.build}/bin/bootstrap.jar"/>

  <property name="annotations-api.jar" value="${tomcat.build}/lib/annotations-api.jar"/>
  <property name="servlet-api.jar" value="${tomcat.build}/lib/servlet-api.jar"/>
  <property name="jsp-api.jar" value="${tomcat.build}/lib/jsp-api.jar"/>
  <property name="el-api.jar" value="${tomcat.build}/lib/el-api.jar"/>
  <property name="catalina.jar" value="${tomcat.build}/lib/catalina.jar"/>
  <property name="catalina-ant.jar" value="${tomcat.build}/lib/catalina-ant.jar"/>
  <property name="tomcat-coyote.jar" value="${tomcat.build}/lib/tomcat-coyote.jar"/>

  <property name="jasper.jar" value="${tomcat.build}/lib/jasper.jar"/>
  <property name="jasper-el.jar" value="${tomcat.build}/lib/jasper-el.jar"/>

  <property name="tomcat-dbcp.home" value="${base.path}/tomcat${version.major}-deps/dbcp" />
  <property name="tomcat-dbcp.jar" value="${tomcat-dbcp.home}/tomcat-dbcp.jar"/>

  <property name="tomcat-juli.jar" value="${tomcat.extras}/tomcat-juli.jar"/>
  <property name="tomcat-juli-adapters.jar" value="${tomcat.extras}/tomcat-juli-adapters.jar"/>
  <property name="catalina-ws.jar" value="${tomcat.extras}/catalina-ws.jar"/>

  <property name="catalina-jmx-remote.jar" value="${tomcat.extras}/catalina-jmx-remote.jar"/>

  <!-- Classpath -->
  <path id="tomcat.classpath">
    <pathelement location="${tomcat.classes}"/>
  </path>

  <target name="prepare">
    <mkdir dir="${tomcat.extras}"/>
    <mkdir dir="${tomcat.extras}/logging"/>
    <mkdir dir="${tomcat.extras}/webservices"/>
  </target>

  <target name="clean">
    <delete dir="${tomcat.extras}"/>
  </target>

  <target name="commons-logging-prepare"
          depends="prepare"
          description="Prepare to build web services extras package">

    <antcall target="downloadfile-2">
      <param name="sourcefile.1" value="${commons-logging-src.loc.1}"/>
      <param name="sourcefile.2" value="${commons-logging-src.loc.2}"/>
      <param name="destfile" value="${commons-logging-src.tar.gz}"/>
      <param name="destdir" value="${commons-logging.home}"/>
    </antcall>

    <antcall target="downloadfile">
      <param name="sourcefile" value="${avalon-framework.loc}"/>
      <param name="destfile" value="${avalon-framework.jar}"/>
      <param name="destdir" value="${avalon-framework.home}"/>
    </antcall>

    <antcall target="downloadfile">
      <param name="sourcefile" value="${log4j.loc}"/>
      <param name="destfile" value="${log4j.jar}"/>
      <param name="destdir" value="${log4j.home}"/>
    </antcall>

    <antcall target="downloadfile">
      <param name="sourcefile" value="${logkit.loc}"/>
      <param name="destfile" value="${logkit.jar}"/>
      <param name="destdir" value="${logkit.home}"/>
    </antcall>

    <antcall target="downloadfile">
      <param name="sourcefile" value="${servletapi.loc}"/>
      <param name="destfile" value="${servletapi.jar}"/>
      <param name="destdir" value="${servletapi.home}"/>
    </antcall>

  </target>

  <target name="commons-logging"
          depends="commons-logging-prepare"
          description="Build JULI for log4j extras package">

    <gunzip src="${commons-logging-src.tar.gz}"
      dest="${tomcat.extras}/logging/commons-logging-src.tar"/>
    <untar src="${tomcat.extras}/logging/commons-logging-src.tar"
      dest="${tomcat.extras}/logging/"/>

    <replace dir="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src/src/java/org/apache/commons"
        encoding="ISO-8859-1">
      <replacefilter token="org.apache.commons"
            value="org.apache.juli" />
    </replace>
    <mkdir dir="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src/src/java/org/apache/juli" />
    <move todir="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src/src/java/org/apache/juli">
      <fileset dir="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src/src/java/org/apache/commons" />
    </move>

    <replace dir="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src"
      encoding="ISO-8859-1">
      <replacefilter token="org.apache.commons"
        value="org.apache.juli" />
      <replacefilter token="org/apache/commons/"
        value="org/apache/juli/" />
    </replace>
    <copy tofile="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src/build2.xml"
      file="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src/build.xml" />

    <copy todir="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src">
      <fileset file="${avalon-framework.jar}" />
      <fileset file="${log4j.jar}" />
      <fileset file="${logkit.jar}" />
      <fileset file="${servletapi.jar}" />
    </copy>

    <ant antfile="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src/build2.xml"
         dir="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src"
         target="compile" />

    <jar jarfile="${tomcat-juli.jar}"
         manifest="${tomcat.manifests}/default.manifest" >
      <fileset dir="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src/target/classes">
        <include name="org/apache/juli/logging/*.class" />
        <include name="org/apache/juli/logging/impl/LogFactoryImpl*.class" />
        <include name="org/apache/juli/logging/impl/WeakHashtable*.class" />
        <include name="org/apache/juli/logging/impl/SimpleLog*.class" />
        <include name="org/apache/juli/logging/impl/NoOpLog*.class" />
        <include name="org/apache/juli/logging/impl/Jdk14Logger.class" />
        <!-- Javadoc and i18n exclusions -->
        <exclude name="**/package.html" />
        <exclude name="**/LocalStrings_*" />
      </fileset>
      <fileset dir="${tomcat.classes}">
        <include name="org/apache/juli/*" />
        <!-- Javadoc and i18n exclusions -->
        <exclude name="**/package.html" />
        <exclude name="**/LocalStrings_*" />
      </fileset>
      <zipfileset file="${tomcat.manifests}/default.notice"
        fullpath="META-INF/NOTICE" />
      <zipfileset file="${tomcat.manifests}/default.license"
        fullpath="META-INF/LICENSE" />
    </jar>
    <antcall target="md5sum">
      <param name="file" value="${tomcat-juli.jar}" />
    </antcall>

    <jar jarfile="${tomcat-juli-adapters.jar}"
      manifest="${tomcat.manifests}/default.manifest" >
      <fileset dir="${tomcat.extras}/logging/commons-logging-${commons-logging.version}-src/target/classes">
        <include name="org/apache/juli/logging/impl/**.class" />
        <exclude name="org/apache/juli/logging/impl/WeakHashtable*.class" />
        <exclude name="org/apache/juli/logging/impl/LogFactoryImpl.class" />
        <!-- Javadoc and i18n exclusions -->
        <exclude name="**/package.html" />
        <exclude name="**/LocalStrings_*" />
      </fileset>
      <zipfileset file="${tomcat.manifests}/default.notice"
        fullpath="META-INF/NOTICE" />
      <zipfileset file="${tomcat.manifests}/default.license"
        fullpath="META-INF/LICENSE" />
    </jar>
    <antcall target="md5sum">
      <param name="file" value="${tomcat-juli-adapters.jar}" />
    </antcall>
  </target>

  <target name="webservices-prepare"
          depends="prepare"
          description="Prepare to build web services extras package">

    <antcall target="downloadfile">
      <param name="sourcefile" value="${jaxrpc-lib.loc}"/>
      <param name="destfile" value="${jaxrpc-lib.jar}"/>
      <param name="destdir" value="${jaxrpc-lib.home}"/>
    </antcall>

    <antcall target="downloadfile">
      <param name="sourcefile" value="${wsdl4j-lib.loc}"/>
      <param name="destfile" value="${wsdl4j-lib.jar}"/>
      <param name="destdir" value="${wsdl4j-lib.home}"/>
    </antcall>

    <copy file="${jaxrpc-lib.jar}"
          tofile="${tomcat.extras}/webservices/jaxrpc.jar" />
    <copy file="${wsdl4j-lib.jar}"
          tofile="${tomcat.extras}/webservices/wsdl4j.jar" />
  </target>

  <target name="webservices"
          depends="webservices-prepare"
          description="Build web services extras package">

    <!-- Classpath -->
    <path id="tomcat.webservices.classpath">
      <pathelement path="${tomcat.classpath}"/>
      <fileset dir="${tomcat.extras}/webservices">
        <include name="jaxrpc.jar"/>
        <include name="wsdl4j.jar"/>
      </fileset>
    </path>

    <!-- Compile internal server components -->
    <javac srcdir="java" destdir="${tomcat.classes}"
           debug="${compile.debug}"
           deprecation="${compile.deprecation}"
           source="${compile.source}"
           encoding="ISO-8859-1">
      <classpath refid="tomcat.webservices.classpath" />
      <include name="org/apache/naming/factory/webservices/**" />
    </javac>

    <!-- Catalina Main JAR File -->
    <jar jarfile="${catalina-ws.jar}"
         manifest="${tomcat.manifests}/default.manifest">
      <fileset dir="${tomcat.classes}">
        <include name="org/apache/naming/factory/webservices/**" />
        <!-- Javadoc and i18n exclusions -->
        <exclude name="**/package.html" />
        <exclude name="**/LocalStrings_*" />
      </fileset>
      <zipfileset file="${tomcat.manifests}/default.notice"
        fullpath="META-INF/NOTICE" />
      <zipfileset file="${tomcat.manifests}/default.license"
        fullpath="META-INF/LICENSE" />
    </jar>
    <antcall target="md5sum">
      <param name="file" value="${catalina-ws.jar}" />
    </antcall>
  </target>

  <target name="jmx-remote" >
    <!-- Create the JAR file -->
    <jar jarfile="${catalina-jmx-remote.jar}"
      manifest="${tomcat.manifests}/default.manifest" >
      <fileset dir="${tomcat.classes}">
        <include name="org/apache/catalina/mbeans/JmxRemote*" />
      </fileset>
      <zipfileset file="${tomcat.manifests}/default.notice"
        fullpath="META-INF/NOTICE" />
      <zipfileset file="${tomcat.manifests}/default.license"
        fullpath="META-INF/LICENSE" />
    </jar>
    <antcall target="md5sum">
      <param name="file" value="${catalina-jmx-remote.jar}" />
    </antcall>
  </target>

  <target name="extras"
      depends="commons-logging,webservices,jmx-remote">
  </target>

  <!-- Download and dependency building -->
  <target name="proxyflags">
    <!-- check proxy parameters. -->
    <condition property="useproxy">
      <equals arg1="${proxy.use}" arg2="on" />
    </condition>
  </target>

  <target name="setproxy" depends="proxyflags" if="useproxy">
    <taskdef name="setproxy"
            classname="org.apache.tools.ant.taskdefs.optional.net.SetProxy" />
    <setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}"
              proxyuser="${proxy.user}" proxypassword="${proxy.password}" />
    <echo message="Using ${proxy.host}:${proxy.port} to download ${sourcefile}"/>
  </target>

  <target name="testexist">
    <echo message="Testing  for ${destfile}"/>
    <available file="${destfile}" property="exist"/>
  </target>

  <target name="downloadfile" unless="exist" depends="setproxy,testexist">
    <!-- Download extract the file -->
    <mkdir dir="${destdir}" />
    <get src="${sourcefile}" dest="${destfile}" />
  </target>

  <target name="downloadfile-2" unless="exist" depends="testexist">
    <!-- Download the file from the two alternative locations -->
    <mkdir dir="${destdir}" />

    <antcall target="trydownload">
      <param name="sourcefile" value="${sourcefile.1}" />
    </antcall>

    <antcall target="trydownload">
      <param name="sourcefile" value="${sourcefile.2}" />
    </antcall>

    <available file="${destfile}" property="exist"/>
    <fail unless="exist" message="Failed to download [${destfile}]. All download sources are unavailable." />
  </target>

  <target name="trydownload.check" depends="setproxy">
    <condition property="trydownload.run">
      <and>
        <not>
          <available file="${destfile}" />
        </not>
        <http url="${sourcefile}" />
      </and>
    </condition>
  </target>

  <target name="trydownload" if="trydownload.run" depends="trydownload.check">
    <!-- Downloads a file if not yet downloaded and the source URL is available -->
    <get src="${sourcefile}" dest="${destfile}" />
  </target>

  <!-- Helper target, used to create a md5 checksum file  -->
  <!-- Requires 'file' as a parameter. -->
  <target name="md5sum">
    <fail unless="file" />
    <fail if="filename" />
    <fail if="value" />
    <basename file="${file}" property="filename" />
    <checksum file="${file}" property="value" />
    <echo file="${file}.md5" message="${value}${md5sum.binary-prefix}${filename}" />
  </target>
</project>
