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

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

# Production jars, including kafka, rest-utils, and schema-registry
for library in "confluent-common" "rest-utils" "schema-registry"; do
  CLASSPATH=$CLASSPATH:$base_dir/share/java/$library/*      
done

# Log directory to use
if [ "x$LOG_DIR" = "x" ]; then
    LOG_DIR="/tmp/schema-registry-logs"
fi
  
# create logs directory
if [ ! -d "$LOG_DIR" ]; then
  mkdir -p "$LOG_DIR"
fi

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

SCHEMA_REGISTRY_LOG4J_OPTS="-Dschema-registry.log.dir=$LOG_DIR $SCHEMA_REGISTRY_LOG4J_OPTS"

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

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

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

# Memory options
if [ -z "$SCHEMA_REGISTRY_HEAP_OPTS" ]; then
  SCHEMA_REGISTRY_HEAP_OPTS="-Xmx512M"
fi

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