#!/usr/bin/env sh
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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 WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

if [ -z "$ARTEMIS_HOME" ] ; then

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

  # need this for relative symlinks
  dirname_prg=`dirname "$PRG"`
  cd "$dirname_prg"

  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

  ARTEMIS_HOME=`dirname "$PRG"`
  cd "$saveddir"

  # make it fully qualified
  ARTEMIS_HOME=`cd "$ARTEMIS_HOME/.." && pwd`
fi

# Set Defaults Properties
JAVA_ARGS="-XX:+UseParallelGC -XX:+AggressiveOpts -XX:+UseFastAccessorMethods -Xms512M -Xmx1024M"
CLASSPATH="$ARTEMIS_HOME/lib/artemis-boot.jar"

# OS specific support.
cygwin=false;
darwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true
    OSTYPE=cygwin
    export OSTYPE
    ;;
  Darwin*) darwin=true
    if [ -z "$JAVA_HOME" ] ; then
     JAVA_HOME=$(/usr/libexec/java_home)
    fi
    ;;
esac

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

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=java
  fi
fi

if $cygwin ; then
  JAVA_HOME=`cygpath --windows "$JAVA_HOME"`
  ARTEMIS_HOME=`cygpath --windows "$ARTEMIS_HOME"`
  CLASSPATH=`cygpath --windows "$CLASSPATH"`
fi

exec "$JAVACMD" $JAVA_ARGS $ARTEMIS_CLUSTER_PROPS \
    -classpath "$CLASSPATH" \
    -Dartemis.home="$ARTEMIS_HOME" \
    -Djava.library.path="$ARTEMIS_HOME/bin/lib/linux-$(uname -m)" \
    $DEBUG_ARGS \
    org.apache.activemq.artemis.boot.Artemis "$@"
