<?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="bayeux" basedir="../..">


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

  <!-- See "build.properties.sample" in the top level directory for all     -->
  <!-- property values you must customize for successful building!!!        -->
  <property file="${user.home}/build.properties"/>
  <property file="${basedir}/build.properties"/>
  <property file="${basedir}/build.properties.default"/>

  <!-- Project Properties -->
  <property name="project"               value="apache-tomcat" />
  <property name="name"                  value="Apache Tomcat" />
  <property name="year"                  value="2008" />
  <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.build"      value="${basedir}/output/build"/>
  <property name="tomcat.classes"    value="${basedir}/output/classes"/>
  <property name="tomcat.dist"       value="${basedir}/output/dist"/>
  <property name="tomcat.extras"     value="${basedir}/output/extras"/>
  <property name="tomcat.deployer"   value="${basedir}/output/deployer"/>
  <property name="tomcat.release"    value="${basedir}/output/release"/>
  <property name="test.failonerror"  value="true"/>
  <property name="test.runner"       value="junit.textui.TestRunner"/>

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

  <!-- JAR artifacts -->
  <property name="cometd-api.jar" value="${tomcat.extras}/cometd-api.jar"/>
  <property name="tomcat-bayeux.jar" value="${tomcat.extras}/tomcat-bayeux.jar"/>
  <property name="cometd.war" value="${tomcat.extras}/cometd.war"/>
  <property name="tomcat-bayeux-samples.jar" value="${tomcat.extras}/tomcat-bayeux-samples.jar"/>

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

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

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


	<target name="bayeux">
    <mkdir dir="${tomcat.extras}"/>

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

    <antcall target="downloadgz">
      <param name="sourcefile" value="${dojo-js.loc}"/>
      <param name="destfile" value="${dojo-js.jar}"/>
    </antcall>
  	
    <copy todir="${tomcat.extras}" file="${json-lib.home}/${json-lib.jar}"/>
    <!-- Classpath -->
    <path id="tomcat.bayeux.classpath">
      <pathelement path="${tomcat.classpath}"/>
      <pathelement path="${json-lib.home}/${json-lib.jar}"/>
    </path>

    <!-- compile org.apache.tomcat.bayeux -->
    <!-- compile org.apache.cometd -->
    <javac srcdir="modules/bayeux/java" destdir="${tomcat.classes}"
           debug="${compile.debug}"
           deprecation="${compile.deprecation}"
           source="${compile.source}"
           encoding="ISO-8859-1">
      <classpath refid="tomcat.bayeux.classpath" />
      <include name="org/apache/tomcat/bayeux/**" />
      <include name="org/apache/cometd/**" />
    </javac>

    <!-- Cometd API JAR File -->
    <jar jarfile="${cometd-api.jar}">
      <fileset dir="${tomcat.classes}">
        <exclude name="**/package.html" />
        <exclude name="**/LocalStrings_*" />
        <include name="org/apache/cometd/**" />
      </fileset>
    </jar>
    <!-- Cometd API JAR File -->
    <jar jarfile="${tomcat-bayeux.jar}">
      <fileset dir="${tomcat.classes}">
        <exclude name="**/package.html" />
        <exclude name="**/LocalStrings_*" />
        <include name="org/apache/tomcat/bayeux/**" />
      </fileset>
    </jar>

    <!-- cometd samples application -->
    <javac srcdir="modules/bayeux/test" destdir="${tomcat.classes}"
           debug="${compile.debug}"
           deprecation="${compile.deprecation}"
           source="${compile.source}"
           encoding="ISO-8859-1">
      <classpath refid="tomcat.bayeux.classpath" />
      <include name="org/apache/tomcat/bayeux/**" />
      <include name="org/apache/cometd/**" />
    </javac>

    <!-- Cometd samples JAR File -->
    <jar jarfile="${tomcat-bayeux-samples.jar}">
      <fileset dir="${tomcat.classes}">
        <exclude name="**/package.html" />
        <exclude name="**/LocalStrings_*" />
        <include name="org/apache/cometd/bayeux/samples/**" />
      </fileset>
    </jar>

    <!-- build samples webapplication /cometd -->
  	<property name="cometd-app" value="${base.path}/cometd"/>
  	<mkdir dir="${cometd-app}"/>
  	
  	<copy todir="${cometd-app}">
  	  <fileset dir="${basedir}/modules/bayeux/webapps/cometd">
  		<include name="**/**"/>
  	  </fileset>
      <fileset dir="${dojo-js.home}">
      	<include name="dojo/**"/>
      	<include name="dojox/**"/>
      </fileset>	
  	</copy>
    <mkdir dir="${cometd-app}/WEB-INF/lib"/>
    <copy todir="${cometd-app}/WEB-INF/lib" file="${tomcat-bayeux-samples.jar}"/>
  	
    <zip zipfile="${cometd.war}">
      <fileset dir="${cometd-app}">
        <include name="**/**"/>
      </fileset>
    </zip>
  	
  	<delete dir="${cometd-app}"/>
  	
  	<!-- create checksums -->
    <checksum file="${cometd-api.jar}" forceOverwrite="yes" fileext=".md5" />
    <checksum file="${tomcat-bayeux.jar}" forceOverwrite="yes" fileext=".md5" />
    <checksum file="${cometd.war}" forceOverwrite="yes" fileext=".md5" />
    <checksum file="${tomcat.extras}/${json-lib.jar}" forceOverwrite="yes" fileext=".md5" />
  	
  	<!-- print out how to -->
    <echo>You've built the Tomcat Bayeux libraries, simply add the following libraries to your CATALINA_HOME/lib directory:
          ${cometd-api.jar}
          ${tomcat-bayeux.jar}
          ${tomcat.extras}/${json-lib.jar}
To run the sample application, copy the following applications into your CATALINA_BASE/webapps directory
          ${cometd.war}
    </echo>
  </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="downloadgz" unless="exist" depends="setproxy,testexist">
    <!-- Download and extract the package -->
    <get src="${sourcefile}" dest="${base.path}/file.tar.gz" />
    <gunzip src="${base.path}/file.tar.gz" dest="${base.path}/file.tar"/>
    <untar src="${base.path}/file.tar" dest="${base.path}"/>
    <delete file="${base.path}/file.tar"/>
    <delete file="${base.path}/file.tar.gz"/>
  </target>


</project>
