<?xml version="1.0"?>

<!-- ===================================================================
     This file is called by other build files to create web applications.

     It is assumed that no changes will be made to this file. It should be
     possible to make all changes needed by modifying the configuration
     properties or a bedework.build.proeprties file

     An unwrapped web application will be built in the temp directory
     and a war and ear file will be left in the dist directory.

     Properties which must be defined on entry, apart from those described
     in the main build file are:
    app.root.dir       Location of the root directory for the apps
    app.sou.dir        Location of the application skeleton,
    app.name           Name of the application. This wil be used to determine
                       the name of the file containing the rest of the
                       application properties, the directory we build in
                       and the name of war and ear files.
     These should be defined in the build properties for the application.

     It is possible to define any of the app.dest properties below to override
     the location of destination files.

     In the directory
        ${apps.sou.dir}
     we expect an application skeleton formatted like a web-application
     For the purposes of this build it should contain a
     structure like
              war - docs    (contains jsp and html for this app)
                    WEB-INF - struts-config.xml
                              web.xml
                              jboss-web.xml (for jboss only)

     Authors: Mike Douglass
     =================================================================== -->

<project name="bedework.buildwar" default="build" >
  <import file="${build.dir}/buildfilters.xml" />

  <if>
    <isset property="org.bedework.global.portal.platform" />
    <then>
      <import file="${build.dir}/portals/${org.bedework.global.portal.platform}War.xml" />
    </then>
  </if>

  <import file="${build.dir}/platforms/${org.bedework.global.deploy.platform}War.xml" />

  <target name="init" depends="init.common">
    <if>
      <isset property="org.bedework.global.build.ear" />
      <then>
        <!-- Common library in ear file -->
        <property name="app.dest.lib"
                  location="${org.bedework.ear.templib}" />

        <!-- Properties all go into a single jar file at termination -->
        <property name="app.dest.classes"
                  location="${org.bedework.ear.properties.dir}" />
      </then>
      <else>
        <!-- Library in war file -->
        <property name="app.dest.lib"
                  location="${app.dest.webinf}/lib" />

        <!-- Properties in the war file -->
        <property name="app.dest.classes"
                  location="${app.dest.webinf}/classes" />
      </else>
    </if>
    <property name="app.dest.properties.old"
              location="${app.dest.classes}/properties/calendar" />
    <property name="app.dest.properties"
              location="${app.dest.classes}/properties" />
  </target>

  <target name="init.common">
    <!-- Destinations - where we build an unwrapped war -->
    <property name="app.dest.war"
              location="${org.bedework.temp.wars.home}/${propval.app.war.name}.war" />
    <property name="app.dest.docs"
              location="${app.dest.war}/docs" />
    <property name="app.dest.webinf"
              location="${app.dest.war}/WEB-INF" />
    <property name="app.dest.metainf"
              location="${app.dest.war}/META-INF" />

    <!--temp web.xml location  -->
    <property name="app.temp.web.xml"
              location="${app.dest.webinf}/tempweb.xml" />

    <!--final web.xml location  -->
    <property name="app.dest.web.xml"
              location="${app.dest.webinf}/web.xml" />

    <property name="app.war.file"
              location="${dist.home}/${propval.app.war.name}.war" />

    <!-- default web.xml -->
    <property name="propval.app.web.xml"
              value="war/WEB-INF/web.xml" />

              <!--
    <property name="app.web.xml"
              location="${propval.app.web.xml}" />
              -->
    <resolveFile name="app.web.xml"
                 file="${propval.app.web.xml}"
                 base="${app.sou.dir}"/>

    <property name="app.sou.properties"
              location="${app.sou.dir}/war/WEB-INF/classes/servlet.properties" />
  </target>

  <target name="build" depends="init">
    <echo message="**************************************************" />
    <echo message=" * Building war ${propval.app.war.name}"/>
    <echo message="**************************************************" />

    <delete dir="${app.dest.war}" />

    <!-- Add modifed login pages -->
    <copy todir="${app.dest.docs}/login" failonerror="false" >
      <fileset dir="${org.bedework.common.resources}/login" />
      <filterset>
        <filter token="DISPLAY-NAME" value="${propval.app.display.name}"/>
      </filterset>
    </copy>

    <!-- ===============================================================
         We copy 3 sets of properties.

         First (for struts) are the servlet properties which ends up as
              classes/servlet.properties
         Next we copy in a set of generic property files and then with
         overwrite we copy in a set of base specific files.

         All are updated with new values.
         =============================================================== -->

    <if>
      <available file="${app.sou.dir}/war/docs" type="dir" />
      <then>
        <copy todir="${app.dest.docs}">
          <fileset dir="${app.sou.dir}/war/docs" />
        </copy>
      </then>
    </if>

    <!-- ===============================================================
         Any property files we need for configuration, other than the
         standard struts file, should be in the "${app.sou.dir}/properties
         directory. They will be copied into the appropriate place for the
         war and transformed by the above filter.
         =============================================================== -->

    <!-- Create the runtime options file -->
    <mkdir dir="${app.dest.properties}" />

    <if>
      <available file="${app.sou.dir}/properties" type="dir" />
      <then>
        <copy todir="${app.dest.properties}" overwrite="yes" >
          <fileset dir="${app.sou.dir}/properties"
                   includes="*.properties"/>
          <filterset refid="property.filters" />
        </copy>
      </then>
    </if>

    <copy tofile="${app.dest.properties}/options.xml"
          file="${org.bedework.config.options}" >
      <filterset>
        <filter token="RPI-DEBUG" value="${app.debug.val}"/>
      </filterset>
      <filterset refid="property.filters" />
    </copy>

    <!-- Temp do it again -->
    <mkdir dir="${app.dest.properties.old}" />

    <copy tofile="${app.dest.properties.old}/options.xml"
          file="${org.bedework.config.options}" >
      <filterset>
        <filter token="RPI-DEBUG" value="${app.debug.val}"/>
      </filterset>
      <filterset refid="property.filters" />
    </copy>

    <if>
      <available file="${app.sou.dir}/war/WEB-INF/classes" type="dir" />
      <then>
		    <copy todir="${app.dest.classes}">
		      <fileset dir="${app.sou.dir}/war/WEB-INF/classes"
		               includes="*"/>
		      <filterset refid="property.filters" />
		    </copy>
      </then>
    </if>

  	<!-- Make our own servlet.properties file for struts applications
    <copy tofile="${app.dest.classes}/servlet.properties" file="${app.sou.properties}">
      <filterset refid="property.filters" />
    </copy>
    -->

    <copy tofile="${app.dest.classes}/sysevents.properties"
          file="${org.bedework.config.sysevents.properties}" />

    <!-- ===============================================================
         Add hibernate jars and property files from calendar api.
         =============================================================== -->

    <copy todir="${app.dest.classes}">
      <fileset dir="${org.bedework.project.calendarapi}/calCore/resources/properties" />
      <filterset refid="property.filters" />
    </copy>

    <copy todir="${app.dest.classes}">
      <fileset dir="${org.bedework.project.calendarapi}/calCore/resources/hbms" />
      <filterset refid="property.filters" />
    </copy>

    <!-- Make a temp copy of the web.xml file -->
    <copy tofile="${app.temp.web.xml}" file="${app.web.xml}"
          overwrite="yes" />

    <antcall target="struts-stuff" inheritRefs="true" />

    <!-- Filter the temp copy of the web.xml file -->
    <copy tofile="${app.dest.web.xml}"
          file="${app.temp.web.xml}"
          overwrite="yes" >
      <filterset>
        <filter token="RPI-DEBUG" value="${app.debug.val}"/>
      </filterset>
      <filterset refid="property.filters" />
    </copy>

    <delete file="${app.temp.web.xml}" />

    <!-- Add the contex root to the file. Note that newline is required -->
    <echo file="${org.bedework.global.context.roots}" append="yes">
${propval.app.war.name}.context=${propval.app.context.root}
    </echo>

    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.antlr}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.asm-attrs}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.asm}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.cglib}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.dom4j}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.ehcache}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.hibernate}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.javassist}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.jgroups}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.jta}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.slf4j-api}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.slf4j-log4j12}" />

    <!-- jms support -->
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.activemq-core}" />
    <copy todir="${app.dest.lib}"
          file="${org.bedework.appjar.geronimo-j2ee-management_1.0_spec}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.jms}" />

    <antcall target="bexchange-stuff" inheritRefs="true" />
    <antcall target="domino-stuff" inheritRefs="true" />
    <antcall target="google-stuff" inheritRefs="true" />

    <!-- ===============================================================
         Any jar files required
         =============================================================== -->

    <echo message="+++++++++++  bw-logging at ${org.bedework.appjar.bw-logging}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-annotations}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.rpiaccess}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.rpiutil}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-davio}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-apiutil}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-calcore}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-calcorei}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-calfacade}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-calsvc}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-calsvci}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-client}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-icalendar}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-logging}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-mail}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-sysevents}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.bw-appcommon}" />

    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.lucene-core}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.lucene-misc}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.ical4j}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.commons-codec}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.commons-collections}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.commons-httpclient}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.commons-lang}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.commons-ssl}"/>

    <!-- Mail related stuff -->
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.activation}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.dsn}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.imap}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.mailapi}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.pop3}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.smtp}" />

    <!-- any extra files-->
    <copy todir="${app.dest.lib}" >
      <fileset dir="${org.bedework.temp.extrajars.dir}" />
    </copy>

    <!-- Platform specific -->
    <antcall target="doPlatform" inheritRefs="true" />

    <!-- build the war file -->
    <mkdir dir="${dist.home}" />
    <delete file="${app.war.file}" />

    <war warfile="${app.war.file}"
         webxml="${app.dest.web.xml}"
         basedir="${app.dest.war}"
         excludes="WEB-INF/web.xml" />
  </target>

  <target name="bexchange-stuff" if="org.bedework.build.caldav.bexchange">
  </target>

  <target name="domino-stuff" if="org.bedework.build.caldav.domino">
  </target>

  <target name="google-stuff" if="org.bedework.build.caldav.google">
    <copy todir="${app.dest.lib}">
      <fileset dir="${google.dir}">
        <include name="*.jar"/>
      </fileset>
    </copy>
  </target>

  <!-- =================================================================
       If this is a struts app the following gets executed.
       We create an updated struts-config.xml file and add some jars.
       ================================================================= -->

  <target name="struts-stuff" if="org.bedework.strutsapp">
  	<!-- Make our own servlet.properties file for struts applications -->
    <echo file="${app.dest.classes}/servlet.properties" ><![CDATA[#
# ========================================================================
# This file contains all properties for servlet applications.
# It cannot be application specific as the jboss build creates a
# single set of resources on the classpath.
# ========================================================================
#

# ========================================================================
# Properties for the xml/xslt based calendar web applications
# ========================================================================
#
# author Mike Douglass douglm rpi.edu

# This is only here because the jetspeed portal-struts bridge does not seem to be
# passing across the servlet context.
#org.bedework.svcicb.sessionAttrName=org.bedework.admin.svcicb.sessionattrname.@APP-NAME@

# These are required by the Struts framework
errors.header=<errors><ul>
errors.footer=</ul></errors>

messages.header=<messages><ul>
messages.footer=</ul></messages>

edu.rpi.sss.util.action.noactionerrors=yes

applicationResources=<li>Cannot load application resources bundle {0}</li>

# set the default content type - html for jsp, xml for xml/xslt.
#edu.rpi.sss.util.action.contenttype=@DEFAULT-CONTENTTYPE@
edu.rpi.sss.util.action.contenttype=text/xml

edu.rpi.sss.util.error.exc=<li>Exception: {0}</li>

# Turn nocache headers on. We don't use the standard struts directives
# because we need to be able to turn nocache off for a single response.
# This gets around an IE problem with delivery of files.
edu.rpi.sss.util.action.nocache=yes

org.bedework.security.prefix=@SECURITY-PREFIX@

edu.rpi.sss.util.action.logprefix=@APP-NAME@

# Roles we need to know about - init action uses these
# (Not using roles in the current system)
#org.bedework.role.admin=@SECURITY-PREFIX@.admin
#org.bedework.role.contentadmin=@SECURITY-PREFIX@.contentadmin
#org.bedework.role.alert=@SECURITY-PREFIX@.alerts
#org.bedework.role.owner=@SECURITY-PREFIX@.owner
]]></echo>

    <!-- Replace the tokens -->
    <replace file="${app.dest.classes}/servlet.properties">
      <replacefilter token="@SECURITY-PREFIX@"
                     value="${propval.app.security.prefix}" />
      <replacefilter token="@APP-NAME@"
                     value="${propval.app.name}" />
      <replacefilter token="@DEFAULT-CONTENTTYPE@"
                     value="${propval.app.default.contenttype}" />
    </replace>
    	
    <!-- Copy a dummy startup file into the war. -->
    <copy todir="${app.dest.war}">
      <fileset dir="${app.sou.dir}/war/" includes="setup.do" />
    </copy>

    <copy todir="${app.dest.webinf}"
          file="${app.sou.dir}/war/WEB-INF/struts-config.xml"/>

    <mkdir  dir="${app.dest.webinf}/tlds"/>
    <copy todir="${app.dest.webinf}/tlds" >
      <fileset dir="${org.bedework.struts.tlds}">
        <include name="*"/>
      </fileset>
    </copy>

    <if>
      <isset property="org.bedework.global.portal.platform" />
      <then>
        <antcall target="doPortal" inheritRefs="true" />
      </then>
    </if>

    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.serializer}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.xalan}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.struts}"/>

    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.antlr}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.commons-beanutils}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.commons-digester}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.commons-fileupload}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.commons-logging}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.commons-validator}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.jakarta-oro}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.jstl}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.jstl-standard}"/>
  </target>
</project>
