#!/bin/bash
#
# Copyright 2018 Confluent Inc.
#
# Licensed under the Confluent Community License (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.confluent.io/confluent-community-license
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES 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)/..

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

# Production jars
for library in "confluent-security/kafka-rest" "confluent-common" "confluent-telemetry" "rest-utils" "kafka-rest-bin" "kafka-rest-lib" "monitoring-interceptors"; do
  CLASSPATH=$CLASSPATH:$base_dir/share/java/$library/*
done

# logj4 settings
if [ "x$KAFKAREST_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
    KAFKAREST_LOG4J_OPTS="-Dlog4j.configuration=file:$base_dir/config/log4j.properties"
  elif [ -e "$base_dir/etc/kafka-rest/log4j.properties" ]; then # Simple zip file layout
    KAFKAREST_LOG4J_OPTS="-Dlog4j.configuration=file:$base_dir/etc/kafka-rest/log4j.properties"
  elif [ -e "/etc/kafka-rest/log4j.properties" ]; then # Normal install layout
    KAFKAREST_LOG4J_OPTS="-Dlog4j.configuration=file:/etc/kafka-rest/log4j.properties"
  fi
fi

if [[ -n $LOG_DIR ]]; then
    [[ -d $LOG_DIR ]] || mkdir -p "$LOG_DIR"
    KAFKAREST_LOG4J_OPTS="-Dkafka-rest.log.dir=$LOG_DIR ${KAFKAREST_LOG4J_OPTS}"
fi

# JMX settings
if [ -z "$KAFKAREST_JMX_OPTS" ]; then
  KAFKAREST_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
  KAFKAREST_JMX_OPTS="$KAFKAREST_JMX_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT "
fi

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

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

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

# JVM performance options
if [ -z "$KAFKAREST_JVM_PERFORMANCE_OPTS" ]; then
  KAFKAREST_JVM_PERFORMANCE_OPTS="-server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent -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 $KAFKAREST_HEAP_OPTS $KAFKAREST_JVM_PERFORMANCE_OPTS $KAFKAREST_JMX_OPTS $KAFKAREST_LOG4J_OPTS -cp $CLASSPATH $KAFKAREST_OPTS "$MAIN" "$@" 2>&1 < /dev/null &
else
  exec $JAVA $KAFKAREST_HEAP_OPTS $KAFKAREST_JVM_PERFORMANCE_OPTS $KAFKAREST_JMX_OPTS $KAFKAREST_LOG4J_OPTS -cp $CLASSPATH $KAFKAREST_OPTS "$MAIN" "$@"
fi
