#!/bin/bash
#
# Copyright 2018 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.
#

KAFKAREST_HOME=/usr/lib/kafka-rest

CLASSPATH=$CLASSPATH:$KAFKAREST_HOME/lib/*

# Production jars
for library in "kafka-common-utils" "rest-utils"; do
  CLASSPATH=$CLASSPATH:/usr/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 "$KAFKAREST_HOME/config/log4j.properties" ]; then # Dev environment
    KAFKAREST_LOG4J_OPTS="-Dlog4j.configuration=file:$KAFKAREST_HOME/config/log4j.properties"
  elif [ -e "$KAFKAREST_HOME/etc/kafka-rest/log4j.properties" ]; then # Simple zip file layout
    KAFKAREST_LOG4J_OPTS="-Dlog4j.configuration=file:$KAFKAREST_HOME/etc/kafka-rest/log4j.properties"
  elif [ -e "/etc/kafka-rest/conf/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
