#!/bin/bash
# Gateway Project Generic Launcher Script

# Use shellcheck to lint this file

base_dir=$( cd -P "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )

# Log directory to use
if [ -z "$LOG_DIR" ]; then
  LOG_DIR="$base_dir/logs"
fi

# create logs directory
if [ ! -d "$LOG_DIR" ]; then
  mkdir -p "$LOG_DIR"
fi

: "${GATEWAY_CLASSPATH:=""}"
: "${GATEWAY_LOG4J_OPTS:=""}"
: "${GATEWAY_OPTS:=""}"
: "${GATEWAY_HEAP_OPTS:=""}"
: "${GATEWAY_JVM_PERFORMANCE_OPTS:=""}"
: "${JAVA_HOME:=""}"

# Development jars. `mvn package` should collect all the required dependency jars here
for project in gateway-app gateway-filters; do
    for dir in "$base_dir/$project/target/$project"-*-development; do
      GATEWAY_DIR="$dir/share/java/$project"
      if [ -d "$GATEWAY_DIR" ]; then
        GATEWAY_CLASSPATH="$GATEWAY_CLASSPATH:$GATEWAY_DIR/*"
      fi
    done
done

# Production jars - each one is prepended so they will appear in reverse order.
for library in "gateway" "gateway-filters" "gateway-app"; do
  DIR="$base_dir/share/java/$library"
  if [ -d "$DIR" ]; then
    GATEWAY_CLASSPATH="$DIR/*:$GATEWAY_CLASSPATH"
  fi
done

# Include main target jar and dependencies
if [ -d "$base_dir/gateway-app/target" ]; then
  GATEWAY_CLASSPATH="$base_dir/gateway-app/target/*:$GATEWAY_CLASSPATH"
  # Include dependency jars
  if [ -d "$base_dir/gateway-app/target/dependency" ]; then
    GATEWAY_CLASSPATH="$base_dir/gateway-app/target/dependency/*:$GATEWAY_CLASSPATH"
  fi
fi

# Include lib directory if it exists
if [ -d "$base_dir/lib" ]; then
  GATEWAY_CLASSPATH="$base_dir/lib/*:$GATEWAY_CLASSPATH"
fi

# log4j settings
if [ -z "$GATEWAY_LOG4J2_OPTS" ]; 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/log4j2.yaml" ]; then # Dev environment
    GATEWAY_LOG4J2_OPTS="-Dlog4j2.configurationFile=file:$base_dir/config/log4j2.yaml"
  elif [ -e "$base_dir/etc/gateway/log4j2.yaml" ]; then # Simple zip file layout
    GATEWAY_LOG4J2_OPTS="-Dlog4j2.configurationFile=file:$base_dir/etc/gateway/log4j2.yaml"
  elif [ -e "/etc/gateway/log4j2.yaml" ]; then # Normal install layout
    GATEWAY_LOG4J2_OPTS="-Dlog4j2.configurationFile=file:/etc/gateway/log4j2.yaml"
  fi
fi

GATEWAY_LOG4J2_OPTS+=" -Dgateway.log.dir=$LOG_DIR ${GATEWAY_LOG4J_OPTS}"

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

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

# Memory options
if [ -z "$GATEWAY_HEAP_OPTS" ]; then
  GATEWAY_HEAP_OPTS="-Xms2g -Xmx2g"
fi

# JVM performance options
if [ -z "$GATEWAY_JVM_PERFORMANCE_OPTS" ]; then
  GATEWAY_JVM_PERFORMANCE_OPTS="-server -XX:+UseG1GC -XX:+ExplicitGCInvokesConcurrent -XX:NewRatio=1 -Djava.awt.headless=true"
fi

if [ $# -lt 1 ];
then
  echo "USAGE: $0 [-daemon] [opts] [-help]"
    exit 1
fi

MAIN="$1"
shift

DAEMON_NAME=""
GC_LOG_ENABLED=""
DAEMON_MODE=""
HELP=""
if [ "$1" = "help" ]; then
  HELP="true"
fi
while [ $# -gt 0 ]; do
  COMMAND="$1"
  case "$COMMAND" in
    -name)
      DAEMON_NAME=$2
      shift 2
      ;;
    -loggc)
      if [ -z "$GATEWAY_GC_LOG_OPTS" ]; then
        GC_LOG_ENABLED="true"
      fi
      shift
      ;;
    -daemon)
      DAEMON_MODE="true"
      shift
      ;;
    -help)
      HELP="true"
      shift
      ;;
   *)
      break
      ;;
  esac
done

# GC options
GC_FILE_SUFFIX='-gc.log'
GC_LOG_FILE_NAME=''
if [ "$GC_LOG_ENABLED" = "true" ]; then
  GC_LOG_FILE_NAME=$DAEMON_NAME$GC_FILE_SUFFIX
  # The first segment of the version number, which is '1' for releases before Java 9
  # it then becomes '9', '10', ...
  JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([0-9]*).*$/\1/p')
  if [[ "$JAVA_MAJOR_VERSION" -ge "9" ]] ; then
    GATEWAY_GC_LOG_OPTS="-Xlog:gc*:file=$LOG_DIR/$GC_LOG_FILE_NAME:time,tags:filecount=10,filesize=102400"
  else
    GATEWAY_GC_LOG_OPTS="-Xloggc:$LOG_DIR/$GC_LOG_FILE_NAME -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M"
  fi
fi

if [ "$HELP" = "true" ]; then
  exec "$JAVA" -cp "$GATEWAY_CLASSPATH" "$MAIN" --help
  exit 1
fi


# shellcheck disable=SC2206
OPTIONS=($GATEWAY_HEAP_OPTS)
# shellcheck disable=SC2206
OPTIONS+=($GATEWAY_JVM_PERFORMANCE_OPTS)
# shellcheck disable=SC2206
OPTIONS+=($GATEWAY_LOG4J2_OPTS)
# shellcheck disable=SC2206
OPTIONS+=($GATEWAY_OPTS)
# shellcheck disable=SC2206
OPTIONS+=($GATEWAY_GC_LOG_OPTS)

echo "Classpath: $GATEWAY_CLASSPATH"
echo "options:"
for opt in "${OPTIONS[@]}"; do
  echo "  $opt"
done
# Launch mode
if [ "$DAEMON_MODE" = "true" ]; then
  DAEMON_STDOUT_FILE="$LOG_DIR/gateway.out"
  echo "Writing console output to $DAEMON_STDOUT_FILE"
  nohup "$JAVA" -cp "$GATEWAY_CLASSPATH" "${OPTIONS[@]}" "$MAIN" "$@" 2>&1 < /dev/null > "$DAEMON_STDOUT_FILE" &
else
  exec "$JAVA" -cp "$GATEWAY_CLASSPATH" "${OPTIONS[@]}" "$MAIN" "$@"
fi
