#!/usr/bin/env bash

#************************ Warning **********************************
#*                                                                 *
#* This configuration is property of OPEN SOURCE CONSULTING, INC.  *
#* Don't distribute this to other project.                         *
#*                                                                 *
#* Contact jchoi@osci.kr(http://www.osci.kr)                       *
#*******************************************************************

BASE_DIR="$(cd "$(dirname "$0")"; pwd)";

export CONTAINER_IP=`cat /etc/hosts | egrep -v "ip6-|local|Kubernetes-managed" | awk '{print $1}'`
export CONTAINER_NAME=`cat /etc/hosts | egrep -v "ip6-|local|Kubernetes-managed" | awk '{print $2}'`

export UMASK=022

#####################################
## Set Tomcat base env
#####################################
export JAVA_HOME=/usr/lib/jvm/java-1.8.0
export SERVER_NAME=${WASUP_TOMCAT9_TEMPLATE}
export SERVER=${SERVER_NAME}-${CONTAINER_NAME}

export CATALINA_HOME=/opt/wasup/engines/${WASUP_TOMCAT9_ENGINE}
export CATALINA_BASE=/opt/wasup/servers/${WASUP_TOMCAT9_TEMPLATE}

export CONF_DIR=${CATALINA_BASE}/conf
export PORT_OFFSET=0
export COMP_USER=wasup

export JVM_ROUTE=${WASUP_TOMCAT9_TEMPLATE}
export LOG_BASE=$CATALINA_BASE/logs/

#####################################
## Set Port Configuration
#####################################
#  Default Ports are as below
#  HTTP_PORT        : 8080
#  SSL_PORT         : 8443
#  SHUTDOWN_PORT    : 8005
#  RMI_PORT         : 9999
#####################################

CUSTOM_HTTP_PORT=8080
CUSTOM_AJP_PORT=8009
CUSTOM_SSL_PORT=
CUSTOM_SHUTDOWN_PORT=8005
CUSTOM_RMI_PORT=9999
CUSTOM_JVM_OPTIONS="-Xms1024m -Xmx1024m -XX:MaxMetaspaceSize=256m -XX:MetaspaceSize=256m"

if [ "x$CUSTOM_HTTP_PORT" = "x" ]; then
    export HTTP_PORT=$(expr 8080 + $PORT_OFFSET)
else
    export HTTP_PORT=$CUSTOM_HTTP_PORT
fi

if [ "x$CUSTOM_AJP_PORT" = "x" ]; then
    export AJP_PORT=$(expr 8009 + $PORT_OFFSET)
else
    export AJP_PORT=$CUSTOM_AJP_PORT
fi

if [ "x$CUSTOM_SSL_PORT" = "x" ]; then
    export SSL_PORT=$(expr 8443 + $PORT_OFFSET)
else
    export SSL_PORT=$CUSTOM_SSL_PORT
fi

if [ "x$CUSTOM_SHUTDOWN_PORT" = "x" ]; then
    export SHUTDOWN_PORT=$(expr 8005 + $PORT_OFFSET)
else
    export SHUTDOWN_PORT=$CUSTOM_SHUTDOWN_PORT
fi

if [ "x$CUSTOM_RMI_PORT" = "x" ]; then
    export RMI_PORT=$(expr 9999 + $PORT_OFFSET)
else
    export RMI_PORT=$CUSTOM_RMI_PORT
fi

if [ "x$CUSTOM_JVM_OPTIONS" = "x" ]; then
    export JVM_OPTIONS="-Xms1024m -Xmx1024m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m"
else
    export JVM_OPTIONS=$CUSTOM_JVM_OPTIONS
fi

#####################################
## JVM Parameter Configuration
#####################################

# Remove the JAVA_OPTS environment variable.
# Initialize required in case of start by WASup agent process.
unset JAVA_OPTS

if [ "x$JAVA_OPTS" = "x" ]; then
    JAVA_OPTS="-server"
    JAVA_OPTS="$JAVA_OPTS -Dserver=$SERVER"
    JAVA_OPTS="$JAVA_OPTS -Djvmid=$SERVER"
    JAVA_OPTS="$JAVA_OPTS -Dhttp.port=$HTTP_PORT"
    JAVA_OPTS="$JAVA_OPTS -Dajp.port=$AJP_PORT"
    JAVA_OPTS="$JAVA_OPTS -Dssl.port=$SSL_PORT"
    JAVA_OPTS="$JAVA_OPTS -Dshutdown.port=$SHUTDOWN_PORT"
    JAVA_OPTS="$JAVA_OPTS -DjvmRoute=$JVM_ROUTE"
    JAVA_OPTS="$JAVA_OPTS -Djava.library.path=${CATALINA_HOME}/lib"
    JAVA_OPTS="$JAVA_OPTS -DLOG_BASE=$LOG_BASE"

#####################################
## Set Port Configuration
#####################################

    JAVA_OPTS="$JAVA_OPTS -noverify"
    JAVA_OPTS="$JAVA_OPTS -XX:+UseG1GC"
    JAVA_OPTS="$JAVA_OPTS $JVM_OPTIONS"
#    JAVA_OPTS="$JAVA_OPTS -verbose:gc"
    JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails"
    JAVA_OPTS="$JAVA_OPTS -XX:+UseLargePagesInMetaspace"
    JAVA_OPTS="$JAVA_OPTS -XX:+ExplicitGCInvokesConcurrent"
    JAVA_OPTS="$JAVA_OPTS -XX:+DisableExplicitGC"
    JAVA_OPTS="$JAVA_OPTS -XX:ReservedCodeCacheSize=512m"
    JAVA_OPTS="$JAVA_OPTS -XX:-UseCodeCacheFlushing"

    JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true"
    JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true"
    JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/urandom"

    JAVA_OPTS="$JAVA_OPTS -Dorg.apache.tomcat.websocket.DEFAULT_BUFFER_SIZE=32768"

    JAVA_OPTS="$JAVA_OPTS -Dsun.lang.ClassLoader.allowArraySyntax=true "
    JAVA_OPTS="$JAVA_OPTS -Dsun.rmi.dgc.client.gcInterval=3600000"
    JAVA_OPTS="$JAVA_OPTS -Dsun.rmi.dgc.server.gcInterval=3600000"

    JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote"
    JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=$RMI_PORT"
    JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.local.only=false"
    JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
    JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.ssl=false"

fi

#####################################
## Docker WasUp Opts
#####################################
export JAVA_OPTS="$JAVA_OPTS -DCONTAINER_NAME=${CONTAINER_NAME}"
export JAVA_OPTS="$JAVA_OPTS -DCONTAINER_IP=${CONTAINER_IP}"

#####################################
## Athena Dolly Clustering
#####################################
export JAVA_OPTS="$JAVA_OPTS -Ddolly.properties=$CONF_DIR/dolly.properties"
#export JAVA_OPTS="$JAVA_OPTS -javaagent:$CATALINA_HOME/dolly/core-1.0.0-SNAPSHOT.jar"

#####################################
## Set Scouter Tomcat Agent
#####################################

bash <<EOF
sed -i -e '/obj_name/d' ${CATALINA_BASE}/scouter/conf/scouter.conf
sed -i -e '/obj_host_name/d' ${CATALINA_BASE}/scouter/conf/scouter.conf
sed -i -e '/net_collector_ip/d' ${CATALINA_BASE}/scouter/conf/scouter.conf
echo "net_collector_ip=${SCOUTER_IP}" >> ${CATALINA_BASE}/scouter/conf/scouter.conf
EOF

export JAVA_OPTS="$JAVA_OPTS -javaagent:${CATALINA_HOME}/scouter/scouter.agent.jar"
export JAVA_OPTS="$JAVA_OPTS -Dscouter.config=${CATALINA_BASE}/scouter/conf/scouter.conf"
export JAVA_OPTS="$JAVA_OPTS -Dobj_name=${SERVER}"

export JAVA_OPTS
