#!/bin/bash
#
#  Copyright (C) 2015 Cloudius Systems, Ltd.

JMX_PORT="7199"
JMX_ADDR=

API_ADDR=
API_PORT=

CONF_FILE=""
DEBUG=""
PARAM_HELP="-h"
PARAM_JMX_PORT="-jp"
PARAM_JMX_ADDR="-ja"
PARAM_API_PORT="-p"
PARAM_ADDR="-a"
PARAM_LOCATION="-l"
LOCATION="target"
LOCATION_SCRIPTS="scripts"
PARAM_FILE="-cf"
ALLOW_REMOTE="-r"
ALLOW_DEBUG="-d"
REMOTE=0
HOSTNAME=`hostname`


PROPERTIES=
JMX_AUTH=-Dcom.sun.management.jmxremote.authenticate=false
JMX_SSL=-Dcom.sun.management.jmxremote.ssl=false

print_help() {
 cat <<HLPEND

scylla-jmx [$PARAM_HELP] [$PARAM_API_PORT port] [$PARAM_ADDR address] [$PARAM_JMX_PORT port] [$PARAM_FILE file]

This script is used to run the jmx proxy

By default it would connect to the local API ($API_ADDR) and will listen on the JMX port ($JMX_PORT)

This script receives the following command line arguments:

    $PARAM_HELP - print this help screen and exit
    $PARAM_JMX_PORT <port> - The jmx port to open
    $PARAM_API_PORT <port> - The API port to connect to
    $PARAM_ADDR <address> - The API address to connect to
    $PARAM_JMX_ADDR <address> - JMX bind address
    $PARAM_FILE <file> - A configuration file to use
    $PARAM_LOCATION <location> - The location of the jmx proxy jar file
    $ALLOW_REMOTE - When set allow remote jmx connectivity
    $ALLOW_DEBUG - When set open debug ports for remote debugger
HLPEND
}

while test "$#" -ne 0
do
  case "$1" in
    "$PARAM_API_PORT")
      API_PORT="-Dapiport="$2
      shift 2
      ;;
    "$PARAM_ADDR")
      API_ADDR="-Dapiaddress="$2
      shift 2
      ;;
    "$PARAM_JMX_PORT")
      JMX_PORT=$2
      shift 2
      ;;
    "$PARAM_JMX_ADDR")
      JMX_ADDR=-Dcom.sun.management.jmxremote.host=$2
      shift 2
      ;;
    "$PARAM_LOCATION")
      LOCATION=$2
      LOCATION_SCRIPTS="$2"
      shift 2
      ;;
    "$PARAM_FILE")
      CONF_FILE="-Dapiconfig="$2
      shift 2
      ;;
    "$ALLOW_REMOTE")
      REMOTE=1
      shift 1
      ;;
    "$PARAM_HELP")
      print_help
      exit 0
      ;;
    "$ALLOW_DEBUG")
      DEBUG="-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:7690,server=y,suspend=n"
      shift 1
      ;;
    -Dcom.sun.management.jmxremote.host=*)
      JMX_ADDR=$1
      HOSTNAME=${1:36}
      shift
      ;;
    -Dcom.sun.management.jmxremote.authenticate=*)
      JMX_AUTH=$1
      shift 1
      ;;
    -Dcom.sun.management.jmxremote.ssl=*)
      JMX_SSL=$1
      shift 1
      ;;
    -Dcom.sun.management.jmxremote.local.only=*)
      JMX_LOCAL=$1
      shift 1
      ;;
    -D*)
      PROPERTIES="$PROPERTIES $1"
      shift 1
      ;;
    *)
      echo "Unknown parameter: $1"
      print_help
      exit 1
    esac
done

if [ $REMOTE -eq 0 ]; then
    if [ -z $JMX_ADDR ]; then
        JMX_ADDR=-Dcom.sun.management.jmxremote.host=localhost
    fi
    HOSTNAME=localhost
else
    if [ -z $JMX_LOCAL ]; then
        JMX_LOCAL=-Dcom.sun.management.jmxremote.local.only=false
    fi
fi

exec "$LOCATION_SCRIPTS"/select-java $DEBUG \
    $API_PORT $API_ADDR $CONF_FILE -Xmx256m -XX:+UseSerialGC \
    -XX:+HeapDumpOnOutOfMemoryError \
    $JMX_AUTH $JMX_SSL $JMX_ADDR $JMX_LOCAL \
    -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMX_PORT \
    -Djava.rmi.server.hostname=$HOSTNAME -Dcom.sun.management.jmxremote.rmi.port=$JMX_PORT \
    -Djavax.management.builder.initial=com.scylladb.jmx.utils.APIBuilder \
    $PROPERTIES -jar $LOCATION/scylla-jmx-1.0.jar

