<?xml version="1.0"?>

<!-- ===================== Deployment build file =======================
     This directory and the files within it are to be part of the config
     and deployment process.
     
     ======================== new comment ========================
     Modules are of type and kind.
     
     The type is for example, webadmin, usercaldav etc.
     
     The kind is webapp, shellscr etc.
     
     We cycle over the information we have for each application we want to 
     build and for each application use ants property mapping to map the 
     properties onto a constant set of names, e.g. the applicfation specific 
     name 
         org.bedework.app.CalAdmin.deploy.dir
     would be mapped onto 
         propval.app.deploy.dir
         
     We then look for build.xml files on a path given by the type and kind, 
     for example 
         webadmin/webapp/build.xml
     or
         indexer/shellscr/build.xml
         
     The intent was to allow certain modules to be built in different forms.
     At one point it was also the case that we tried to deal with platform
     specific issues through this scheme. Now that is handled by importing
     platform specific build files into a generic build file.
     
     That may allow for some simplification. 
     ======================== old comment ========================

     A number of properties of the form org.bedework.install.XXX will be
     defined, each indicating that a particular component is to be deployed.

     Targets below will be executed only if that property is defined.

     Adding a new configuration, e.g. a different portal platform, requires
     the steps:
        Create a subdirectory in each components subdirectory,
        e.g.  webadmin/myportal

        Create any other required resource directories and resources and
        create a build.xml file to configure and package the component(s).

        Update the config program to load, save and display the new
        properties for that configuration.

        Add an antcall for each component and a corresponding target

     Authors: Mike Douglass   douglm rpi.edu
     =================================================================== -->

<project name="deploy.bedework" default="init">
  <property name="org.bedework.temp.wars.home"
            location="${org.bedework.temp.dir}/wars" />

  <property name="org.bedework.temp.shellscr.home"
            location="${org.bedework.temp.dir}/shellscr" />

  <!-- Recreated by each app build file. Place extra jars here -->
  <property name="org.bedework.temp.extrajars.dir"
            location="${org.bedework.temp.dir}/extrajars" />

  <!-- Preserve extra jars for ear builds -->
  <property name="org.bedework.temp.ear.extrajars.dir"
            location="${org.bedework.temp.dir}/earextrajars" />
	
  <dirname property="this.build.dir" file="${ant.file}"/>
  <property name="bedework.deployment.base" location="${this.build.dir}" />
  <property name="org.bedework.deployment.resources" 
            location="${bedework.deployment.base}/resources" />

  <import file="${build.dir}/buildTools/getJar.xml"/>

  <import file="${build.dir}/loadDeployConfig.xml"/>
  
  <property name="ear.meta.dir" location="${this.build.dir}/ear.meta"/>

  <target name="init" >
    <property name="org.bedework.struts.tlds"
              location="${resources.dir}/struts-1.2.9/tlds"/>

    <loadDeployConfig />

    <!-- All the projects we deploy -->
    <property name="org.bedework.project.bwtzsvr"
              location="${org.bedework.project.bedework}/../bwtzsvr" />
    <property name="org.bedework.project.bwwebapps"
              location="${org.bedework.project.bedework}/../bwwebapps" />
    <property name="org.bedework.project.bwcaldav"
              location="${org.bedework.project.bedework}/../bwcaldav" />
    <property name="org.bedework.project.caldavTest"
              location="${org.bedework.project.bedework}/../caldavTest" />
    <property name="org.bedework.project.testsuite"
              location="${org.bedework.project.bedework}/../testsuite" />
    <property name="org.bedework.project.bwtools"
              location="${org.bedework.project.bedework}/../bwtools" />
    <property name="org.bedework.project.dumprestore"
              location="${org.bedework.project.bedework}/../dumprestore" />
    <property name="org.bedework.project.indexer"
              location="${org.bedework.project.bedework}/../indexer" />
    <property name="org.bedework.project.bwsysevents"
              location="${org.bedework.project.bedework}/../bwsysevents" />
    <property name="org.bedework.project.bwcalFacade"
              location="${org.bedework.project.bedework}/../bwcalFacade" />
    <property name="org.bedework.project.bwcalcore"
              location="${org.bedework.project.bedework}/../bwcalcore" />
    <property name="org.bedework.project.carddav"
              location="${org.bedework.project.bedework}/../carddav" />
    <property name="org.bedework.project.synch"
              location="${org.bedework.project.bedework}/../synch" />


    <!-- default to debug off -->
    <property name="app.debug.val" value="0" />

    <!-- set these for the war builds -->
    <dirname property="app.root.dir" file="${ant.file}"/>
    
    <property name="org.bedework.deployment.name"
              value="bedework" />
    
    <property name="org.bedework.sou.hibernate.properties"
              location="${org.bedework.deployment.resources}/hibernate" />
    
    <property name="org.bedework.jboss.datasource.jndiname"
              value="${org.bedework.global.jboss.db.datasource.jndiname}" />
    
    <!-- Common resources directory -->
    <property name="org.bedework.common.resources"
              location="${app.root.dir}/resources" />

    <property name="org.bedework.global.build.common.context.war.name"
              value="bw-common" />

    <!-- Where we put ear stuff -->
    <if>
      <isset property="org.bedework.global.build.ear" />
      <then>
        <property name="app.ear.file.name"
                  value="${org.bedework.global.ear.name}.ear" />

        <property name="org.bedework.ear.name"
                  value="${org.bedework.global.ear.name}" />

        <property name="org.bedework.ear.properties.dir"
                  location="${org.bedework.temp.dir}/ear-properties" />

        <property name="org.bedework.ear.templib"
                  location="${org.bedework.temp.dir}/earlib"/>
        
        <delete dir="${org.bedework.ear.templib}" />
        <mkdir dir="${org.bedework.ear.templib}" />

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

    <property name="org.bedework.global.context.roots"
              location="${org.bedework.temp.dir}/context-roots.properties" />

    <if>
      <not>
        <isset property="org.bedework.global.context.roots" />
      </not>
      <then>
        <tempfile property="org.bedework.global.context.roots"
                  destdir="${org.bedework.temp.dir}"
                  prefix="context-roots" suffix=".properties" />
      </then>
    </if>
  </target>

  <target name="build.configured" depends="init" >
    <!-- Clean up before we start -->
    <delete dir="${org.bedework.temp.wars.home}" />

    <antcall target="build.apps" inheritrefs="true" />
  </target>

  <!-- =================================================================
       Targets called by the build.configured target. Each is conditional on a
       config property and each executes a build file in a subdirectory of
       this component.
       ================================================================= -->

  <target name="build.apps" >
    <property name="org.bedework.deploy.kind" value="webapp" />
    <antcall target="build.configured.apps" inheritrefs="true" />
  </target>

  <!-- =================================================================
                         The deploy target.
       ================================================================= -->

  <target name="deploy" depends="init" >
    <!-- Clean up before we start -->
    <delete dir="${org.bedework.temp.wars.home}" />

    <property name="org.bedework.lang.properties"
              location="${org.bedework.deployment.resources}/org/bedework/locale/resources" />

    <property name="org.bedework.xsllang.dir"
              location="${org.bedework.temp.dir}/xsllang" />

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

    <makeLangXsl dir="${org.bedework.lang.properties}"
                 prefix="BwResources"
                 resdir="${org.bedework.xsllang.dir}"
                 xsldir="${org.bedework.xsllang.dir}"
                 name="org.bedework.locales"
                 check="true" />

    <!-- Build the webapps stuff.-->
    <antcall target="deploy.apps" inheritrefs="true" >
      <param name="org.bedework.deploy.kind" value="webapp" />
    </antcall>

    <!-- Build the shellscr stuff.-->
    <antcall target="deploy.apps" inheritrefs="true" >
      <param name="org.bedework.deploy.kind" value="shellscr" />
    </antcall>

    <!-- ================= termination tasks for deployment ================ -->

    <ant antfile="${org.bedework.deployment.base}/termination/build.xml"
         inheritrefs="true" target="deploy" />
  </target>

  <!-- =================================================================
       Targets called by the deploy target. Each is conditional on a
       config property and each executes a build file in a subdirectory of
       this component.
       ================================================================= -->

  <!-- The cp.xalan task conditionally copies the xalan.jar file, if
       necessary -->
  <target name="cp.xalan" if="xalan.in.tomcat.common.endorsed">
    <copy file="${org.bedework.default.lib}/xalan.jar"
          todir="${appserver.endorsed.dir}"/>
  </target>

  <!-- =================================================================
       Targets called by the build.configured target. Each is conditional on a
       config property and each executes a build file in a subdirectory of
       this component.
       ================================================================= -->
  <target name="build.configured.apps">
    <forEachApp names="${org.bedework.install.app.names}"
                prefix="org.bedework.deploy"
                appPrefix="org.bedework.app"
                projectPrefix="org.bedework.project">
      <sequential>
        <antcall target="build.available.configured.app" inheritrefs="true" />
      </sequential>
    </forEachApp>

    <!-- ==================== termination tasks for apps ================= -->

    <ant antfile="${org.bedework.deployment.base}/termination/${org.bedework.deploy.kind}/build.xml"
         inheritrefs="true" target="build.configured" />
  </target>

  <target name="build.available.configured.app">
    <propertycopy name="propval.app.type"
                  from="bedework-options.org.bedework.app.${org.bedework.deploy.name}.appType" />
    <property name="org.bedework.deploy.type.dir"
              location="${app.root.dir}/${org.bedework.deploy.name}/${propval.app.type}" />
    <if>
      <istrue value="${org.bedework.build.debug}" />
      <then>
        <echo message="*************** deployment build directory is ${org.bedework.deploy.type.dir}" />
      </then>
    </if>

    <if>
      <available file="${org.bedework.deploy.type.dir}/${org.bedework.deploy.kind}/build.xml"/>
      <then>
        <propertyset id="deploy-app-properties">
          <propertyref prefix="org.bedework.app.${org.bedework.deploy.name}"/>
          <globmapper from="org.bedework.app.${org.bedework.deploy.name}.*" to="propval.app.*"/>
        </propertyset>

        <propertycopy name="propval.app.unauthenticated"
                      from="bedework-options.org.bedework.app.${org.bedework.deploy.name}.guestMode" />

        <ant antfile="${org.bedework.deploy.type.dir}/${org.bedework.deploy.kind}/build.xml"
             inheritrefs="true" target="build.configured" >
          <propertyset refid="deploy-app-properties" />
        </ant>
      </then>
    </if>
  </target>

  <target name="deploy.apps">
    <forEachApp names="${org.bedework.install.app.names}"
                prefix="org.bedework.deploy"
                appPrefix="org.bedework.app"
                projectPrefix="org.bedework.project">
      <sequential>
        <antcall target="deploy.available.app" inheritrefs="true" />
      </sequential>
    </forEachApp>

    <!-- =================== termination tasks for apps ================= -->

    <ant antfile="${org.bedework.deployment.base}/termination/${org.bedework.deploy.kind}/build.xml"
         inheritrefs="true" target="deploy" />
  </target>

  <target name="deploy.available.app">
    <propertycopy name="propval.app.type"
                  from="bedework-options.org.bedework.app.${org.bedework.deploy.name}.appType" />
    <property name="org.bedework.deploy.type.dir"
              location="${app.root.dir}/${propval.app.type}" />
    <if>
      <istrue value="${org.bedework.build.debug}" />
      <then>
        <echo message="*********************** deployment build directory is ${org.bedework.deploy.type.dir}" />
      </then>
    </if>

    <if>
      <available file="${org.bedework.deploy.type.dir}/${org.bedework.deploy.kind}/build.xml"/>
      <then>
        <propertyset id="deploy-app-properties">
          <propertyref prefix="org.bedework.app.${org.bedework.deploy.name}"/>
          <globmapper from="org.bedework.app.${org.bedework.deploy.name}.*" to="propval.app.*"/>
        </propertyset>

        <propertycopy name="propval.app.unauthenticated"
                      from="bedework-options.org.bedework.app.${org.bedework.deploy.name}.guestMode" />

        <ant antfile="${org.bedework.deploy.type.dir}/${org.bedework.deploy.kind}/build.xml"
             inheritrefs="true" target="deploy" >
          <propertyset refid="deploy-app-properties" />
        </ant>
      </then>
    </if>
  </target>
</project>

