#! /bin/sh

#   Copyright 2001-2004 The Apache Software Foundation
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRMULEIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

# Extract launch and mule arguments, (see details below).
mule_exec_args=
no_config=false
use_jikes_default=false
mule_exec_debug=false
show_help=false
for arg in "$@" ; do
  if [ "$arg" = "--noconfig" ] ; then
    no_config=true
  elif [ "$arg" = "--usejikes" ] ; then
    use_jikes_default=true
  elif [ "$arg" = "--execdebug" ] ; then
    mule_exec_debug=true
  elif [ my"$arg" = my"--h"  -o my"$arg" = my"--help"  ] ; then
    show_help=true
    mule_exec_args="$mule_exec_args -h"
  else
    if [  my"$arg" = my"-h"  -o  my"$arg" = my"-help" ] ; then
      show_help=true
    fi
    mule_exec_args="$mule_exec_args \"$arg\""
  fi
done

# Source/default mule configuration
if $no_config ; then
  rpm_mode=false
  usejikes=$use_jikes_default
else
  # load system-wide mule configuration
  if [ -f "/etc/mule.conf" ] ; then
    . /etc/mule.conf
  fi

  # load user mule configuration
  if [ -f "$HOME/.mule/mule.conf" ] ; then
    . $HOME/.mule/mule.conf
  fi
  if [ -f "$HOME/.mulerc" ] ; then
    . "$HOME/.mulerc"
  fi

  # provide default configuration values
  if [ -z "$rpm_mode" ] ; then
    rpm_mode=false
  fi
  if [ -z "$usejikes" ] ; then
    usejikes=$use_jikes_default
  fi
fi

# Setup Java environment in rpm mode
if $rpm_mode ; then
  if [ -f /usr/share/java-utils/java-functions ] ; then
    . /usr/share/java-utils/java-functions
    set_jvm
    set_javacmd
  fi
fi

# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  Darwin*) darwin=true
           if [ -z "$JAVA_HOME" ] ; then
             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
           fi
           ;;
esac


#set MULE_HOME if not set
if [ -z "$MULE_HOME" -o ! -d "$MULE_HOME" ] ; then
  # try to find MULE
  if [ -d /opt/mule ] ; then
    MULE_HOME=/opt/mule
  fi

  if [ -d "${HOME}/opt/mule" ] ; then
    MULE_HOME="${HOME}/opt/mule"
  fi

  ## resolve links - $0 may be a link to mule's home
  PRG="$0"
  progname=`basename "$0"`

  # need this for relative symlinks
  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
    else
    PRG=`dirname "$PRG"`"/$link"
    fi
  done

  MULE_HOME=`dirname "$PRG"`/..

  # make it fully qualified
  MULE_HOME=`cd "$MULE_HOME" && pwd`
fi
echo MULE_HOME=$MULE_HOME

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$MULE_HOME" ] &&
    MULE_HOME=`cygpath --unix "$MULE_HOME"`
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi

# set MULE_LIB location
MULE_LIB="${MULE_HOME}/lib"

if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD=`which java 2> /dev/null `
    if [ -z "$JAVACMD" ] ; then
        JAVACMD=java
    fi
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit 1
fi

# Build local classpath using just the launcher in non-rpm mode or
# use the Jpackage helper in rpm mode with basic and default jars
# specified in the mule.conf configuration. Because the launcher is
# used, libraries linked in MULE_HOME will also be include, but this
# is discouraged as it is not java-version safe. A user should
# request optional jars and their dependencies via the OPT_JAR_LIST
# variable
if $rpm_mode && [ -f /usr/bin/build-classpath ] ; then
  LOCALCLASSPATH="$(/usr/bin/build-classpath mule mule-launcher)"
  # If the user requested to try to add some other jars to the classpath
  if [ -n "$OPT_JAR_LIST" ] ; then
    _OPTCLASSPATH="$(/usr/bin/build-classpath $OPT_JAR_LIST 2> /dev/null)"
    if [ -n "$_OPTCLASSPATH" ] ; then 
      LOCALCLASSPATH="$LOCALCLASSPATH:$_OPTCLASSPATH"
    fi
  fi

  # Explicitly add javac path to classpath, assume JAVA_HOME set
  # properly in rpm mode
  if [ -f "$JAVA_HOME/lib/tools.jar" ] ; then
    LOCALCLASSPATH="$LOCALCLASSPATH:$JAVA_HOME/lib/tools.jar"
  fi
  if [ -f "$JAVA_HOME/lib/classes.zip" ] ; then
    LOCALCLASSPATH="$LOCALCLASSPATH:$JAVA_HOME/lib/classes.zip"
  fi

  # if CLASSPATH_OVERRIDE env var is set, LOCALCLASSPATH will be
  # user CLASSPATH first and mule-found jars after.
  # In that case, the user CLASSPATH will override mule-found jars
  #
  # if CLASSPATH_OVERRIDE is not set, we'll have the normal behaviour
  # with mule-found jars first and user CLASSPATH after
  if [ -n "$CLASSPATH" ] ; then
    # merge local and specified classpath 
    if [ -z "$LOCALCLASSPATH" ] ; then 
      LOCALCLASSPATH="$CLASSPATH"
    elif [ -n "$CLASSPATH_OVERRIDE" ] ; then
      LOCALCLASSPATH="$CLASSPATH:$LOCALCLASSPATH"
    else
      LOCALCLASSPATH="$LOCALCLASSPATH:$CLASSPATH"
    fi

    # remove class path from launcher -lib option
    CLASSPATH=""
  fi
else
  # not using rpm_mode; use launcher to determine classpaths
  if [ -z "$LOCALCLASSPATH" ] ; then
      LOCALCLASSPATH=$MULE_HOME/bin/mule-launcher.jar
  else
      LOCALCLASSPATH=$MULE_HOME/bin/mule-launcher.jar:$LOCALCLASSPATH
  fi
fi

if [ -n "$JAVA_HOME" ] ; then
  # OSX hack to make Mule work with jikes
  if $darwin ; then
    OSXHACK="${JAVA_HOME}/../Classes"
    if [ -d "${OSXHACK}" ] ; then
      for i in "${OSXHACK}"/*.jar
      do
        JIKESPATH="$JIKESPATH:$i"
      done
    fi
  fi
fi

if [ -n "MULE_OPTS" ] ; then
  MULE_OPTS="-Xmx512M"
fi

# Allow Jikes support (off by default)
if $usejikes; then
  MULE_OPTS="$MULE_OPTS -Dbuild.compiler=jikes"
fi

# For Cygwin, switch paths to appropriate format before running java
if $cygwin; then
  if [ "$OS" = "Windows_NT" ] && cygpath -m .>/dev/null 2>/dev/null ; then
    format=mixed
  else
    format=windows
  fi
  MULE_HOME=`cygpath --$format "$MULE_HOME"`
  MULE_LIB=`cygpath --$format "$MULE_LIB"`
  JAVA_HOME=`cygpath --$format "$JAVA_HOME"`
  LOCALCLASSPATH=`cygpath --path --$format "$LOCALCLASSPATH"`
  if [ -n "$CLASSPATH" ] ; then
    CLASSPATH=`cygpath --path --$format "$CLASSPATH"`
  fi
  CYGHOME=`cygpath --$format "$HOME"`
fi

# add a second backslash to variables terminated by a backslash under cygwin
if $cygwin; then
  case "$MULE_HOME" in
    *\\ )
    MULE_HOME="$MULE_HOME\\"
    ;;
  esac
  case "$CYGHOME" in
    *\\ )
    CYGHOME="$CYGHOME\\"
    ;;
  esac
  case "$JIKESPATH" in
    *\\ )
    JIKESPATH="$JIKESPATH\\"
    ;;
  esac
  case "$LOCALCLASSPATH" in
    *\\ )
    LOCALCLASSPATH="$LOCALCLASSPATH\\"
    ;;
  esac
  case "$CLASSPATH" in
    *\\ )
    CLASSPATH="$CLASSPATH\\"
    ;;
  esac
fi
# Execute mule using eval/exec to preserve spaces in paths,
# java options, and mule args
mule_sys_opts=
if [ -n "$CYGHOME" ]; then
  if [ -n "$JIKESPATH" ]; then
    mule_sys_opts="-Djikes.class.path=\"$JIKESPATH\" -Dcygwin.user.home=\"$CYGHOME\""
  else
    mule_sys_opts="-Dcygwin.user.home=\"$CYGHOME\""
  fi
else
  if [ -n "$JIKESPATH" ]; then
    mule_sys_opts="-Djikes.class.path=\"$JIKESPATH\""
  fi
fi


# Uncomment to enable YourKit profiling
#MULE_DEBUG_OPTS="-Xrunyjpagent"

# Uncomment to enable remote debugging
#MULE_DEBUG_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"

if [ ! -n "MULE_MAIN" ] ; then
  MULE_MAIN=org.mule.MuleServer
fi

mule_exec_command="exec \"$JAVACMD\" $MULE_DEBUG_OPTS $MULE_OPTS -classpath \"$LOCALCLASSPATH\" -Dorg.mule.main.class=\"$MULE_MAIN\" -Dorg.mule.home=\"$MULE_HOME\" -Dorg.mule.lib.dir=\"$MULE_LIB\" $mule_sys_opts org.mule.tools.launcher.Launcher $MULE_ARGS -lib \"$CLASSPATH\" $mule_exec_args"
if $mule_exec_debug ; then
    echo $mule_exec_command
fi
eval $mule_exec_command
