<?xml version="1.0"?>

<!-- ===================== Deployment build file =======================
     This directory and the files within it are to be part of the config
     and deployment process.

     A number of properties of the form org.bedework.install.XXX will be
     defined, each indicating that a particualr 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="base.name" value="deployment"/>

  <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" />

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

  <dirname property="this.build.dir" file="${ant.file}"/>
  <property name="bedework.deployment.base" location="this.build.dir" />

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

  <import file="${this.build.dir}/appjars.xml" />

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

  <target name="init" >
    <getAppJars />

    <property name="source.home" location="${this.build.dir}/src"/>

    <loadDeployConfig />

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

    <!-- set this for the war builds -->
    <dirname property="app.root.dir" file="${ant.file}"/>

    <!-- Common resources directory -->
    <property name="org.bedework.common.resources"
              location="${app.root.dir}/resources" />

    <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="deftasks" depends="init">
    <path id="app.xml.cp">
      <pathelement location="${org.bedework.deployutil.jar}"/>
    </path>

    <taskdef name="forEachApp"
             classname="org.bedework.deployment.ForEachAppTask">
      <classpath refid="app.xml.cp"/>
    </taskdef>

    <taskdef name="resolveFile"
             classname="org.bedework.deployment.ResolveFile">
      <classpath refid="app.xml.cp"/>
    </taskdef>

    <taskdef name="makeLangXsl"
             classname="org.bedework.deployment.MakeLangXsl">
      <classpath refid="app.xml.cp"/>
    </taskdef>
  </target>

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

    <property name="org.bedework.lang.properties"
              location="${org.bedework.project.calendarapi}/calFacade/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>

    <!-- copy startup website that sends you to the applications -->
    <delete dir="${webapp.root.dir}/bedework"/>
    <mkdir dir="${webapp.root.dir}/bedework"/>
    <copy todir="${webapp.root.dir}/bedework">
      <fileset dir="${app.root.dir}/resources/bedework"/>
    </copy>

    <!-- copy jdbc jar so datasources work -->
    <if>
      <available file="${org.bedework.global.ext.lib}/jdbc" type="dir" />
      <then>
        <!-- jdbc drivers -->
        <copy todir="${appserver.lib.dir}" flatten="yes" >
          <!-- local jdbc drivers -->
          <fileset dir="${org.bedework.global.ext.lib}/jdbc">
            <include name="*.jar"/>
          </fileset>
        </copy>
      </then>
      <else>
        <!-- Include the quickstart jdbc driver -->
        <copy todir="${appserver.lib.dir}" file="${org.bedework.appjar.hsqldb}"/>
      </else>
    </if>

    <if>
      <available file="${java.tools.jar}" />
      <then>
        <copy todir="${appserver.lib.dir}" file="${java.tools.jar}"/>
      </then>
    </if>
  </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 ================= -->

    <ant antfile="${org.bedework.deployment.base}/termination/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}" />
    <echo message="*********************************** deploy directory is ${org.bedework.deploy.type.dir}" />

    <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 ================= -->

    <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}" />
    <echo message="*********************************** deploy directory is ${org.bedework.deploy.type.dir}" />

    <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>

