#!/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_INSTANCE" ] ; 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_INSTANCE=`dirname "$PRG"`
  cd "$saveddir"

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

# Set Defaults Properties
ARTEMIS_LOGGING_CONF="file:$ARTEMIS_INSTANCE/etc/logging.properties"
ARTEMIS_DATA_DIR="$ARTEMIS_INSTANCE/data"
ARTEMIS_LOG_MANAGER=org.jboss.logmanager.LogManager

# Load Profile Data
. "$ARTEMIS_INSTANCE/etc/artemis.profile"

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_INSTANCE" ] &&
    ARTEMIS_INSTANCE=`cygpath --unix "$ARTEMIS_INSTANCE"`
  [ -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


# finding the Log Manager
LOG_MANAGER=`ls $ARTEMIS_HOME/lib/jboss-logmanager*jar 2>/dev/null`

if [ -z "$LOG_MANAGER" ] ; then
  # this is the one found when the server was created
  LOG_MANAGER="$ARTEMIS_HOME/lib/"
fi

exec "$JAVACMD"  \
    $JAVA_ARGS \
    -Xbootclasspath/a:"$LOG_MANAGER" \
    -Djava.security.auth.login.config="$ARTEMIS_INSTANCE/etc/login.config" \
    $ARTEMIS_CLUSTER_PROPS \
    -classpath "$CLASSPATH" \
    -Dartemis.home="$ARTEMIS_HOME" \
    -Dartemis.instance="$ARTEMIS_INSTANCE" \
    -Djava.library.path="$ARTEMIS_HOME/bin/lib/linux-$(uname -m)" \
    -Djava.io.tmpdir="$ARTEMIS_INSTANCE/tmp" \
    -Ddata.dir="$ARTEMIS_DATA_DIR" \
    -Djava.util.logging.manager="$ARTEMIS_LOG_MANAGER" \
    -Dlogging.configuration="$ARTEMIS_LOGGING_CONF" \
    $DEBUG_ARGS \
    org.apache.activemq.artemis.boot.Artemis "$@"
