<?xml version="1.0"?>
<!--
  Copyright 2002-2004 The Apache Software Foundation

  Licensed 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 default="webapp" basedir="." name="webapp">
  
    <target name="project.webapp.defined" unless="project.webapp">
    <fail>
      ------------------------------------------------
      Error: $${project.webapp} variable has not been set. This is where the
      webapp will be assembled. Please define it in the calling script.
      ------------------------------------------------
    </fail>
  </target>
  
  <!-- ===============================================================
       Builds a cocoon webapp for local execution.
       Note, this webapp is not suitable for deploying remotely as it
       is configured to allow editing of files in the ${project.home} 
       directory.
       param: location to put generated webapp ${project.webapp}
       =============================================================== -->
  
<target name="webapp-local" depends="init, validate, project.webapp.defined, ensure-content, prepare-context,
         load-project-props"
   description="Generates a dynamic servlet-based website (an unpackaged webapp).
   Note this webapp is suitable for local execution only, use the 'webapp'
   target if you wish to deploy remotely.">
    <!-- this will need revisions in the case of the siteplan
    extra project-custom classes then will need to be warred in as well. -->
<!--  should be already there
    <mkdir dir="${project.webapp}/WEB-INF"/>
    <copy file="${forrest.home}/context/WEB-INF/web.xml" filtering="true" 
      todir="${project.webapp}/WEB-INF"/>
-->
    
    <!-- Prepare the Lucene search index -->
    <!-- FIXME: There must be a better way to configure this.
         Is this really a concern of the skinconf?
         The indexer must be off by default. If the optional skinconf parameter
         "disable-lucene" is missing or false, then off. 
    -->
    <condition property="use-lucene-indexer">
      <!-- sorry for the convolution -->
      <and>
        <isset property="skinconfig.disable-lucene"/>
        <isfalse value="${skinconfig.disable-lucene}"/>
      </and>
    </condition>
    <antcall target="lucene-index"/>
    
    <available file="${content-dir}/jettyconf.xml" 
      property="custom_jetty_config"/>

    <loadfile  property="webapp-local-generated-message" srcFile="${forrest.home}/var/webapp-local-generated-message.txt">
        <filterchain><expandproperties/></filterchain>
    </loadfile>
    <echo>${webapp-local-generated-message}</echo>
  </target>
  



<!-- ===============================================================
       Builds a cocoon webapp of your project for remote deployment.
       param: location to put generated webapp ${project.webapp}
       =============================================================== -->
  <target name="webapp" depends="webapp-local, copy-xdocs, copy-content"
   description="Generates a dynamic servlet-based website (an unpackaged webapp).">
   
   <!--
     Copy libs
   -->
    <copy toDir="${project.webapp}/WEB-INF/lib" 
      overwrite="true" failonerror="false">
      <fileset dir="${lib-dir}"/>
    </copy>

    <copy toDir="${project.webapp}/WEB-INF/classes" 
      overwrite="true" failonerror="false">
      <fileset dir="${classes-dir}"/>
    </copy>
  
    <copy todir="${project.webapp}/WEB-INF/lib">
    <!-- Note: no overwriting, so user's libs and classes get preference -->
      <fileset dir="${forrest.core.lib-dir}">
        <include name="**.jar"/>
      </fileset>
      <fileset dir="${forrest.optional.lib-dir}">
        <include name="**.jar"/>
      </fileset>
      <fileset dir="${forrest.endorsed.lib-dir}">
        <include name="**.jar"/>
      </fileset>
    </copy>
    
    <copy todir="${project.webapp}/WEB-INF/lib">
      <fileset dir="${forrest.build.lib-dir}">
        <include name="**.jar"/>
      </fileset>
    </copy>
                  
    <copy toDir="${project.webapp}/WEB-INF/classes">
      <fileset dir="${forrest.home}/context/WEB-INF/classes"/>
    </copy>

    <!-- Copy XML jars if not running JDK 1.4 (where they're built-in) -->
    <available property="jdk1.4+" classname="java.lang.CharSequence"/>
    <copy todir="${project.webapp}/WEB-INF/lib">
      <fileset dir="${forrest.endorsed.lib-dir}">
        <include name="*jar" unless="jdk1.4+"/>
      </fileset>
    </copy>
       
   <!-- 
    | Copy all non-xdocs content across to the webapp
   -->
   <mkdir dir="${project.webapp}/project"/>   
   <copy toDir="${project.webapp}/project" overwrite="true" failonerror="false">
     <fileset dir="${project.home}">
       <include name="**"/>
       <exclude name="build/**"/>
     </fileset>
   </copy>
        
    <!-- copy custom xmaps -->
    <copy toDir="${project.webapp}" 
      overwrite="true"
      failonerror="false">
      <fileset dir="${project.content-dir}">
        <include name="*.xmap"/>
      </fileset>
    </copy>
      
    <loadfile  property="webapp-generated-message" 
      srcFile="${forrest.home}/var/webapp-generated-message.txt">
        <filterchain><expandproperties/></filterchain>
    </loadfile>
    <echo>${webapp-generated-message}</echo>
  </target>
       
  <target name="copy-xdocs" if="xdocs.present">
  </target>
  
  <target name="copy-content" if="raw-content.present">
  </target>

 <!-- ===============================================================
       Builds a cocoon .war for your project based on the siteplan.
       param: location to put generated war ${project.war}
       =============================================================== -->
 <target name="war" depends="webapp"
   description="Generates a dynamic servlet-based website (an packaged .war file)">

   <jar destfile="${project.war}" basedir="${project.webapp}"/>
   <echo>
---------------------------------
Webapp .war file generated:
${project.war}
---------------------------------
   </echo>
  </target> 

</project>
