#!/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

HAWTIO_ROLE="NO_HAWTIO_ROLE"

# Load Profile Data
ARTEMIS_INSTANCE_ETC='${artemis.instance.etc}'
. "$ARTEMIS_INSTANCE_ETC/artemis.profile"

# Set Defaults Properties
ARTEMIS_LOGGING_CONF="$ARTEMIS_INSTANCE_ETC_URI/logging.properties"
ARTEMIS_LOG_MANAGER=org.jboss.logmanager.LogManager


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/${logmanager}"
fi

WILDFLY_COMMON=`ls $ARTEMIS_HOME/lib/wildfly-common*jar 2>/dev/null`
if [ -z "$WILDFLY_COMMON" ] ; then
  # this is the one found when the server was created
  WILDFLY_COMMON="$ARTEMIS_HOME/lib/${wildfly-common}"
fi

if [ -f "$ARTEMIS_OOME_DUMP" ] ; then
  # backup the last OOME heap dump
  mv $ARTEMIS_OOME_DUMP $ARTEMIS_OOME_DUMP.bkp
fi

exec "$JAVACMD" \
    $JAVA_ARGS \
    -Dhawtio.role="$HAWTIO_ROLE" \
    -Xbootclasspath/a:"$LOG_MANAGER:$WILDFLY_COMMON" \
    -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" \
    -Dartemis.instance.etc="$ARTEMIS_INSTANCE_ETC" \
    -Djava.util.logging.manager="$ARTEMIS_LOG_MANAGER" \
    -Dlogging.configuration="$ARTEMIS_LOGGING_CONF" \
    -Dartemis.default.sensitive.string.codec.key="$ARTEMIS_DEFAULT_SENSITIVE_STRING_CODEC_KEY" \
    $DEBUG_ARGS \
    org.apache.activemq.artemis.boot.Artemis "$@"
