<?xml version="1.0"?>

<!-- ===================================================================
     This file builds a runnable application wrapped up as a zip file.
     Unpacking the zip should result in a directory containing a shell
     script, a set of jars and any other resources needed.

     Properties we need:
       app.core.env.pname     core properties source
       app.run.shellscr       location of the skeleton shell script.
       app.run.jar.file       application jar file
       app.run.main.class     main class for application.

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

<project name="uwcal.buildsh" default="build">
  <import file="${build.dir}/buildfilters.xml" />

  <target name="init">
    <!-- Load the location of the libraries -->
    <property file="${org.bedework.project.bedework}/build/libjars.properties" />

    <!-- Destinations - where we build stuff -->
    <property name="app.dest.home"
              location="${org.bedework.temp.shellscr.home}/${propval.app.zip.name}" />
    <property name="app.dest.lib"
              location="${app.dest.home}/lib" />
    <property name="app.dest.classes"
              location="${app.dest.home}/classes" />
    <property name="app.dest.properties"
              location="${app.dest.classes}/properties/calendar" />
    <property name="app.dest.resources"
              location="${app.dest.home}/resources" />
    <property name="app.dest.data"
              location="${app.dest.home}/data" />

    <property name="app.zip.file"
              location="${dist.home}/${propval.app.zip.name}.zip" />
  </target>

  <!-- ================================================================
       build target
       ================================================================ -->

  <target name="build" depends="init">
    <delete dir="${app.dest.home}" />

    <!-- Library stuff -->

    <mkdir dir="${app.dest.lib}" />

    <copy todir="${app.dest.lib}">
      <fileset dir="${hibernate.jars.dir}">
        <include name="*.jar"/>
      </fileset>
    </copy>

    <copy todir="${app.dest.lib}" file="${commons-collections.jar}"/>
    <copy todir="${app.dest.lib}" file="${commons-logging.jar}"/>

    <!-- core files -->

    <copy todir="${app.dest.lib}" overwrite="yes">
      <fileset dir="${bin.home}">
        <!-- jars from sub-projects needed by all -->
        <include name="rpiaccess*.jar"/>
        <include name="rpiutil*.jar"/>
        <include name="bw-davio*.jar"/>

        <!-- Calendar api jars -->
        <include name="bw-apiutil*.jar"/>
        <include name="bw-calcore*.jar"/>
        <include name="bw-calcorei*.jar"/>
        <include name="bw-calenv*.jar"/>
        <include name="bw-calfacade*.jar"/>
        <include name="bw-calsvc*.jar"/>
        <include name="bw-calsvci*.jar"/>
        <include name="bw-icalendar*.jar"/>
        <include name="bw-locale*.jar"/>
        <include name="bw-logging*.jar"/>
        <include name="bw-mail*.jar"/>
      </fileset>
    </copy>

     <!-- any extra bin files-->
    <copy todir="${app.dest.lib}" >
      <fileset refid="app.bin.files" />
    </copy>

    <!-- Some more standard libs -->
    <copy todir="${app.dest.lib}" file="${log4j.jar}"/>
    <copy todir="${app.dest.lib}" file="${lucene.core.jar}"/>
    <copy todir="${app.dest.lib}" file="${lucene.misc.jar}"/>
    <copy todir="${app.dest.lib}" file="${ical4j.jar}"/>
    <copy todir="${app.dest.lib}" file="${commons-digester.jar}"/>
    <copy todir="${app.dest.lib}" file="${commons-httpclient.jar}"/>
    <copy todir="${app.dest.lib}" file="${commons-ssl.jar}"/>
    <copy todir="${app.dest.lib}" file="${commons-codec.jar}"/>
    <copy todir="${app.dest.lib}" file="${commons-lang.jar}"/>

    <!-- Extra libraries specified in calling build file -->
    <copy todir="${app.dest.lib}" flatten="yes" >
      <fileset refid="org.bedework.run.jars" />
      <fileset refid="org.bedework.run.lib.jars" />

      <!-- local jdbc drivers -->
      <fileset dir="${org.bedework.jdbc.lib}">
        <include name="*.jar"/>
      </fileset>
    </copy>

    <!-- Include the quickstart jdbc driver -->
    <copy todir="${app.dest.lib}" file="${hsqldb.jdbcdriver.jar}"/>

    <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.classes}" overwrite="yes" >
      <fileset refid="org.bedework.extra.resources" />
      <filterset refid="property.filters" />
    </copy>

    <!-- add the jdbcdriver if defined -->
    <antcall target="add.jdbcdriver" 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}" />

    <!-- ===============================================================
                       Add any resource files
         =============================================================== -->

    <copy tofile="${app.dest.resources}/log4j.xml"
          file="${org.bedework.runsh.log4j.xml}"
          failonerror="false" />

    <!-- ===============================================================
                       Add any data files
         =============================================================== -->

    <mkdir dir="${app.dest.data}" />

    <copy todir="${app.dest.data}" overwrite="yes" >
      <fileset refid="org.bedework.shellscr.data" />
      <filterset refid="property.filters" />
    </copy>

    <!-- ===============================================================
                       Build the classpath
         =============================================================== -->

    <path id="app.run.path">
      <fileset dir="${app.dest.lib}">
        <include name="*.jar"/>
      </fileset>
      <pathelement location="${app.dest.resources}" />
    </path>
    <pathconvert property="app.run.cp" refid="app.run.path"
                 targetos="unix" >
      <map from="${app.dest.lib}" to="./lib"/>
      <map from="${app.dest.resources}" to="./resources"/>
    </pathconvert>
    <pathconvert property="app.run.wincp" refid="app.run.path"
                 targetos="windows" >
      <map from="${app.dest.lib}" to="./lib"/>
      <map from="${app.dest.resources}" to="./resources"/>
    </pathconvert>

    <!-- ===============================================================
                       Copy and modify the shell script
         =============================================================== -->

    <copy tofile="${app.dest.home}/${propval.app.shellscr.name}"
          file="${app.run.shellscr}" >
      <filterset>
        <filter token="CP"
                value=".:./classes:${app.run.cp}"/>
      </filterset>
      <filterset refid="property.filters" />
    </copy>

    <copy tofile="${app.dest.home}/${propval.app.batscr.name}"
          file="${app.run.batscr}" >
      <filterset>
        <filter token="CP"
                value=".;./classes;${app.run.wincp}"/>
      </filterset>
      <filterset refid="property.filters" />
    </copy>

    <!-- build the zip file -->
    <mkdir dir="${dist.home}" />
    <delete file="${app.zip.file}" />

    <zip destfile="${app.zip.file}" >
      <zipfileset prefix="${propval.app.zip.name}" dir="${app.dest.home}" />
    </zip>
  </target>

  <target name="add.jdbcdriver" if="propval.app.jdbcdriver.jar">
    <copy todir="${app.dest.lib}" file="${propval.app.jdbcdriver.jar}"/>
  </target>
</project>

