<?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.
-->
<!-- $Id: webapp-build.xml 607696 2007-12-31 08:04:01Z cziegeler $ -->
<project name="webapp">

  <description>
    Webapp targets
  </description>

  <target name="prepare-webapp" depends="blocks, package">
    <mkdir dir="${build.webapp}"/>

    <!-- Important to use here encoding="iso-8859-1" to avoid
         mutilating LATIN-1 characters in the copy operation.
         If these were read assuming UTF-8 encoding, umlauts
         are invalid byte sequences and get replaced by '?'.
    
         On the other hand, reading UTF-8 files using LATIN-1
         encoding is not a problem in this context since every
         UTF-8 byte sequence maps to one or more LATIN-1 characters.
         We only need to assume that the tokens to be replaced
         by the filtering option are written in ASCII only.
     -->
    <copy filtering="on" todir="${build.webapp}" encoding="iso-8859-1">
      <fileset dir="${webapp}">
        <exclude name="**/*.ico"/>
        <exclude name="**/*.gif"/>
        <exclude name="**/*.jpg"/>
        <exclude name="**/*.png"/>
        <exclude name="samples/**"/>
        <exclude name="test-suite/**"/>
      </fileset>
    </copy>
    <copy filtering="off" todir="${build.webapp}">
      <fileset dir="${webapp}">
        <exclude name="samples/**"/>
        <exclude name="test-suite/**"/>
        <include name="**/*.ico"/>
        <include name="**/*.gif"/>
        <include name="**/*.jpg"/>
        <include name="**/*.png"/>
      </fileset>
    </copy>

    <!-- generate sitemap entries
    <sitemap-components sitemap="${build.webapp}/sitemap.xmap" source="${java}"/>
    -->
    <copy file="${build}/${name}.jar"
          tofile="${build.webapp.lib}/${name}-${version}.jar"/>

    <copy todir="${build.webapp.lib}">
      <fileset dir="${lib}/endorsed">
        <include name="*.jar"/>
      </fileset>
      <fileset dir="${lib.core}">
        <include name="*.jar"/>
        <exclude name="servlet*.jar"/>
      </fileset>
      <!-- Currently, we have no JVM dependent libraries
        <fileset dir="${lib.core}/jvm${target.vm}">
          <include name="*.jar"/>
        </fileset>
      -->
      <fileset dir="${lib.local}">
        <include name="*.jar"/>
      </fileset>
    </copy>

    <copy todir="${build.webapp.lib}">
      <fileset dir="${build.blocks}">
        <include name="*-block.jar"/>
      </fileset>
      <mapper type="glob" from="*-block.jar" to="cocoon-*-block.jar"/>
    </copy>

    <if>
      <isfalse value="${internal.exclude.webapp.test-suite}"/>
      <then>
        <copy file="${build}/${name}-testcase.jar" todir="${build.webapp.lib}"/>
      </then>
    </if>

    <if>
      <istrue value="${include.sources-jars}"/>
      <then>
        <copy file="${build}/${name}.src.jar"
              tofile="${build.webapp.lib}/${name}-${version}.src.jar"/>
        <copy todir="${build.webapp.lib}">
          <fileset dir="${build.blocks}">
            <include name="*-block.src.jar"/>
          </fileset>
          <mapper type="glob" from="*-block.src.jar" to="cocoon-*-block.src.jar"/>
        </copy>
      </then>
    </if>

    <ant antfile="${build.temp}/blocks-build.xml"
         inheritAll="true"
         inheritRefs="false"
         target="lib"/>

    <ant antfile="${build.temp}/blocks-build.xml"
         inheritAll="true"
         inheritRefs="false"
         target="patch"/>
  </target>

  <target name="prepare-webapp-samples" depends="prepare-webapp, samples, block-samples" unless="internal.exclude.webapp.samples"/>

  <target name="prepare-webapp-test-suite" depends="prepare-webapp" unless="internal.exclude.webapp.test-suite">
    <!-- copy test suite files -->
    <copy todir="${build.webapp.test-suite}">
      <fileset dir="${webapp.test-suite}"/>
    </copy>
  </target>

  <target name="prepare-webapp-deprecated" depends="prepare-webapp" unless="internal.exclude.deprecated">
    <copy file="${build}/${name}-deprecated.jar" tofile="${build.webapp.lib}/${name}-${version}-deprecated.jar"/>
    <xpatch file="${build.webapp}/WEB-INF/cocoon.xconf"
            srcdir="${deprecated.conf}"
            includes="**/*.xconf"
            addComments="true"/>
  </target>

  <target name="webapp" depends="prepare-webapp,prepare-webapp-samples,prepare-webapp-test-suite,prepare-webapp-deprecated,validate-jars,validate-config,custom-conf,hsqldb-port" description="Builds web application folder">
  </target>

  <target name="war" depends="webapp" description="Builds web application archive">
    <!-- A task to create manifest for webapp. -->
    <taskdef name="manifest" classname="ManifestToolTask" classpath="${tools.tasks.dest}"/>
    <!-- Create WAR manifest -->
    <manifest directory="${build.webapp.lib}" manifest="${build.webapp}/WEB-INF/Manifest.mf"/>
    <!-- Package WAR file -->
    <jar jarfile="${build.war}"  manifest="${build.webapp}/WEB-INF/Manifest.mf" index="true">
      <fileset dir="${build.webapp}"/>
    </jar>
  </target>

  <target name="custom-conf" depends="init-tasks" description="Uses Cocoon's xpatch task to customize runtime configuration">
    <xpatch file="${build.webapp}/sitemap.xmap" srcdir="">
       <include name="${customconf}/*.xmap" />
       <include name="${customconf}/*.xpipe" />
    </xpatch>
    <xpatch file="${build.webapp}/WEB-INF/cocoon.xconf" srcdir="" addComments="true">
       <include name="${customconf}/*.xconf" />
    </xpatch>
    <xpatch file="${build.webapp}/WEB-INF/logkit.xconf" srcdir="">
       <include name="${customconf}/*.xlog" />
    </xpatch>
    <xpatch file="${build.webapp}/WEB-INF/web.xml" srcdir="">
       <include name="${customconf}/*.xweb" />
    </xpatch>
  </target>
  
  <!-- Change the hsqldb server port if specified in the local properties -->
  <target name="hsqldb-port" if="build.webapp.hsqldb-server-port">
    <replace file="${build.webapp}/WEB-INF/cocoon.xconf"
             token="jdbc:hsqldb:hsql://localhost:9002"
             value="jdbc:hsqldb:hsql://localhost:${build.webapp.hsqldb-server-port}"/>
    <replace file="${build.webapp}/WEB-INF/cocoon.xconf"
             token='parameter name="port" value="9002"'
             value='parameter name="port" value="${build.webapp.hsqldb-server-port}"'/>
  </target>
</project>
