#!/bin/sh
# $Id: //depot/prod/davit/Rdavit2.0/src/bin/ssc_server#1 $
#

### BEGIN INIT INFO
# Provides: Storage Services Connector for SAP
# Required-Start:
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the Storage Services Connector server daemon
### END INIT INFO

# All paths are relative to here
#
INSTALL_DIR=/opt/NetApp/ssc
cd ${INSTALL_DIR}

if [ -z "${JAVA_HOME}" ] ; then
    echo "JAVA_HOME must be defined"
    exit 1;
fi

SERVICE=`basename $0`

# use the server classes path for the server start boot path stuff
WEB_INF_PATH=${INSTALL_DIR}/webapp/server/WEB-INF
LIB_PATH=$WEB_INF_PATH/lib

#CP=${INSTALL_DIR}/etc:${INSTALL_DIR}/properties/cli/ssc_server:$WEB_INF_PATH:$WEB_INF_PATH/classes:$WEB_INF_PATH/classes/resources
CP=${INSTALL_DIR}/etc:${INSTALL_DIR}/properties/cli/ssc_server:$WEB_INF_PATH:$WEB_INF_PATH/classes:$WEB_INF_PATH/classes/resources

# Define just the classpath required to boot jetty
files="$LIB_PATH/jetty-6.1.3.jar"
files="$files $LIB_PATH/jetty-util-6.1.3.jar"
files="$files $LIB_PATH/log4j-1.2.15.jar"
files="$files $LIB_PATH/slf4j-api-1.3.1.jar"
files="$files $LIB_PATH/slf4j-jdk14-1.3.1.jar"
files="$files $LIB_PATH/netapp-common.jar"
files="$files $LIB_PATH/commons-cli-20061128.jar"
files="$files $LIB_PATH/spring-2.0.4.jar"
files="$files $LIB_PATH/commons-logging-api-1.0.4.jar"
files="$files $LIB_PATH/commons-lang-2.2.jar"
files="$files $LIB_PATH/servlet-api-2.5-6.1.3.jar"

# Extend files list with JDBC_LIB to given jar file for the crash database driver
if [ ! -z "${JDBC_LIB}" ] ; then
    files="$files $JDBC_LIB"
fi

# or we'll attempt to get it from dbmcli (for MaxDB)
SAPLIB=`dbmcli dbm_getpath IndepProgPath | grep "^/"`
if [ $? -eq 0 ] ; then
    SAPLIB="${SAPLIB}/runtime/jar/sapdbc.jar"
    echo "Using SAP JDBC driver from ${SAPLIB}"
    files="$files ${SAPLIB}"
else
    echo "No SAP JDBC driver found; MaxDB interactions will be disabled."
fi

for file in $files
do
  CP=$CP:$file
done

CP=${INSTALL_DIR}/etc:$CP

# If shell is killed the java process will keep running as zombie and user will see no message.
# To kill java process, send it a TERM instead of this shell.
trap /bin/true TERM

umask 0000

PID_DIR=./var/run
LOG_DIR=./log
LOG=$LOG_DIR/ssc_server_console.log
mkdir -p $LOG_DIR

memcount=`echo $JVM_ARGS | grep -c "\-Xmx"`

if [ $memcount -ne 1  ] ; then
   JVM_ARGS="$JVM_ARGS -Xmx128m"
fi

if [ -n "$DEBUG_PORT" ] ; then
    JVM_ARGS="$JVM_ARGS -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=$DEBUG_PORT,timeout=20000"
fi

MAIN=com.netapp.davit.server.DavitServerMain

java $JVM_ARGS -cp $CP -Dcom.sun.management.jmxremote -Dinstall.dir=$INSTALL_DIR -Dcommon.dir=$INSTALL_DIR $MAIN status > $LOG 2>&1 

spid=`sed -ne 's/SERVER-01112.*PID \([0-9]*\)\./\1/p' $LOG`

if [ "$spid" == "" ] ; then
  spid=`ps -ef | grep $MAIN | grep -v grep | awk '{print $2}'`
fi


if [ "$spid" != "" ] ; then
	isup=`ps --no-headers -p $spid | wc -l`
else 
	isup=0
fi


if [ "$1" = "start" ] ; then
   if [ "$isup" != "0" ] ; then
         echo "Server is running with process id $spid."
         exit 1
   fi
elif [ "$1" = "stop" ] ; then
   if [ "$isup" = "0" ] ; then
         ps -ef | grep $MAIN | grep -v grep | awk '{print $2}' 	| xargs -i kill -9 {} 
	 rm $PID_DIR/* >> /dev/null 2>&1
         echo "Server is stopped."
         exit 0
   fi
elif [ "$1" = "status" ] ; then
   if [ "$isup" = "1" ] ; then
         echo "Server is running with process id $spid."
   else
	 echo "Server is stopped."
   fi
   exit 0
else
   echo "$SERVICE start|stop|status"
   exit 0
fi
  	
java $JVM_ARGS -cp $CP -Dcom.sun.management.jmxremote -Dinstall.dir=$INSTALL_DIR -Dcommon.dir=$INSTALL_DIR $MAIN $@ > $LOG 2>&1 &

pid="$!"


while ps -p $pid | grep $pid >/dev/null 2>&1
do
    sleep 1
    # grep for the log entry that indicates the server has started.
    if [ "$1" = "start" ] ; then
    	msg=`grep "SERVER-01102" $LOG`
    # grep for the log entry that indicates the server has stopped
    elif [ "$1" = "stop" ] ; then
   	msg=`grep "SERVER-01111" $LOG`
    fi
    if [ -n "$msg" ] ; then
        echo "$msg"
        exit 0
    fi
done
if [ "$1" = "stop" ] ; then
         ps -ef | grep $MAIN | grep -v grep | awk '{print $2}'  | xargs -i kill -9 {}
         rm $PID_DIR/* >> /dev/null 2>&1
         echo "Server is stopped."
         exit 0
fi
#echo "${1} of SSC server failed."
exit 1
