#! /bin/sh

# 3rd party libraries for LoanBroker ESB
ACTIVEMQ=activemq-core-4.1.0-incubator.jar
OPENEJB=openejb-core-1.0.jar
# 3rd party libraries for LoanBroker BPM
BSH=bsh-1.3.0.jar
HSQLDB=hsqldb-1.8.0.7.jar

# There is no need to call this if you set the MULE_HOME in your environment
if [ -z "$MULE_HOME" ] ; then
  # find the name of the real MULE_HOME which is two levels up
  MULE_HOME=`pwd`
  MULE_HOME=`dirname "$MULE_HOME"`
  MULE_HOME=`dirname "$MULE_HOME"`
  export MULE_HOME
fi

# If MULE_BASE is not set, make it MULE_HOME
if [ -z "$MULE_BASE" ] ; then
  MULE_BASE="$MULE_HOME"
  export MULE_BASE
fi

# Any changes to the files in ./conf will take precedence over those deployed to $MULE_HOME/lib/user
MULE_LIB=./conf
export MULE_LIB

echo "The Loan Broker example is available in three flavors:"
echo "  1. Loan Broker ESB"
echo "  2. Loan Broker ESN"
echo "  3. Loan Broker BPM"
echo "Select the one you wish to execute and press Enter..."
read i

if [ 1 = $i ]
then
    if [ ! -f "$MULE_BASE/lib/user/${ACTIVEMQ}" ]; then
       if [ ! -f "$MULE_HOME/lib/user/${ACTIVEMQ}" ]; then
          MISSING_LIB=1
       fi
    fi
    if [ ! -f "$MULE_BASE/lib/user/${OPENEJB}" ]; then
       if [ ! -f "$MULE_HOME/lib/user/${OPENEJB}" ]; then
          MISSING_LIB=1
       fi
    fi
    if [ -z "$MISSING_LIB" ]
    then
        exec "$MULE_BASE/bin/mule" -main org.mule.examples.loanbroker.esb.LoanBrokerApp
    fi
elif [ 2 = $i ]
then
    exec "$MULE_BASE/bin/mule" -main org.mule.examples.loanbroker.esn.LoanBrokerApp
elif [ 3 = $i ]
then
    if [ ! -f "$MULE_BASE/lib/user/${BSH}" ]; then
       if [ ! -f "$MULE_HOME/lib/user/${BSH}" ]; then
          MISSING_LIB=1
       fi
    fi
    if [ ! -f "$MULE_BASE/lib/user/${HSQLDB}" ]; then
       if [ ! -f "$MULE_HOME/lib/user/${HSQLDB}" ]; then
          MISSING_LIB=1
       fi
    fi
    if [ -z "$MISSING_LIB" ]
    then
        exec "$MULE_BASE/bin/mule" -main org.mule.examples.loanbroker.bpm.LoanBrokerApp
    fi
fi

if [ 1 = "$MISSING_LIB" ]
then
    echo "This example requires additional libraries which need to be downloaded by the build script.  Please follow the instructions in the README.txt file."
fi

