#!/bin/sh
#
# CMS Control Script

### BEGIN INIT INFO
# Provides: cms
# X-UnitedLinux-Should-Start: alerts $remote_fs $network
# Required-Start: alerts sshd
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Content Management System Server
### END INIT INFO

PRODUCT_HOME=/opt/tandbergtv/cms
ACS_SERVER_HOME=/usr/local/acsserver
JAVA_HOME=/usr/java/default/bin
JSTACK_CMD=$JAVA_HOME/jstack

#define the user under which jboss will run, or use RUNASIS to run as the current user
JBOSSUS=${JBOSSUS:-"nobody"}

if [ "$JBOSSUS" = "RUNASIS" ]; then
  SUBIT=""
else
   SUBIT="su $JBOSSUS -c "
fi

PROGNAME=`basename $0`

#define where jboss is - this is the directory containing directories log, bin, conf etc
JBOSS_HOME=${JBOSS_HOME:-"$PRODUCT_HOME/jboss"}

if [ ! -d "$JBOSS_HOME" ]; then
  echo JBOSS_HOME does not exist as a valid directory : $JBOSS_HOME
  exit 1
fi

# Setup the java endorsed dirs
JBOSS_ENDORSED_DIRS="$JBOSS_HOME/lib/endorsed"

#define the classpath for the shutdown class
JBOSSCP=${JBOSSCP:-"$JBOSS_HOME/bin/shutdown.jar:$JBOSS_HOME/client/jnet.jar"}

# Setup the classpath
JBOSS_BOOT_CLASSPATH="$JBOSS_HOME/bin/run.jar"
JBOSS_CLASSPATH="$JBOSS_BOOT_CLASSPATH:$PRODUCT_HOME/workflow/lib:/usr/local/platform/lib/SignatureVerifier.jar"

# Setup properties
JAVA_OPTS="$JAVA_OPTS -Dprogram.name=$PROGNAME -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10008 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
JAVA_OPTS="$JAVA_OPTS -Dcom.tandbergtv.cms.product.dir=$PRODUCT_HOME -Xmx48g -XX:+HeapDumpOnOutOfMemoryError -XX:MaxPermSize=512m -XX:StackShadowPages=20 -Dcom.tandbergtv.workflow.message.origin=true"
JAVA_OPTS="$JAVA_OPTS -Dnetworkaddress.cache.ttl=0 -Dnetworkaddress.cache.negative.ttl=0"

CMD_START="java -server $JAVA_OPTS -Djava.endorsed.dirs="$JBOSS_ENDORSED_DIRS" -classpath "$JBOSS_CLASSPATH" org.jboss.Main "$1" --configuration=standard -b 0.0.0.0"
CMD_STOP="java -server -classpath $JBOSSCP org.jboss.Shutdown --shutdown"

############ VCS Check Logic [START] ############
VCSBIN="/opt/VRTSvcs/bin/"
HA_CURRENT_SYSTEM=`hostname -s`
SERVICE=`echo $0 | awk -F"/" '{print $NF}'`

if [[ $2 == "--VCSOverride" ]] || [[ -z `/etc/init.d/vcs status | grep running...` ]] ; then
############# VCS Check Logic [END] #############
#
########## Original Case Logic [START] ##########

    case "$1" in
    start)
        pid=$(ps axww | grep "[o]rg.jboss.Main" | sed -e 's/^\s*//' | cut -d' ' -f1)
        if [ ! -z "${pid}" ] ; then
            echo "Content Management System is already running"
        else
            echo "Starting Content Management System"
            cd $JBOSS_HOME/bin
            if [ -z "$SUBIT" ] ; then
                $ACS_SERVER_HOME/scripts/AdiNameService start
                sleep 2
                eval env LANG=en_US.UTF-8 $CMD_START >/dev/null 2>&1 &
            else
                if [ -d "$ACS_SERVER_HOME" ] ; then # Check if ACSServer is installed
                    $ACS_SERVER_HOME/scripts/AdiNameService start
                    sleep 2
                fi
                $SUBIT "env LANG=en_US.UTF-8 $CMD_START >/dev/null 2>&1 &"
            fi
        fi
        # echo pids to file
        echo `ps axww | grep "[o]rg.jboss.Main" | sed -e 's/^\s*//' | cut -d' ' -f1` > /etc/init.d/cms.pid
        echo `ps axww | grep "[o]rg.jacorb.naming.NameServer" | sed -e 's/^\s*//' | cut -d' ' -f1` > /etc/init.d/cms-adinameservice.pid
        ;;
    stop)
        echo "Stopping Content Management System"
        pid=$(ps axww | grep "[o]rg.jboss.Main" | sed -e 's/^\s*//' | cut -d' ' -f1)
        su nobody -c "$JSTACK_CMD -l $pid" > $PRODUCT_HOME/cms_stackdump.txt
        if [ -z "$SUBIT" ] ; then
            $CMD_STOP >/dev/null 2>&1 &
        else
            $SUBIT $CMD_STOP >/dev/null 2>&1 &
        fi

        sleep 5

        # Forcefully kill the process in case if it is still running0o-
        if [ ! -z "${pid}" ] ; then
             kill -9 $pid
        fi

        if [ -d "$ACS_SERVER_HOME" ] ; then #Check if ACSServer is installed
             # Stop ADI name service
             $ACS_SERVER_HOME/scripts/AdiNameService stop
             sleep 2

             # Forcefully kill the process in case if it is still running
             pid=$(ps axww | grep "[o]rg.jacorb.naming.NameServer" | sed -e 's/^\s*//' | cut -d' ' -f1)
             if [ ! -z "${pid}" ] ; then
                     kill -9 $pid
             fi
        fi
        
        # echo "Purging temporary jboss files... This may take a while"
        if [[ "$(ls -A /opt/tandbergtv/cms/jboss/server/standard/tmp)" ]] ; then
        `find /opt/tandbergtv/cms/jboss/server/standard/tmp/* -name '*' -print0 | xargs -0 rm -rf`
        fi
        # echo "Purging complete"
        
        #clear pid files
        echo "" > /etc/init.d/cms.pid
        echo "" > /etc/init.d/cms-adinameservice.pid
        ;;
    status)
        pid=$(ps axww | grep "[o]rg.jboss.Main" | sed -e 's/^\s*//' | cut -d' ' -f1)
        if [ -z "${pid}" ] ; then
            echo "cms service is stopped"
        else
            echo "cms service (pid " $pid") is running..."
        fi

        if [ -d "$ACS_SERVER_HOME" ] ; then #Check if ACSServer is installed
            pid=$(ps axww | grep "[o]rg.jacorb.naming.NameServer" | sed -e 's/^\s*//' | cut -d' ' -f1)
            if [ -z "${pid}" ] ; then
                echo "ADI NameServer is stopped"
            else
                echo "ADI NameServer (pid " $pid") is running..."
            fi
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "usage: $0 (start|stop|restart|help)"
        ;;
    esac

########### Original Case Logic [END] ###########
#
############# VCS Case Logic [START] ############
else
    case "$1" in
        start)
            echo "VCS is currently managing the $SERVICE service, Attempting to $1 $SERVICE on $HA_CURRENT_SYSTEM."
            `$VCSBIN/hares -online $SERVICE -sys $HA_CURRENT_SYSTEM &`
            ;;
        stop)
            echo "VCS is currently managing the $SERVICE service, Attempting to $1 $SERVICE on $HA_CURRENT_SYSTEM."
            `$VCSBIN/hares -offline -ignoreparent $SERVICE -sys $HA_CURRENT_SYSTEM &`
            ;;
        status)
            service $SERVICE status --VCSOverride
            ;;
        restart)
            echo "VCS is currently managing the $SERVICE service and restart is not available, please stop and start the service manually."
    esac
fi
############## VCS Case Logic [END] #############
