#! /bin/sh

######################################################################
# Deploy examples and launch Mule ESB
######################################################################

URL=http://localhost:18082/examples/

######################################################################
# Setting MULE_HOME if the script is called from outside mule script
######################################################################
# Check for MULE_HOME
if [ -z "$MULE_HOME" ] ; then
    # Get the fully qualified path to the script
    case $0 in
        /*)
            SCRIPT="$0"
            ;;
        *)
            PWD=`pwd`
            SCRIPT="$PWD/$0"
            ;;
    esac

    # Resolve the true real path without any sym links.
    CHANGED=true
    while [ "X$CHANGED" != "X" ]
    do
        # Change spaces to ":" so the tokens can be parsed.
        SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
        # Get the real path to this script, resolving any symbolic links
        TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
        REALPATH=
        for C in $TOKENS; do
            # Change any ":" in the token back to a space.
            C=`echo $C | sed -e 's;:; ;g'`
            REALPATH="$REALPATH/$C"
            # If REALPATH is a sym link, resolve it.  Loop for nested links.
            while [ -h "$REALPATH" ] ; do
                LS="`ls -ld "$REALPATH"`"
                LINK="`expr "$LS" : '.*-> \(.*\)$'`"
                if expr "$LINK" : '/.*' > /dev/null; then
                    # LINK is absolute.
                    REALPATH="$LINK"
                else
                    # LINK is relative.
                    REALPATH="`dirname "$REALPATH"`""/$LINK"
                fi
            done
        done

        if [ "$REALPATH" = "$SCRIPT" ]
        then
            CHANGED=""
        else
            SCRIPT="$REALPATH"
        fi
    done

    # Save the startup directory 
    STARTUP_DIR=`pwd`

    # Change the current directory to the location of the script
    cd "`dirname "$REALPATH"`"
    REALDIR=`pwd`

    # REALDIR points to $MULE_HOME/bin now, strip off the bin part so
    # we get a proper MULE_HOME instead of a relative path
    MULE_HOME="`dirname ${REALDIR}`"
fi

# strip a potential trailing slash in MULE_HOME. This is required to build a
# valid path for WRAPPER_CMD below which is used when running the stop
# command
MULE_HOME=`echo ${MULE_HOME} | sed -e 's/\/$//'`
export MULE_HOME

######################################################################
# Deploy examples and launch Mule ESB
######################################################################

$MULE_HOME/bin/launcher $MULE_HOME/bin/mule_examples.groovy "${URL}"
EXIT_STATUS=$?

if [ "$EXIT_STATUS" -ne "0" ] ; then
        echo "ERROR: $EXIT_STATUS. Please check log file ($MULE_HOME/logs/mule.log) to see why Mule ESB is not starting"
        exit $EXIT_STATUS
fi

######################################################################
# Open broweser and launch application
######################################################################
# Resolve the os
DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
case "$DIST_OS" in
    'sunos')
        DIST_OS="solaris"
        ;;
    'hp-ux' | 'hp-ux64')
        DIST_OS="hpux"
        ;;
    'darwin')
        DIST_OS="macosx"
        ;;
    'unix_sv')
        DIST_OS="unixware"
        ;;
    'aix')
        DIST_OS="aix"
        ;;
esac

# MAC
type -P open
if [ "$DIST_OS" = "macosx" ]; then
    open "${URL}"
    exit 0
fi

# Linux Browsers
for B in "firefox" "opera" "konqueror" "epiphany" "mozilla" "netscape"
do
if [ -n "`which $B 2>/dev/null`" ]; then
        $B "${URL}"
        exit 0
fi
done

echo "No default browser found. Example launcher URL: ${URL}"
exit 1
