#! /bin/sh

# 3rd party libraries for LoanBroker ESB
ACTIVEMQ=activemq-core-4.1.2.jar
OPENEJB=openejb-core-1.0.jar
# 3rd party libraries for LoanBroker BPM
BSH=bsh-1.3.0.jar
DERBYDB=derby-10.3.1.4.jar

# The variable is not actually needed to run the example, but it is needed to 
# build the example, and if you haven't built the example, you can't run it. 
if [ -z "$MULE_HOME" ] ; then
  echo "You must set the environment variable MULE_HOME to the location of your Mule install in order to run this example." 
  exit 1
fi

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

# This extends the classpath to include the configuration directory
# 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
        # MULE_HOME_UNIX must be set. This is a workaround for windows but since the config relies
        # on it we need to implement it here, too
        exec "$MULE_BASE/bin/mule" -main org.mule.example.loanbroker.esb.LoanBrokerApp -M-Dmule.home.unix="$MULE_HOME"
    fi
elif [ 2 = $i ]
then
    exec "$MULE_BASE/bin/mule" -main org.mule.example.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/${DERBYDB}" ]; then
       if [ ! -f "$MULE_HOME/lib/user/${DERBYDB}" ]; then
          MISSING_LIB=1
       fi
    fi
    if [ -z "$MISSING_LIB" ]
    then
        exec "$MULE_BASE/bin/mule" -main org.mule.example.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
