#!/bin/bash
BASE_DIR=$(dirname $0)
if [ $BASE_DIR == "." ] ; then
    SCRIPT_DIR=$(pwd)
else
    SCRIPT_DIR=$(pwd)/$BASE_DIR
fi
. $SCRIPT_DIR/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 "Tomcat ($SERVER_NAME) is already RUNNING..."
exit;
fi

# ------------------------------------
UNAME=`id -u -n`
if [ e$UNAME != "e$COMP_USER" ]
then
echo "$COMP_USER USER to start this SERVER! - $SERVER_NAME..."
exit;
fi

# ------------------------------------
if [ ! -d $CATALINA_BASE/temp ]
then
echo "temp directory is not exist. create temp directory."
mkdir -p $CATALINA_BASE/temp
fi

if [ ! -d $CATALINA_BASE/logs/tmp/ ]
then
echo "log_temp directory is not exist. create log_temp directory."
mkdir -p $CATALINA_BASE/logs/tmp
fi


# ------------------------------------
mv ${LOG_DIR}/$SERVER_NAME.out $LOG_DIR/tmp/$SERVER_NAME.out.$DATE

nohup $CATALINA_HOME/bin/catalina.sh run >> ${LOG_DIR}/$SERVER_NAME.out 2>&1 &
#nohup $CATALINA_HOME/bin/catalina.sh run >> ${LOG_DIR}/$SERVER_NAME.out 2>&1 "| /usr/sbin/rotatelogs ${LOG_DIR}/$SERVER_NAME.out.%Y%m%d 86400" >> /dev/null &

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

# ------------------------------------
#$CATALINA_BASE/bin/tail.sh
