#!/bin/sh

#######################################
#        JBoss Default Setting        #
#######################################

export DATE=`date +%Y%m%d%H%M%S`
export LANG=ko_KR.utf8

export JAVA_HOME=~/java
export JBOSS_HOME=~/wildfly
export DOMAIN_BASE=~/wildfly
export SERVER_NAME=golf-svr-11
export SERVER_HOME=$DOMAIN_BASE/$SERVER_NAME


#######################################
#          Configration File          #
#######################################

#export CONFIG_FILE=standalone-ha.xml
export CONFIG_FILE=standalone-full.xml

export HOST_NAME=slave01
export NODE_NAME=$SERVER_NAME
export PORT_OFFSET=100

export JBOSS_USER=asmanager

#######################################
#            Bind Address             #
#######################################

export BIND_ADDR=192.168.0.143
#export BIND_ADDR=0.0.0.0

export MULTICAST_ADDR=230.1.11.175
export JMS_MULTICAST_ADDR=231.2.22.175
export MODCLUSTER_MULTICAST_ADDR=224.0.1.105

export MGMT_ADDR=192.168.0.143

export CONTROLLER_IP=$MGMT_ADDR
let CONTROLLER_PORT=9990+$PORT_OFFSET
export CONTROLLER_PORT


#######################################
#            port usage               #
#######################################


#=============================
#ex) export HTTP_CUSTOM_PORT=8080
#ex) export AJP_CUSTOM_PORT=8309
#=============================

export HTTP_DEFAULT_PORT=8080
export AJP_DEFAULT_PORT=8009

if [ "x$HTTP_CUSTOM_PORT" = "x" ]; then
    HTTP_PORT=$HTTP_DEFAULT_PORT
    AJP_PORT=$AJP_DEFAULT_PORT

    let HTTP_FINAL_PORT=$HTTP_PORT+$PORT_OFFSET
    let AJP_FINAL_PORT=$AJP_PORT+$PORT_OFFSET

    export HTTP_FINAL_PORT
    export AJP_FINAL_PORT

else

    if [ "x$AJP_CUSTOM_PORT" = "x" ]; then

        AJP_PORT=$AJP_DEFAULT_PORT
        
        let HTTP_PORT=$HTTP_CUSTOM_PORT-$PORT_OFFSET
        let AJP_FINAL_PORT=$AJP_PORT+$PORT_OFFSET

        export HTTP_FINAL_PORT=$HTTP_CUSTOM_PORT
        export AJP_FINAL_PORT

    else
        let HTTP_PORT=$HTTP_CUSTOM_PORT-$PORT_OFFSET
        let AJP_PORT=$AJP_CUSTOM_PORT-$PORT_OFFSET

        export HTTP_FINAL_PORT=$HTTP_CUSTOM_PORT
        export AJP_FINAL_PORT=$AJP_CUSTOM_PORT
    fi
fi

export HTTP_PORT
export AJP_PORT

export JGROUP_TCP_PORT=7600
let JGROUP_TCP_PORT=7600+$PORT_OFFSET
export JGROUP_TCP_PORT

export JMXREMOTE_TCP_PORT=5999
let JMXREMOTE_TCP_PORT=5999+$PORT_OFFSET
export JMXREMOTE_TCP_PORT

export JBOSS_JGROUPS_STACK=udp
export JBOSS_JGROUPS_TCPPING_INITIAL_HOSTS=192.168.0.143[$JGROUP_TCP_PORT],192.168.0.144[$JGROUP_TCP_PORT]
export JBOSS_JGROUPS_TCPPING_NUM_INITIAL_MEMBERS=2

export LAUNCH_JBOSS_IN_BACKGROUND=true


######################################################
##### Logging Directory Setup
######################################################

export LOG_HOME=$SERVER_HOME/logs
export LOG_DIR=${LOG_HOME}/log
export GC_LOG_DIR=${LOG_HOME}/gclog
export HEAP_DUMP_DIR=${LOG_HOME}/heaplog
export LOG_LEVEL=INFO


##### JBoss System module and User module directory #####
export JBOSS_MODULEPATH=$JBOSS_HOME/modules:$JBOSS_HOME/modules.ext

# JVM Options : Server
export JAVA_OPTS="-server $JAVA_OPTS"

# JVM Options : Memory 
export JAVA_OPTS="$JAVA_OPTS -Xms1024m -Xmx1024m"
export JAVA_OPTS="$JAVA_OPTS -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m"

export JAVA_OPTS="$JAVA_OPTS -XX:+UseG1GC"
export JAVA_OPTS="$JAVA_OPTS -XX:+UnlockDiagnosticVMOptions"
export JAVA_OPTS="$JAVA_OPTS -XX:+G1SummarizeConcMark"
export JAVA_OPTS="$JAVA_OPTS -XX:InitiatingHeapOccupancyPercent=35"
export JAVA_OPTS="$JAVA_OPTS -XX:ConcGCThreads=2"
export JAVA_OPTS="$JAVA_OPTS -XX:+PrintFlagsFinal"
export JAVA_OPTS="$JAVA_OPTS -XX:+PrintReferenceGC"

export JAVA_OPTS="$JAVA_OPTS -XX:+UseLargePagesInMetaspace"

#export JAVA_OPTS="$JAVA_OPTS -XX:+UseParallelGC"

export JAVA_OPTS="$JAVA_OPTS -verbose:gc"
export JAVA_OPTS="$JAVA_OPTS -Xloggc:$GC_LOG_DIR/gc_$DATE.log"
export JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails"
export JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCTimeStamps"
#export JAVA_OPTS="$JAVA_OPTS -XX:+PrintAdaptiveSizePolicy"

export JAVA_OPTS="$JAVA_OPTS -XX:+ExplicitGCInvokesConcurrent"
export JAVA_OPTS="$JAVA_OPTS -XX:+DisableExplicitGC"
export JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError"
export JAVA_OPTS="$JAVA_OPTS -XX:HeapDumpPath=$HEAP_DUMP_DIR/$SERVER_NAME.hprof.$DATE"

export JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true"
export JAVA_OPTS="$JAVA_OPTS -Dorg.jboss.resolver.warning=true"
export JAVA_OPTS="$JAVA_OPTS -Dsun.rmi.dgc.client.gcInterval=3600000 "
export JAVA_OPTS="$JAVA_OPTS -Dsun.rmi.dgc.server.gcInterval=3600000"
export JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true"
export JAVA_OPTS="$JAVA_OPTS -Djboss.modules.system.pkgs=org.jboss.byteman,scouter,org.jboss.logmanager"

# Linux Large Page Setting
#export JAVA_OPTS=" $JAVA_OPTS  -XX:+UseLargePages "

#for darwin
export JBOSS_BASE_DIR="$DOMAIN_BASE/$SERVER_HOME"

export JAVA_OPTS="$JAVA_OPTS -Djboss.server.base.dir=$DOMAIN_BASE/$SERVER_NAME"
export JAVA_OPTS="$JAVA_OPTS -Djboss.server.log.dir=$LOG_DIR"
export JAVA_OPTS="$JAVA_OPTS -Djboss.socket.binding.port-offset=$PORT_OFFSET"

export JAVA_OPTS="$JAVA_OPTS -Djboss.node.name=$NODE_NAME"
export JAVA_OPTS="$JAVA_OPTS -Djboss.server.log.dir=$LOG_DIR"
export JAVA_OPTS="$JAVA_OPTS -Djboss.server.base.dir=$DOMAIN_BASE/$SERVER_NAME"
export JAVA_OPTS="$JAVA_OPTS -Djboss.bind.address=$BIND_ADDR"
export JAVA_OPTS="$JAVA_OPTS -Djboss.bind.address.management=$MGMT_ADDR"
export JAVA_OPTS="$JAVA_OPTS -Djboss.socket.binding.port-offset=$PORT_OFFSET"

export JAVA_OPTS="$JAVA_OPTS -Djboss.default.jgroups.stack=$JBOSS_JGROUPS_STACK"
export JAVA_OPTS="$JAVA_OPTS -Djboss.default.jgroups.stack.tcp.tcpping.initial_hosts=$JBOSS_JGROUPS_TCPPING_INITIAL_HOSTS"
export JAVA_OPTS="$JAVA_OPTS -Djboss.default.jgroups.stack.tcp.tcpping.num_initial_members=$JBOSS_JGROUPS_TCPPING_NUM_INITIAL_MEMBERS"
export JAVA_OPTS="$JAVA_OPTS -Djgroups.bind_addr=${BIND_ADDR}"
export JAVA_OPTS="$JAVA_OPTS -Djgroups.tcp.bind_port=${JGROUP_TCP_PORT}"

export JAVA_OPTS="$JAVA_OPTS -Djboss.default.multicast.address=$MULTICAST_ADDR"
export JAVA_OPTS="$JAVA_OPTS -Djboss.messaging.group.address=$JMS_MULTICAST_ADDR"
export JAVA_OPTS="$JAVA_OPTS -Djboss.modcluster.multicast.address=$MODCLUSTER_MULTICAST_ADDR"

#export JAVA_OPTS=" $JAVA_OPTS -Djboss.mod_cluster.excludedContexts=admin-console,invoker,jbossws,jmx-console,juddi,web-console,images,test"

export JAVA_OPTS="$JAVA_OPTS -Djboss.http.port=$HTTP_PORT"
export JAVA_OPTS="$JAVA_OPTS -Djboss.http.final.port=$HTTP_FINAL_PORT"
export JAVA_OPTS="$JAVA_OPTS -Djboss.ajp.port=$AJP_PORT"
export JAVA_OPTS="$JAVA_OPTS -Djboss.ajp.final.port=$AJP_FINAL_PORT"

export JAVA_OPTS="$JAVA_OPTS -DLOG_LEVEL=$LOG_LEVEL"
export JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8"

export JAVA_OPTS="$JAVA_OPTS -Xbootclasspath/p:/home/asmanager/wildfly/modules/system/layers/base/org/jboss/logmanager/main/jboss-logmanager-2.0.4.Final.jar"
export JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.jboss.logmanager.LogManager"
export JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote"
export JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=$JMXREMOTE_TCP_PORT"
export JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.ssl=false"
export JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"

export JAVA_OPTS=" $JAVA_OPTS -Dserver.mode=local"

# Use log4j in application
export JAVA_OPTS=" $JAVA_OPTS -Dorg.jboss.as.logging.per-deployment=false "


#################################################
## Session Clustering Solution - Athena Dolly   #
#################################################
#export JAVA_OPTS="$JAVA_OPTS -Ddolly.properties=/opt/dolly-agent/dolly.properties"
#export JAVA_OPTS="$JAVA_OPTS -javaagent:/opt/dolly-agent/lib/core-1.0.0-SNAPSHOT.jar"

#################################################
## Application Performance Monitoring - Scouter #
#################################################
#export JAVA_OPTS="$JAVA_OPTS -javaagent:/opt/apm/scouter/agent.java/scouter.agent.jar"
#export JAVA_OPTS="$JAVA_OPTS -Dscouter.config=/opt/apm/scouter/agent.java/conf/jboss_standalone_ha_11_143.conf"


echo "================================================"
echo "JAVA_HOME=$JAVA_HOME"
echo "JBOSS_HOME=$JBOSS_HOME"
echo "DOMAIN_BASE=$DOMAIN_BASE"
echo "SERVER_NAME=$SERVER_NAME"
echo "CONFIG_FILE=$CONFIG_FILE"
echo "BIND_ADDR=$BIND_ADDR"
echo "PORT_OFFSET=$PORT_OFFSET"
echo "MULTICAST_ADDR=$MULTICAST_ADDR"
echo "CONTROLLER=$CONTROLLER_IP:$CONTROLLER_PORT"
echo "HTTP_FINAL_PORT=$HTTP_FINAL_PORT"
echo "AJP_FINAL_PORT=$AJP_FINAL_PORT"
echo "================================================"
