<?xml version="1.0"?>

<!-- ===================================================================
     This file is imported by buildwar.xml and adds the targets
     which will be invoked by the buildwar targets.
        addLibs: add libraries for this platform
        doPlatform - misc stuff
        deployWar: deploy the war to the server
        deployEar: deploy the ear to the server 
     =================================================================== -->

<project name="bedework.platformWar" default="doPlatform" >
  <target name="doPlatform" >
    <!-- ===============================================================
         We create an updated jboss-web.xml file (for jboss) and a ear
         =============================================================== -->

    <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>
  
  <!-- ================================================================
       Add libraries
       ================================================================ -->

  <target name="addLibs">
    <if>
      <not>
        <isset property="org.bedework.global.build.ear" />
      </not>
      <then>
        <!-- Library in war file -->
        <property name="app.dest.lib"
                  location="${app.dest.webinf}/lib" />
        <if>
          <not>
            <isset property="propval.app.no.defaultlibs" />
          </not>
          <then>
            <antcall target="copyLibs" inheritRefs="true" />
          </then>
        </if>
      </then>
      <else>
        <!-- Preserve extra jars for later -->
        <copy todir="${org.bedework.temp.ear.extrajars.dir}" >
          <fileset dir="${org.bedework.temp.extrajars.dir}" />
        </copy>
      </else>
    </if>
  </target>
  
  <!-- ================================================================
       Deploy war
       ================================================================ -->

  <target name="deployWar">
    <if>
      <not>
        <isset property="org.bedework.global.build.ear" />
      </not>
      <then>
        <infoMsg message="* =========== Deploying standalone app ${app.war.file} into ${org.bedework.appserver.dir}/${propval.app.deploy.dir}" />

        <!-- copy the war file. -->
        <copy todir="${org.bedework.appserver.dir}/${propval.app.deploy.dir}" file="${app.war.file}"
              overwrite="yes" />

        <!-- Delete expanded version -->
        <delete dir="${org.bedework.appserver.dir}/${propval.app.deploy.dir}/${propval.app.war.name}" />
      </then>
    </if>
  </target>
    
  <!-- ================================================================
       Deploy ear
       ================================================================ -->

  <target name="deployEar">
    <if>
      <equals arg1="${org.bedework.global.ear.zipped}"
              arg2="yes" />
      <then>
        <property name="org.bedework.ear.dir"
                  location="${org.bedework.temp.dir}/${org.bedework.ear.name}" />
        <property name="org.bedework.ear.file"
                  location="${org.bedework.temp.dir}/${org.bedework.ear.name}.ear" />
      </then>
      <else>
        <property name="org.bedework.ear.dir"
                  location="${org.bedework.temp.dir}/${org.bedework.ear.name}.ear" />
      </else>
    </if>

    <delete dir="${org.bedework.ear.dir}" />
    <mkdir dir="${org.bedework.ear.dir}" />

    <property name="org.bedework.ear.properties.jar"
              value="${org.bedework.ear.dir}/bw-ear-properties.jar"/>

    <!-- Common library in ear file -->
    <property name="app.dest.lib"
              location="${org.bedework.ear.dir}" />
    <if>
      <not>
        <isset property="propval.app.no.defaultlibs" />
      </not>
      <then>
        <antcall target="copyLibs" inheritRefs="true" />
      </then>
    </if>
    
    <!-- Add any extra jars -->
    <copy todir="${org.bedework.temp.ear.extrajars.dir}" >
      <fileset dir="${org.bedework.temp.extrajars.dir}" />
    </copy>

    <!-- And the extra jars we preserved -->
    <copy todir="${app.dest.lib}" >
      <fileset dir="${org.bedework.temp.ear.extrajars.dir}" />
    </copy>
  	
    <infoMsg message="* =========== Building ear file ${org.bedework.ear.dir} for jboss deployment" />

    <jar jarfile="${org.bedework.ear.properties.jar}">
      <fileset dir="${org.bedework.ear.properties.dir}"/>
    </jar>

    <debugMsg message="outFile=${org.bedework.ear.dir}/META-INF/application.xml
                        warDir=${org.bedework.temp.dir}/wars
                        contexts=${org.bedework.global.context.roots}" />

  	<mkdir dir="${org.bedework.ear.dir}/META-INF" />
  	
    <applicationXml displayName="Bedework calendar suite"
                    outFile="${org.bedework.ear.dir}/META-INF/application.xml"
                    warDir="${org.bedework.temp.dir}/wars"
                    contexts="${org.bedework.global.context.roots}">
      <fileset dir="${org.bedework.ear.dir}">
        <include name="*.jar"/>
      </fileset>
    </applicationXml>
    
    <echo file="${org.bedework.ear.dir}/META-INF/jboss-app.xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<jboss-app>
   <loader-repository> 
     org.bedework:archive=@EAR_NAME@ 
     <loader-repository-config>java2ParentDelegation=false</loader-repository-config> 
   </loader-repository> 
</jboss-app>
]]></echo>
      
    <replace file="${org.bedework.ear.dir}/META-INF/jboss-app.xml"
             token="@EAR_NAME@" 
             value="${app.ear.file.name}"/>

    <if>
      <and>
        <isset property="ear.meta.dir" />
        <available file="${ear.meta.dir}" type="dir" />
      </and>
      <then>
        <copy todir="${org.bedework.ear.dir}/META-INF">
          <fileset dir="${ear.meta.dir}" />
          <filterset refid="property.filters" />
        </copy>
      </then>
    </if>
          
    <manifest file="${org.bedework.ear.dir}/META-INF/MANIFEST.MF" />

    <if>
      <equals arg1="${org.bedework.global.ear.wars.zipped}"
              arg2="yes" />
      <then>
        <copy toDir="${org.bedework.ear.dir}">
          <fileset dir="${dist.home}"
                   includes="*.war" />
        </copy>
      </then>
      <else>
        <copy toDir="${org.bedework.ear.dir}">
          <fileset dir="${org.bedework.temp.wars.home}"/>
        </copy>
      </else>
    </if>

    <if>
      <equals arg1="${org.bedework.global.ear.zipped}"
              arg2="yes" />
      <then>
         <zip destfile="${org.bedework.ear.file}"
             basedir="${org.bedework.ear.dir}"/>
      </then>
    </if>

    <!-- copy the ear - at the moment copy the uncompressed. -->

    <if>
      <isset property="org.bedework.appserver.deploy.dir" />
      <then>
        <delete dir="${org.bedework.appserver.deploy.dir}/${app.ear.file.name}" />
        <mkdir dir="${org.bedework.appserver.deploy.dir}/${app.ear.file.name}" />
        <copy todir="${org.bedework.appserver.deploy.dir}/${app.ear.file.name}">
          <fileset dir="${org.bedework.ear.dir}"/>
        </copy>
      </then>
    </if>
  </target>
  
  <!-- ================================================================
       Private target to copy libraries
       ================================================================ -->

  <target name="copyLibs">
    <!-- hibernate jars -->
    <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.jta}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.slf4j-api}" />
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.slf4j-log4j12}" />

    <if>
      <isset property="org.bedework.build.caldav.google" />
      <then>
        <copy todir="${app.dest.lib}">
          <fileset dir="${google.dir}">
            <include name="*.jar"/>
          </fileset>
        </copy>
      </then>
    </if>

    <!-- ===============================================================
         Any jar files required
         =============================================================== -->

    <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.backport-util-concurrent}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.ical4j}"/>
    <copy todir="${app.dest.lib}" file="${org.bedework.appjar.ical4j-vcard}"/>
    <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>

  	<!-- Add the struts stuff -->
    <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.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-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>