#!/bin/sh

##############################################################################
#                                                                            #
# Copyright (c) 2011 by Idera rights reserved            			 #
#                                                                            #
##############################################################################
#
# chkconfig: 2345 99 99
# description: R1Soft Enterprise Console
# processname: cdp-console

### BEGIN INIT INFO
# Provides:          cdp-console
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts R1Soft CDP Console
# Description:       Starts R1Soft CDP Console
### END INIT INFO


# cdp-console control script 

INSTALLATION_DIR=/usr/sbin/r1soft-datacenter-console

# the path to the PID file
PIDFILE=/var/run/cdp-console.pid
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
CONSOLE_INSTALL=$INSTALLATION_DIR/
CONSOLE_CONFIG=$INSTALLATION_DIR/conf/server.conf

CONSOLE_BIN=${CONSOLE_INSTALL}/bin
LIB_PATH=${CONSOLE_INSTALL}/jre/lib/i386/server:${CONSOLE_INSTALL}/jre/lib/amd64/server
RUN_CONSOLE="./cdp-console ${CONSOLE_CONFIG}"

# DON'T EDIT PAST HERE #######################################################

PATH=$PATH:/sbin:/usr/sbin

ulimit -d unlimited > /dev/null 2>&1 # data segment
ulimit -f unlimited > /dev/null 2>&1 # file size
ulimit -m unlimited > /dev/null 2>&1 # max memory size
ulimit -n unlimited > /dev/null 2>&1 # open files
ulimit -s unlimited > /dev/null 2>&1 # stack size
ulimit -t unlimited > /dev/null 2>&1 # cpu time
ulimit -v unlimited > /dev/null 2>&1 # virtual memory
umask 077

export LD_LIBRARY_PATH=${LIB_PATH}

if [ -d ${CONSOLE_BIN} ]; then
	cd ${CONSOLE_BIN}
fi

ERROR=0
STATUSCODE=0
ARGV="$@"
if [ "x$ARGV" = "x" ] ; then 
    ARGS="help"
fi

checkpid() {
# check for pidfile
if [ -f $PIDFILE ] ; then
	PID=`cat $PIDFILE`
	if [ "x$PID" != "x" ] && kill -0 $PID 2>/dev/null ; then
	    STATUS="cdp-console (pid $PID) running"
	    RUNNING=1
	    STATUSCODE=0
	else
	    STATUS="cdp-console (pid $PID?) not running"
	    RUNNING=0
	    STATUSCODE=1
	fi
else
	STATUS="cdp-console (no pid file) not running"
	RUNNING=0
	STATUSCODE=3
fi
}

start() {
	if [ ! -f "${INSTALLATION_DIR}/conf/server.conf" ]; then
		echo "Error: server.conf missing. Did you specify which console product to install?"
		echo "Please contact R1Soft Support (support@r1soft.com)"
		exit 5;
	fi
	checkpid
	if [ ! -d "${INSTALLATION_DIR}/log" ]; then
		mkdir -p ${INSTALLATION_DIR}/log 2>/dev/null;
	fi
	if [ $RUNNING -eq 1 ]; then
	    echo "$0 $ARG: cdp-console (pid $PID) already running"
	else
		if $RUN_CONSOLE > /dev/null ; then
	    		echo "$0 $ARG: cdp-console started"
			
			if [ -d "/var/lock/subsys" ]; then
                                > "/var/lock/subsys/cdp-console"
                        fi
	
	
		else
	    		echo "$0 $ARG: cdp-console could not be started"
	    		ERROR=1
		fi
	fi
}

stop() {
	checkpid
	if [ $RUNNING -eq 0 ]; then
	    echo "$0 $ARG: $STATUS"
	else
		if kill $PID ; then
			count=300
			while ([ $count != 0 ]) do
				count=`/usr/bin/expr $count - 1`
				if ps -p $PID > /dev/null ; then
					echo -n .
					sleep 1
				else
					count=0
				fi
			done 
			echo ""
	    		echo "$0 $ARG: cdp-console stopped"

			if [ -f "/var/lock/subsys/cdp-console" ]; then
                                rm -f "/var/lock/subsys/cdp-console" 2>/dev/null
                        fi

		else
	    		echo "$0 $ARG: cdp-console could not be stopped"
	    		ERROR=1
		fi
	fi
}

restart() {
	checkpid
        if [ $RUNNING -eq 0 ]; then
            	echo "$0 $ARG: cdp-console not running, trying to start"
            	if $RUN_CONSOLE > /dev/null ; then
                	echo "$0 $ARG: cdp-console started"
            	else
                 	echo "$0 $ARG: cdp-console could not be started"
                 	ERROR=1
        	fi
        else
	    	stop
	    	start
        fi
}

status() {
	checkpid
        echo "$0 $ARG: $STATUS"
	exit $STATUSCODE
	    continue
}

help() {
	echo "usage: $0 (start|stop|restart|status|help)"
	cat <<EOF

start      - start cdp-console
stop       - stop cdp-console
restart    - stop cdp-console if running then start again, 
             if cdp-console is not running it is started
status     - print status
help       - this screen

EOF
	ERROR=1
}


# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
	status
	;;
  restart)
	restart
        ;;
  *)
	help
	;;
esac

exit $ERROR

