<?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.
    cal.j2ee           Defined if this is a j2ee application
    cal.j2ee.jboss     Defined if this is a jboss application
    cal.strutsapp      Defined if this is a struts application
     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" />

  <target name="init" depends="init.common,init.j2ee,init.standalone" />

  <target name="init.common">
    <!-- Load the location of the libraries -->
    <property file="${org.bedework.project.bedework}/build/libjars.properties" />

    <!-- 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" />
    <property name="app.dest.classes"
              location="${app.dest.webinf}/classes" />
    <property name="app.dest.properties"
              location="${app.dest.classes}/properties/calendar" />

    <!--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="init.standalone" if="org.bedework.global.build.standalone.app">

    <property name="app.dest.lib"
              location="${app.dest.webinf}/lib" />
  </target>

  <target name="init.j2ee" if="org.bedework.global.j2ee.platform">
    <property name="app.dest.lib"
              location="${org.bedework.temp.dir}/earlib" />
  </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.
         =============================================================== -->

    <available file="${app.sou.dir}/properties" type="dir"
               property="org.bedework.properties.dir.exists" />

    <available file="${app.sou.properties}" type="file"
               property="org.bedework.servlet.properties.exists" />

    <available file="${app.sou.dir}/war/docs" type="dir"
               property="org.bedework.war.docs.exists" />

    <antcall target="copy.properties" inheritRefs="true" />
    <antcall target="copy.servlet.properties" inheritRefs="true" />
    <antcall target="copy.war.docs" inheritRefs="true" />

    <!-- Create the env.properties file -->
    <mkdir dir="${app.dest.properties}" />

    <copy tofile="${app.dest.properties}/env.properties"
          file="${org.bedework.config.properties}" />

    <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>

    <!-- 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>

    <!-- ===============================================================
         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>

    <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.jgroups}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.jta}" />

    <antcall target="jboss-stuff" inheritRefs="true" />
    <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.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-calenv}" />
    <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-appcommon}" />

     <!-- any extra bin files-->
    <copy todir="${app.dest.lib}" >
      <fileset refid="app.bin.files" />
    </copy>

    <!-- Some more standard libs -->
    <if>
      <not>
        <isset property="org.bedework.build.for.jboss"/>
      </not>
      <then>
        <copy todir="${app.dest.lib}" file="${org.bedework.appjar.log4j}"/>
      </then>
    </if>
    <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}" />

    <!-- add the jdbcdriver if defined
         Disable this - shouldn't be adding jdbc drivers to the war file
    <if>
      <isset property="app.jdbcdriver.jar"/>
      <then>
        <copy todir="${app.dest.lib}" file="${app.jdbcdriver.jar}"/>
      </then>
    </if>
     -->

    <!-- tomcat -->
    <antcall target="add.tomcat" 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="add.tomcat" if="org.bedework.global.deploy.on.tomcat">
    <property name="propval.app.tomcat.context.xml"
              location="${app.sou.dir}/war/META-INF/context.xml" />
    <resolveFile name="app.tomcat.context.xml"
                 file="${propval.app.tomcat.context.xml}"
                 base="${app.sou.dir}"/>

    <copy tofile="${app.dest.metainf}/context.xml"
          file="${app.tomcat.context.xml}"
          overwrite="yes" >
      <filterset refid="property.filters" />
    </copy>
  </target>

  <target name="copy.properties" if="org.bedework.properties.dir.exists">
    <!-- ===============================================================
         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.
         =============================================================== -->
    <copy todir="${app.dest.properties}" overwrite="yes" >
      <fileset dir="${app.sou.dir}/properties"
               includes="*.properties"/>
      <filterset refid="property.filters" />
    </copy>
  </target>

  <target name="copy.servlet.properties">
    <copy todir="${app.dest.classes}">
      <fileset dir="${app.sou.dir}/war/WEB-INF/classes"
               includes="*"/>
      <filterset refid="property.filters" />
    </copy>
  </target>

  <target name="copy.war.docs" if="org.bedework.war.docs.exists">
    <copy todir="${app.dest.docs}">
      <fileset dir="${app.sou.dir}/war/docs" />
    </copy>
  </target>

  <target name="bexchange-stuff" if="org.bedework.build.caldav.bexchange">
    <copy tofile="${app.dest.properties}/timezones.xml"
          file="${org.bedework.project.bedework}/docs/bwtimezones.xml" />
  </target>

  <target name="domino-stuff" if="org.bedework.build.caldav.domino">
    <copy tofile="${app.dest.properties}/timezones.xml"
          file="${org.bedework.project.bedework}/docs/bwtimezones.xml" />
  </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>

    <copy tofile="${app.dest.properties}/timezones.xml"
          file="${org.bedework.project.bedework}/docs/bwtimezones.xml" />
  </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">
    <!-- 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>
      <not>
        <isset property="xalan.in.tomcat.common.endorsed" />
      </not>
      <then>
        <copy todir="${app.dest.lib}" file="${xalan.jar}"/>
      </then>
    </if>

    <if>
      <isset property="org.bedework.global.portal.platform" />
      <then>
        <!-- Add common portlet files -->

        <copy todir="${app.dest.lib}"
              file="${org.bedework.appjar.portals-bridges-common}"/>
        <copy todir="${app.dest.lib}"
              file="${org.bedework.appjar.portals-bridges-struts-1.2.7}"/>

        <!-- Use generic bedework portlet tld -->
        <copy todir="${app.dest.webinf}/tlds"
              overwrite="yes"
              failonerror="no" >
          <fileset dir="${app.root.dir}/resources/portlet" >
            <include name="*.tld"/>
          </fileset>
        </copy>

        <copy todir="${app.dest.webinf}"
              file="${org.bedework.deploy.type.dir}/portlet/struts-portlet-config.xml"
              overwrite="yes"
              failonerror="no" />

        <copy todir="${app.dest.webinf}"
              file="${org.bedework.deploy.type.dir}/portlet/portlet.xml"
              overwrite="yes"
              failonerror="no" >
          <filterset refid="property.filters" />
        </copy>

        <!-- Add the portal bridge controller def -->
        <replace file="${app.dest.webinf}/struts-config.xml">
          <replacetoken><![CDATA[<!-- portlet controller def here -->]]></replacetoken>
          <replacevalue><![CDATA[<controller pagePattern="$M$P" inputForward="true"
                  processorClass="org.apache.portals.bridges.struts.PortletRequestProcessor"/>]]>
          </replacevalue>
        </replace>

        <antcall target="add.jetspeed2" inheritRefs="true" />
        <antcall target="add.uportal2" inheritRefs="true" />
        <antcall target="add.liferay4" inheritRefs="true" />
      </then>
    </if>

    <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}"/>
  </target>

  <!-- =================================================================
       Jetspeed 2 modifications
       ================================================================= -->

  <target name="add.jetspeed2" if="org.bedework.build.for.jetspeed2" >
    <copy todir="${app.dest.webinf}/tlds" >
      <fileset dir="${org.bedework.default.lib}/${jetspeed2.jars.dir}/portals-bridges-struts">
        <include name="*.tld"/>
      </fileset>
    </copy>
  </target>

  <!-- =================================================================
       Uportal 2 modifications
       ================================================================= -->

  <target name="add.uportal2" if="org.bedework.build.for.uportal2" >
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.uportal}"/>

    <!-- Add the portlet servlet mapping -->
    <replace file="${app.temp.web.xml}">
      <replacetoken><![CDATA[<!-- portlet servlet mapping here -->]]></replacetoken>
      <replacevalue><![CDATA[<servlet-mapping>
    <servlet-name>@PORTLET-NAME@</servlet-name>
    <url-pattern>/@PORTLET-NAME@/*</url-pattern>
  </servlet-mapping>]]>
      </replacevalue>
    </replace>

  </target>

  <!-- =================================================================
       Liferay 4 modifications
       ================================================================= -->

  <target name="add.liferay4" if="org.bedework.build.for.liferay4" >
   <!-- Add liferay config files -->
    <copy todir="${app.dest.webinf}" >
      <fileset dir="${app.sou.dir}/war/WEB-INF" >
        <include name="liferay*"/>
      </fileset>
      <filterset refid="property.filters" />
    </copy>

    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.liferay}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.liferay.util-bridges}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.liferay.util-java}"/>

    <!-- Add the context params -->
    <replace file="${app.temp.web.xml}">
      <replacetoken><![CDATA[<!-- add context params here -->]]></replacetoken>
      <replacevalue><![CDATA[<!-- add context params here -->

  <context-param>
    <param-name>company_id</param-name>
    <param-value>@LIFERAY-COMPANY-ID@</param-value>
  </context-param>]]>
      </replacevalue>
    </replace>

    <!-- Add the portal listener -->
    <replace file="${app.temp.web.xml}">
      <replacetoken><![CDATA[<!-- add listeners here -->]]></replacetoken>
      <replacevalue><![CDATA[<!-- add listeners here -->

  <listener>
    <listener-class>
      @PORTAL-SERVLET-CONTEXT-LISTENER@
    </listener-class>
  </listener>]]>
      </replacevalue>
    </replace>

    <!-- Add the portlet servlet definition -->
    <replace file="${app.temp.web.xml}">
      <replacetoken><![CDATA[<!-- add servlet definitions here -->]]></replacetoken>
      <replacevalue><![CDATA[<!-- add servlet definitions here -->

  <!-- Servlet definition for portlet use. -->
  <servlet>
    <servlet-name>@PORTLET-NAME@</servlet-name>
    <display-name>@PORTLET-NAME@ Wrapper</display-name>
    <servlet-class>@PORTLET-SERVLET-CLASS@</servlet-class>
    <init-param>
      <param-name>portlet-class</param-name>
      <param-value>@PORTLET-CLASS@</param-value>
    </init-param>
    <init-param>
      <param-name>portlet-guid</param-name>
      <param-value>@CONTEXT-ROOT@.@PORTLET-NAME@</param-value>
    </init-param>
  </servlet>]]>
      </replacevalue>
    </replace>

    <!-- Add the portlet servlet mapping -->
    <replace file="${app.temp.web.xml}">
      <replacetoken><![CDATA[<!-- add servlet mappings here -->]]></replacetoken>
      <replacevalue><![CDATA[<!-- add servlet mappings here -->

  <servlet-mapping>
    <servlet-name>@PORTLET-NAME@</servlet-name>
    <url-pattern>/@PORTLET-NAME@/*</url-pattern>
  </servlet-mapping>]]>
      </replacevalue>
    </replace>
  </target>

  <!-- =================================================================
       If this is a j2ee app the following gets executed.
       We create an uodated jboss-web.xml file (for jboss) and a ear
       ================================================================= -->

  <target name="jboss-stuff" if="org.bedework.build.for.jboss">
    <copy todir="${app.dest.webinf}"
          file="${app.sou.dir}/war/WEB-INF/jboss-web.xml">
      <filterset refid="property.filters" />
    </copy>

    <if>
      <isset property="propval.app.virtual.host" />
      <then>
        <replace file="${app.dest.webinf}/jboss-web.xml">
          <replacetoken><![CDATA[<!-- Virtual host -->]]></replacetoken>
          <replacevalue><![CDATA[<virtual-host>@VIRTUAL-HOST@</virtual-host>
          ]]>
          </replacevalue>
        </replace>
        <replace file="${app.dest.webinf}/jboss-web.xml">
          <replacefilter token="@VIRTUAL-HOST@" value="${propval.app.virtual.host}"/>
        </replace>
      </then>
    </if>
  </target>

  <target name="build.ear" if="cal.j2ee">
    <ant antfile="${buildear}"
         inheritRefs="true" target="build" />
  </target>
</project>
