#!/bin/sh

umask 02;

# check user identity
OS=`/bin/uname`
if [ "$OS" = "Linux" ]
then
        WHOAMI=`/usr/bin/whoami`
else
        WHOAMI=`/usr/ucb/whoami`
fi

# fennec.fox를 자신의 계정으로 변경하세요
if [ "$WHOAMI" != "fennec.fox" ]
then
        echo 1>&2 "You should excute this program as fennec.fox!!"
        exit
fi

# path configuration 
BASE_HOME=/home/fennecfox
TOMCAT_DIR=$BASE_HOME/program/tomcat
TOMCAT_BINDIR=$TOMCAT_DIR/bin

source $BASE_HOME/.bash_profile

cd $TOMCAT_DIR

# jvm options
case "$1" in
'start')
    export JAVA_OPTS="-Xmx2048m -Xms1024m -XX:MaxMetaspaceSize=256m"
        JAVA_OPTS="$JAVA_OPTS -XX:+CMSParallelRemarkEnabled -XX:+UseConcMarkSweepGC"
        JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF-8 -Djava.security.egd=file:///dev/urandom"
        JAVA_OPTS="$JAVA_OPTS -Xloggc:$TOMCAT_DIR/logs/gc-`date +%Y%m%d%H%M%S`.log"
        JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:NumberOfGCLogFiles=10 -XX:+UseGCLogFileRotation"
        JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$TOMCAT_DIR/logs/oom-`date +%Y%m%d%H%M%S`.hprof"
        JAVA_OPTS="$JAVA_OPTS -Dsun.net.inetaddr.ttl=100"

        JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999"
        JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"


    echo "Starting Tomcat: "
    echo "Using JAVA_OPTS:       $JAVA_OPTS"
        exec "$TOMCAT_BINDIR"/startup.sh $2
        ;;

'stop')
        echo "Stopping Tomcat: "
        "$TOMCAT_BINDIR"/shutdown.sh $2
        sleep 8
PID=`./pidtomcat.pl "$2"`
if [ "$PID" != "" ];
then
        echo "Force stopping Tomcat: $PID"
        kill -9 $PID
else
        echo "Tomcat was stopped."
fi

# catalina.out log backup
CATALINA_OUT="$TOMCAT_DIR/logs/catalina.out"
TODAY=`date +%Y%m%d_%-H%M%S`
if [ -f $CATALINA_OUT ]; then
    `mv -f $CATALINA_OUT $CATALINA_OUT.$TODAY`
fi
        ;;

*)
        echo 1>&2 "Usage: $0 { start | stop }"
        exit 1
esac
exit 0
