#!/bin/sh

. ./env.sh


DATE=`date +%Y%m%d%H%M%S`

# ------------------------------------
PID=`ps -ef | grep java | grep "$SERVER_NAME" | awk '{print $2}'`
echo $PID

if [ e$PID != "e" ]
then
    echo "JBOSS($SERVER_NAME) is already RUNNING..."
    exit;
fi
# ------------------------------------
UNAME=`id -u -n`
if [ e$UNAME != "e$JBOSS_USER" ]
then
    echo "$JBOSS_USER USER to start JBoss SERVER - $SERVER_NAME..."
    exit;
fi
# ------------------------------------


# Check Logging Directory
if [ -d $SERVER_HOME/$SERVER_NAME/log ]; then
  echo ""
else
  echo "Directory does not exists. Now create Logging Directory"
  mkdir $SERVER_HOME/$SERVER_NAME/log
  echo "Logging Directory is $SERVER_HOME/$SERVER_NAME/log"
fi

# Check GC Logging Directory
if [ -d $SERVER_HOME/$SERVER_NAME/gclog ]; then
  echo ""
else
  echo "Directory does not exists. Now create Logging Directory"
  mkdir $SERVER_HOME/$SERVER_NAME/gclog
  echo "Logging Directory is $SERVER_HOME/$SERVER_NAME/gclog"
fi



#mv $SERVER_HOME/$SERVER_NAME/$SERVER_NAME.out $SERVER_HOME/$SERVER_NAME/log/$SERVER_NAME.$DATE
mv $SERVER_HOME/$SERVER_NAME/gclog/gc.log $SERVER_HOME/$SERVER_NAME/gclog/gc.log.$DATE

#rm -rf $SERVER_HOME/$SERVER_NAME/data/*
rm -rf $SERVER_HOME/$SERVER_NAME/tmp/*
rm -rf $SERVER_HOME/$SERVER_NAME/work/*


################## JMX Config ###################
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMX_CONSOLE_PORT"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
JAVA_OPTS="$JAVA_OPTS -Djava.rmi.server.hostname=$DOMAIN_IP"


################## Jennifer Config ##############
JENNIFER_OPTS="-Xbootclasspath/p:/home02/apmagent/agent/lwst.jdk.jar:/home02/apmagent/agent/lwst.boot.jar"
JENNIFER_OPTS="$JENNIFER_OPTS -Djennifer.config=/home02/apmagent/agent/$JENNIFER_CONFIG_NAME.conf"
JENNIFER_OPTS="$JENNIFER_OPTS -javaagent:/home02/apmagent/agent/lwst.javaagent.jar"
JENNIFER_OPTS="$JENNIFER_OPTS -XX:CompileCommand=exclude,org/apache/jennifer/bcel/classfile/Attribute,readAttribute"

#JAVA_OPTS="$JAVA_OPTS $JENNIFER_OPTS"

################ Jboss Engine Starting ###########
#nohup $JBOSS_HOME/bin/run.sh -c $SERVER_NAME -b $DOMAIN_IP >> $SERVER_HOME/$SERVER_NAME/$SERVER_NAME.out 2>&1 &
nohup $JBOSS_HOME/bin/run.sh -c $SERVER_NAME -b $DOMAIN_IP 2>&1 | /usr/local/sbin/cronolog -S $SERVER_HOME/$SERVER_NAME/$SERVER_NAME.out $SERVER_HOME/$SERVER_NAME/log/$SERVER_NAME.%Y%m%d235959 >> /dev/null &

# ------------------------------------
if [ e$1 = "enotail" ]
then
    echo "Starting... $SERVER_NAME"
    exit;
fi
# ------------------------------------

#sh /opt/was/servers/eap51_svr_11/bin/deploy_check.sh > /dev/null 2>&1 &
