<!--
    Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com

    The software in this package is published under the terms of the CPAL v1.0
    license, a copy of which has been included with this distribution in the
    LICENSE.txt file.
-->
<project name="Mule Examples: Loan Broker ESB" default="setup">

    <property name="app.name" value="mule-example-loanbroker-esb"/>
    <property environment="env"/>

    <property name="dir.java" value="src"/>
    <property name="dir.conf" value="conf"/>
    <property name="dir.build" value="build"/>
    <property name="dir.classes" value="build/classes"/>
    <property name="dir.common.tests" value="../common-tests"/>

    <property name="url.maven.repo" value="http://www.ibiblio.org/maven2"/>
    <property name="url.maven.repo2" value="http://people.apache.org/repo/m2-incubating-repository"/>


    <target name="clean"
            description="Clean the output directory (start from scratch).">
        <delete dir="${dir.build}"/>
    </target>

    <!-- Download any additional libraries needed to run the application -->
    <target name="get.libraries">

        <!-- ActiveMQ -->            
        <get src="${url.maven.repo2}/org/apache/activemq/activemq-core/4.1.0-incubator/activemq-core-4.1.0-incubator.jar"
             dest="${dir.user.lib}/activemq-core-4.1.0-incubator.jar"
             verbose="true"
             usetimestamp="true"/>
        <get src="${url.maven.repo2}/org/apache/activemq/activeio-core/3.0.0-incubator/activeio-core-3.0.0-incubator.jar"
             dest="${dir.user.lib}/activeio-core-3.0.0-incubator.jar"
             verbose="true"
             usetimestamp="true"/>

        <!-- OpenEJB -->
        <get src="${url.maven.repo}/openejb/openejb-core/1.0/openejb-core-1.0.jar"
             dest="${dir.user.lib}/openejb-core-1.0.jar"
             verbose="true"
             usetimestamp="true"/>
        <get src="${url.maven.repo}/castor/castor/0.9.9/castor-0.9.9.jar"
             dest="${dir.user.lib}/castor-0.9.9.jar"
             verbose="true"
             usetimestamp="true"/>
    </target>

    <target name="init" depends="get.libraries">

       <ant dir="${dir.common.tests}" antfile="build.xml" />   
           
        <!-- Create the build directory if it doesn't exist -->
        <mkdir dir="${dir.classes}"/>

        <!-- Configure Mule classpath (mirrors wrapper.conf settings -->
        <path id="classpath.mule">
            <pathelement location="${dir.mule.home}/conf"/>
            <fileset dir="${dir.mule.home}/lib/user">
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="${dir.mule.home}/lib/mule">
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="${dir.mule.home}/lib/opt">
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="${dir.mule.home}/lib">
                <include name="**/*.jar"/>
            </fileset>
            <!-- Include dependent test jar-->
            <fileset dir="${dir.common.tests}/build">
                <include name="*.jar"/>
            </fileset>
        </path>

    </target>



    <target name="compile"
            depends="init"
            description="Compile the application.">
            
        <javac srcdir="${dir.java}"
               destdir="${dir.classes}"
               debug="true"
               source="1.5"
               target="1.5">
            <classpath refid="classpath.mule"/>
        </javac>
        
        <!-- Copy all resources to the output folder as well -->
        <copy todir="${dir.classes}">
            <fileset dir="${dir.conf}"/>
        </copy>
    </target>

   <target name="package" 
           depends="compile" 
           description="Package the application">
           
        <jar jarfile="${dir.build}/${app.name}.jar" >
            <fileset dir="${dir.classes}">
                <include name="**/*.class"/>
            </fileset>
            <fileset dir="${dir.java}/main/resources">
                <include name="**/*"/>
            </fileset>
            <fileset dir="${dir.conf}">
                <include name="**/*"/>
            </fileset>

        </jar>

   </target>

   <target name="deploy" 
           depends="package" 
           description="Deploy the application to the Mule server">
           
        <copy file="${dir.build}/${app.name}.jar" 
              todir="${dir.user.lib}" 
              overwrite="true"
              verbose="true"/>
   </target>

   <target name="setup">
      <fail message="MULE_HOME environment variable has not been set.">
       <condition>
        <or>
         <equals arg1="${env.MULE_HOME}" arg2=""/>
          <!-- Using a trick here. If there was no MULE_HOME environment 
               variable set, ant will set the value to the unprocessed 
               template string -->
          <equals arg1="${env.MULE_HOME}" arg2="$${env.MULE_HOME}"/>
         </or>
        </condition>
      </fail>

      <antcall target="ifMuleBase"/>
      <antcall target="ifMuleHome"/>
   </target>

   <target name="ifMuleBase" if="env.MULE_BASE">
      <property name="dir.mule.home" value="${env.MULE_BASE}"/>
      <property name="dir.user.lib" value="${dir.mule.home}/lib/user/"/>
      <antcall target="deploy"/>
   </target>

   <target name="ifMuleHome" if="env.MULE_HOME" unless="env.MULE_BASE">
      <property name="dir.mule.home" value="${env.MULE_HOME}"/>
      <property name="dir.user.lib" value="${dir.mule.home}/lib/user/"/>
      <antcall target="deploy"/>
   </target>

</project>
