#!/bin/sh
#
# Copyright 2014 Confluent Inc.
#
# Licensed 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.

base_dir=$(dirname $0)/..
echo $base_dir
# Development jars. `mvn package` should collect all the required dependency jars here
for dir in $base_dir/rock/target/rock-*-development; do
  CLASSPATH=$CLASSPATH:$dir/share/java/rock/*
done

# Production jars
for library in "confluent-common" "monitoring-interceptors" "confluent-control-center" "rock"; do
  CLASSPATH=$CLASSPATH:$base_dir/share/java/$library/*
done

# logj4 settings
if [ "x$CONTROL_CENTER_LOG4J_OPTS" = "x" ]; then
  # Test for files from dev -> packages so this will work as expected in dev if you have packages
  # installed
  if [ -e "$base_dir/config/log4j.properties" ]; then # Dev environment
    CONTROL_CENTER_LOG4J_OPTS="-Dlog4j.configuration=file:$base_dir/config/log4j.properties"
  elif [ -e "$base_dir/etc/confluent-control-center/log4j.properties" ]; then # Simple zip file layout
    CONTROL_CENTER_LOG4J_OPTS="-Dlog4j.configuration=file:$base_dir/etc/confluent-control-center/log4j.properties"
  elif [ -e "/etc/confluent-control-center/log4j.properties" ]; then # Normal install layout
    CONTROL_CENTER_LOG4J_OPTS="-Dlog4j.configuration=file:/etc/confluent-control-center/log4j.properties"
  fi
fi

# JMX settings
if [ -z "$CONTROL_CENTER_JMX_OPTS" ]; then
  CONTROL_CENTER_JMX_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false "
fi

# JMX port to use
if [  $JMX_PORT ]; then
  CONTROL_CENTER_JMX_OPTS="$CONTROL_CENTER_JMX_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT "
fi

# Generic jvm settings you want to add
if [ -z "$CONTROL_CENTER_OPTS" ]; then
  CONTROL_CENTER_OPTS=""
fi

# Which java to use
if [ -z "$JAVA_HOME" ]; then
  JAVA="java"
else
  JAVA="$JAVA_HOME/bin/java"
fi

# Memory options
if [ -z "$CONTROL_CENTER_HEAP_OPTS" ]; then
  CONTROL_CENTER_HEAP_OPTS="-Xmx256M"
fi

# JVM performance options
if [ -z "$CONTROL_CENTER_JVM_PERFORMANCE_OPTS" ]; then
  CONTROL_CENTER_JVM_PERFORMANCE_OPTS="-server -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+CMSScavengeBeforeRemark -XX:+DisableExplicitGC -Djava.awt.headless=true"
fi

MAIN=$1
shift

while [ $# -gt 0 ]; do
  COMMAND=$1
  case $COMMAND in
    -help)
      HELP="true"
      shift
      ;;
    -daemon)
      DAEMON_MODE="true"
      shift
      ;;
    *)
      break
      ;;
  esac
done

if [ "x$$HELP" = "xtrue" ]; then
  echo "USAGE: $0 [-daemon] [opts] [-help]"
  exit 0
fi

# Launch mode
if [ "x$DAEMON_MODE" = "xtrue" ]; then
  nohup $JAVA $CONTROL_CENTER_HEAP_OPTS $CONTROL_CENTER_JVM_PERFORMANCE_OPTS $CONTROL_CENTER_JMX_OPTS $CONTROL_CENTER_LOG4J_OPTS -cp $CLASSPATH $CONTROL_CENTER_OPTS "$MAIN" "$@" 2>&1 < /dev/null &
else
  exec $JAVA $CONTROL_CENTER_HEAP_OPTS $CONTROL_CENTER_JVM_PERFORMANCE_OPTS $CONTROL_CENTER_JMX_OPTS $CONTROL_CENTER_LOG4J_OPTS -cp $CLASSPATH $CONTROL_CENTER_OPTS "$MAIN" "$@"
fi
