#!/bin/bash
# Copyright (c) 2011-2013 Cloudera, Inc. All rights reserved.

set -e
set -x

# Time marker for both stderr and stdout
date; date 1>&2

# More conservative umask for logs, indexes, and such.
umask 027

# BASE_JAVA_OPTS may be overwritten
BASE_JAVA_OPTS="-server -XX:+UseConcMarkSweepGC -XX:+UseParNewGC"


# This is the name of the command line argument we pass to the
# firehose and headlamp services when we start them. This must be kept in
# sync with com.cloudera.cmon.Firehose.Main and
# com.cloudera.headlamp.HeadlampConfiguration classes.
MGMT_HOME_ARG_NAME="--mgmt-home"

cloudera_config=`dirname $0`
cloudera_config=`cd "$cloudera_config/../common"; pwd`
. ${cloudera_config}/cloudera-config.sh

# attempt to find java
locate_java_home

# Search-replace {{CMF_CONF_DIR}} in files
replace_conf_dir

# Load the parcel environment
source_parcel_environment

if [ "$JAVA_HOME" != "" ]; then
  JAVA="$JAVA_HOME/bin/java"
fi

if [[ "$MGMT_HOME" ]]; then
  # "*" is interpreted by Java, to mean all jars in dir.
  MGMT_CLASSPATH="$MGMT_HOME/lib/*"
fi

if [[ -z "$MGMT_CLASSPATH" ]]; then
  echo "\$MGMT_CLASSPATH" is not set.
fi

# Add JDBC jars to the path. Oracle and Mysql installed by end user, can't ship due to licensing.
JDBC_JARS="$CLOUDERA_MYSQL_CONNECTOR_JAR:$CLOUDERA_POSTGRESQL_JDBC_JAR:$CLOUDERA_ORACLE_CONNECTOR_JAR"
MGMT_CLASSPATH="${JDBC_JARS}:${MGMT_CLASSPATH}:${MGMT_AUX_PATH}"

DDL_DIR="$MGMT_HOME/schema"

echo_and_exec() {
  echo "Executing: $@"
  exec "$@"
}

# NOTHING SENSITIVE SHOULD BE PRINTED BEFORE THIS LINE.

if [[ "$EMIT_SENSITIVE_DATA_IN_STDERR" == "false" ]]; then
  set +x
fi

MGMT_JAVA_OPTS="$BASE_JAVA_OPTS $MGMT_JAVA_OPTS"
MGMT_JAVA_OPTS="$MGMT_JAVA_OPTS -Dmgmt.log.file=$MGMT_LOG_FILE"
MGMT_JAVA_OPTS="$MGMT_JAVA_OPTS -Djava.awt.headless=true"
MGMT_JAVA_OPTS="$MGMT_JAVA_OPTS -Djava.net.preferIPv4Stack=true"

if [[ "$1" == "firehose" ]]; then
  shift

  MGMT_JAVA_OPTS="$MGMT_JAVA_OPTS -Dfirehose.schema.dir=$DDL_DIR"
  # We set the connection and responseTimeout for SMON to make sure its calls as
  # a JMX client have appropriate connection and read timeouts. To adjust these,
  # the additional JVM options configuration parameter can be used, as those
  # values will be last and will therefore take effect.
  #
  # See http://docs.oracle.com/javase/6/docs/technotes/guides/rmi/sunrmiproperties.html
  # for the documentation of these options.
  #
  # We also give SMON more permgen than the default value.
  if [[ $@ =~ "SERVICE_MONITORING" ]]; then
    MGMT_JAVA_OPTS="$MGMT_JAVA_OPTS -XX:PermSize=128m -Dsun.rmi.transport.tcp.handshakeTimeout=10000 -Dsun.rmi.transport.tcp.responseTimeout=10000"
  fi
  # HMON and SMON use leveldbjni, which unpacks a shared object from one of our
  # jars before loading it. This needs to be on a filesystem that is not mounted
  # noexec. We'll use the process's directory. See OPSAPS-21249 for more detail.
  if [[ $@ =~ "SERVICE_MONITORING" ]] || [[ $@ =~ "HOST_MONITORING" ]]; then
    # Clean old leveldbjni libraries. If SMON was killed, e.g., oom, it
    # will be restarted by supervisord in the same dir and we will unpack the
    # leveldbjni again. Let's delete any leveldbjni library leftovers, see
    # OPSAPS-26660 for more details.
    echo "Removing any leveldbjni library files left over from previous runs"
    rm -fv $(pwd)/libleveldbjni-64-*
    MGMT_JAVA_OPTS="$MGMT_JAVA_OPTS -Dlibrary.leveldbjni.path=$(pwd)"
  fi

  # Current directory has the config file and the log4j file
  MGMT_CLASSPATH="$(pwd):$MGMT_CLASSPATH"
  FIREHOSE_JAVA_OPTS=$(replace_pid $FIREHOSE_JAVA_OPTS)
  # We need to add the MGMT_HOME directory as an argument to the jvm.
  echo_and_exec ${JAVA} ${MGMT_JAVA_OPTS} ${FIREHOSE_JAVA_OPTS} -cp "${MGMT_CLASSPATH}" com.cloudera.cmon.firehose.Main "$@" "${MGMT_HOME_ARG_NAME}" "${MGMT_HOME}"

elif [[ "$1" == "eventserver" ]]; then
  shift

  # Current directory has the config file and the log4j file
  MGMT_CLASSPATH="$(pwd):$MGMT_CLASSPATH"

  EVENTSERVER_JAVA_OPTS=$(replace_pid $EVENTSERVER_JAVA_OPTS)

  echo_and_exec ${JAVA} ${MGMT_JAVA_OPTS} ${EVENTSERVER_JAVA_OPTS} -cp "${MGMT_CLASSPATH}" com.cloudera.cmf.eventcatcher.server.EventCatcherService "$@"

elif [[ "$1" == "alertpublisher" ]]; then
  shift

  # Current directory has the config file and the log4j file
  MGMT_CLASSPATH="$(pwd):$MGMT_CLASSPATH"

  ALERTPUBLISHER_JAVA_OPTS=$(replace_pid $ALERTPUBLISHER_JAVA_OPTS)

  echo_and_exec ${JAVA} ${MGMT_JAVA_OPTS} ${ALERTPUBLISHER_JAVA_OPTS} -cp "${MGMT_CLASSPATH}" com.cloudera.enterprise.alertpublisher.AlertPublisher "$@"

elif [[ "$1" == "headlamp" ]]; then
  shift

  MGMT_JAVA_OPTS="$MGMT_JAVA_OPTS -Dheadlamp.schema.dir=$DDL_DIR"

  # Current directory has the config file and the log4j file
  MGMT_CLASSPATH="$(pwd):$MGMT_CLASSPATH"

  HEADLAMP_JAVA_OPTS=$(replace_pid $HEADLAMP_JAVA_OPTS)

  echo_and_exec ${JAVA} ${MGMT_JAVA_OPTS} ${HEADLAMP_JAVA_OPTS} -cp "${MGMT_CLASSPATH}" com.cloudera.headlamp.HeadlampServer --db-conf-dir "`pwd`" --db-conf-file headlamp.db.properties "$@" "${MGMT_HOME_ARG_NAME}" "${MGMT_HOME}"
elif [[ "$1" == "telemetrypublisher" ]]; then
  shift

  # Current directory has the config file and the log4j file
  TELEPUB_HOME=${TELEPUB_HOME:-$MGMT_HOME/cloudera-scm-telepub}
  TELEPUB_CLASSPATH=`find ${TELEPUB_HOME} -name cdx-server*.jar`
  TELEPUB_CLASSPATH="$TELEPUB_CLASSPATH:$TELEPUB_HOME/jars/*:$MGMT_AUX_PATH"
  set_classpath_in_var TELEPUB_EXTRA_CLASSPATH
   perl -pi -e "s#{{TELEPUB_HOME}}#$TELEPUB_HOME#g" $(pwd)/telemetrypublisher.conf

  TELEMETRYPUBLISHER_JAVA_OPTS=$(replace_pid $TELEMETRYPUBLISHER_JAVA_OPTS)

  echo_and_exec ${JAVA} ${MGMT_JAVA_OPTS} ${TELEMETRYPUBLISHER_JAVA_OPTS} \
    -cp "${TELEPUB_CLASSPATH}:${TELEPUB_EXTRA_CLASSPATH}" \
    -Dlog4j.configuration=file://$(pwd)/log4j.properties \
    com.cloudera.cdx.extractor.server.CdhExtractorServer \
    $(pwd)/telemetrypublisher.conf \
    $(pwd)/telemetrypublisher-cm-auth.conf
elif [[ "$1" == "inspector" ]]; then
  shift

  # Current directory has the config file and the log4j file
  MGMT_CLASSPATH="$(pwd):$MGMT_CLASSPATH"

  echo_and_exec ${JAVA} ${MGMT_JAVA_OPTS} -cp "${MGMT_CLASSPATH}" com.cloudera.cmf.inspector.Inspector "$@"

elif [[ "$1" == "navigator" ]]; then
  shift

  AUDIT_SERVER_HOME=${AUDIT_SERVER_HOME:-$MGMT_HOME/cloudera-navigator-audit-server}
  MGMT_JAVA_OPTS="${MGMT_JAVA_OPTS} -Dnavigator.schema.dir=${AUDIT_SERVER_HOME}/schema -Dnavigator.auditModels.dir=${AUDIT_SERVER_HOME}/auditModels"

  NAVIGATOR_JAVA_OPTS=$(replace_pid $NAVIGATOR_JAVA_OPTS)

  set_classpath_in_var NAVIGATOR_EXTRA_CLASSPATH
  # Current directory has the config file and the log4j file
  AUDIT_SERVER_CLASSPATH="$(pwd):${JDBC_JARS}:${NAVIGATOR_EXTRA_CLASSPATH}:${AUDIT_SERVER_HOME}/*:$MGMT_AUX_PATH"

  echo_and_exec ${JAVA} ${MGMT_JAVA_OPTS} ${NAVIGATOR_JAVA_OPTS} \
    -cp "${AUDIT_SERVER_CLASSPATH}" \
    com.cloudera.navigator.NavigatorMain "$@" --conf-dir "`pwd`"

elif [[ "$1" == "nav-meta-server" ]]; then
  shift

  NAVIGATOR_HOME=${NAVIGATOR_HOME:-$MGMT_HOME/cloudera-navigator-server}
  NAVIGATOR_CLASSPATH=`find ${NAVIGATOR_HOME} -name nav-server*.jar`
  NAVIGATOR_CLASSPATH="$NAVIGATOR_CLASSPATH:$NAVIGATOR_HOME/jars/*:$MGMT_AUX_PATH"
  set_classpath_in_var NAVIGATOR_EXTRA_CLASSPATH
  perl -pi -e "s#{{NAVIGATOR_HOME}}#$NAVIGATOR_HOME#g" $(pwd)/cloudera-navigator.properties
  perl -pi -e "s#{{SSL_CLIENT_LOC}}#$(pwd)#g" $(pwd)/cloudera-navigator.properties

  NAVIGATOR_JAVA_OPTS=$(replace_pid $NAVIGATOR_JAVA_OPTS)

  AUDIT_SERVER_HOME=${AUDIT_SERVER_HOME:-$MGMT_HOME/cloudera-navigator-audit-server}
  echo_and_exec ${JAVA} ${MGMT_JAVA_OPTS} ${NAVIGATOR_JAVA_OPTS} \
    -cp "${JDBC_JARS}:$NAVIGATOR_CLASSPATH:$NAVIGATOR_EXTRA_CLASSPATH" \
    -Dlog4j.configuration=file://$(pwd)/log4j.properties \
    -Dnavigator.auditModels.dir=${AUDIT_SERVER_HOME}/auditModels \
    com.cloudera.nav.server.NavServer \
    $(pwd)/cloudera-navigator.properties \
    $(pwd)/cloudera-navigator-cm-auth.properties \
    $(pwd)/db.navms.properties \
    $(pwd)/cm-ext-accounts.properties
else
  echo "Unsupported arguments: $*"
  exit 1
fi
