#!/bin/bash
###############################################################################
#
#          DELL INC. PROPRIETARY INFORMATION
#  This software is supplied under the terms of a license agreement or
#  nondisclosure agreement with Dell Computer Corporation and may not
#  be copied or disclosed except in accordance with the terms of that
#  agreement.
#
#  Copyright (c) 2000-2005 Dell Inc. All Rights Reserved.
#
#  Module Name:
#    Server Administrator Control Script
#
#  Abstract/Purpose:
#    Shell script to control Server Administrator
#
#  Environment:
#    Linux
#
#  Last Modified By/On/Revision:
#    $Author: $ / $Date: $ /  $Revision: $
#
#  chkconfig: 345 96 04
#  description: Provides infrastructure support for system management functions.
#
### BEGIN INIT INFO
# Provides: dsm_om_shrsvc
# Required-Start: $localfs $remotefs $syslog
# Required-Stop: $localfs $remotefs $syslog
# Default-Start: 3 4 5
# Default-Stop: 1 2
# Should-Start: dataeng
# Short-Description: DSM OM Shared Services
# Description: Systems Management infrastructure support for system management functions.
### END INIT INFO
#
#
###############################################################################

# SARA hack
#
# Stupid bash shell does not expand aliases in a shell
#
shopt -s expand_aliases



PATH=/usr/sara/sbin:/usr/sara/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11
##
# Status function
##
DaemonStatus()
{
	DAEMON=${1##*/}

	# Check for daemon name
	if [ -z ${DAEMON} ];
	then
		return 1
	fi

	# Get list of pids using pidof
	PIDLIST=`pidof -o $$ -o ${PPID} -o %PPID -x ${DAEMON}`
	if [ -n "${PIDLIST}" ];
	then
		echo "${DAEMON} (pid ${PIDLIST}) is running"
		return 0
	fi

	# Check for pid file in standard location
	PIDFILE="/var/run/${DAEMON}.pid"
	if [ -f ${PIDFILE} ];
	then
		echo "${DAEMON} is dead and /var/run pid file exists"
		return 1
	fi

	# Check for lock file in standard location
	LOCKFILE="/var/lock/subsys/${DAEMON}"
	if [ -f ${LOCKFILE} ];
	then
		echo "${DAEMON} is dead and /var/lock lock file exists"
		return 2
	fi

	echo "${DAEMON} is stopped"
	return 3
}

##
## Get the proper function library.
## Set aliases for functions and some return codes
##
if [ -f /lib/lsb/init-functions ]; then
  . /lib/lsb/init-functions
  alias START_DAEMON=start_daemon
  alias STATUS=DaemonStatus
  alias LOG_SUCCESS=log_success_msg
  alias LOG_FAILURE=log_failure_msg
  alias LOG_WARNING=log_warning_msg
elif [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
  alias START_DAEMON=daemon
  alias STATUS=status
  alias LOG_SUCCESS=success
  alias LOG_FAILURE=failure
  alias LOG_WARNING=passed
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
  alias START_DAEMON=daemon
  alias STATUS=status
  alias LOG_SUCCESS=success
  alias LOG_FAILURE=failure
  alias LOG_WARNING=passed
else
  exit 0
fi



## Source any dependent configuration here
##
. /etc/default/omsad.src


## Sanity check
##
if [ -z "$PROGRAM_NAME" ] ; then
  echo "$0: ERROR: PROGRAM_NAME is not set!"
  exit 1
fi
if [ -z "$PROGRAM_PATH" ] ; then
  echo "$0: ERROR: PROGRAM_PATH is not set!"
  exit 1
fi
if [ -z "$PROGRAM_BIN" ] ; then
  echo "$0: ERROR: PROGRAM_BIN is not set!"
  exit 1
fi
if [ -z "$INSTALL_ROOT" ] ; then
  echo "$0: ERROR: INSTALL_ROOT is not set!"
  exit 1
fi


## Definitions
##
if [ ${HOSTTYPE} = "ia64" ] ; then
	OMSA_BIT_TYPE="64"
else
	OMSA_BIT_TYPE="32"
fi

PROGRAM_DAEMON="${PROGRAM_PATH}/${PROGRAM_BIN}${OMSA_BIT_TYPE}d"
PROGRAM_LOCK_FILE="/var/lock/subsys/${PROGRAM_BIN}"


## Initialize return value
##
RETVAL=0


###############################################################################
##
###############################################################################
start() 
{
	STATUS ${PROGRAM_DAEMON} >/dev/null
	if [ $? == 0 ];
	then
		echo -n "${PROGRAM_NAME} is already started"
		echo
		return 2
	fi

	if [ ! -r ${PROGRAM_DAEMON} ]
	then
		return 0
	fi

	echo -n $"Starting ${PROGRAM_NAME}: "
	START_DAEMON ${PROGRAM_DAEMON}
	RETVAL=$?

	# check for successful daemon start
	if [ $RETVAL -eq 0  ];
	then
		if [ `touch ${PROGRAM_LOCK_FILE}` ];
		then
			# failed to set lock file
			RETVAL=1
		fi
	fi

	# check for complete success
	if [ $RETVAL -eq 0  ];
	then
		# log the success
		if [ -f /lib/lsb/init-functions ];
		then
			LOG_SUCCESS ""
			echo
		else
			echo -en \\033[45G
			echo "OK"
		fi
	else
		# log the error
		if [ -f /lib/lsb/init-functions ];
		then
			LOG_FAILURE
		    	echo
		else
			echo -en \\033[45G
			echo "FAILED"
		fi
	fi

	return $RETVAL
}

###############################################################################
##
###############################################################################
stop() {
	# Check if the daemon is running
	STATUS ${PROGRAM_DAEMON} >/dev/null
        if [ $? == 3 ];
        then
		echo -n "${PROGRAM_NAME} is already stopped"
		echo
		return 2
        fi
	echo -n $"Shutting down ${PROGRAM_NAME}: "
	PIDLIST=`pidof -o $$ -o ${PPID} -o %PPID -x ${PROGRAM_DAEMON}`
	kill -15 ${PIDLIST}
	#killproc ${PROGRAM_DAEMON}
	
	COUNTER=0
	STATUS ${PROGRAM_DAEMON} >/dev/null
	STATUSVAL=$?
	while [ ${STATUSVAL} != 3 ] && [ ${COUNTER} -le 5 ]
	do
		let COUNTER=${COUNTER}+1
		#The service is NOT completely stopped yet.
		#Wait 5 seconds and then check the status again
		sleep 5
		STATUS ${PROGRAM_DAEMON} >/dev/null
		STATUSVAL=$?
	done
	#if after 5 retries it is still not stopped
	#kill the process again
	if [ ${STATUSVAL} != 3 ]
	then
		kill -9 ${PIDLIST}
		#killproc ${PROGRAM_DAEMON} >/dev/null
	fi
	
	STATUS ${PROGRAM_DAEMON} >/dev/null
	STATUSVAL=$?
	
	RETVAL=0
	
	if [ ${STATUSVAL} != 3 ]
	then
		RETVAL=1
	fi

	# check for successful daemon stop
	if [ $RETVAL -eq 0  ];
	then
		if [ `rm -f ${PROGRAM_LOCK_FILE}` ];
		then
			# failed to clear lock file
			RETVAL=1
		fi
	fi

	# check for complete success
	if [ $RETVAL -eq 0  ];
	then
		# log the success
		if [ -f /lib/lsb/init-functions ];
		then
			LOG_SUCCESS ""
			echo
		else
			echo -en \\033[45G
			echo "OK"
	fi
	else
		# log the error
		if [ -f /lib/lsb/init-functions ];
		then
			LOG_FAILURE
		    	echo
		else
			echo -en \\033[45G
			echo "FAILED"
		fi
	fi

	echo
	[ $RETVAL -eq 0 ] && rm -f ${PROGRAM_LOCK_FILE}
	return $RETVAL
}

###############################################################################
##
###############################################################################
restart() {
	stop
	start
}

###############################################################################
##
###############################################################################
reload() {
	stop
	start
}

###############################################################################
##
###############################################################################
rhstatus() {
	STATUS ${PROGRAM_DAEMON}
}

###############################################################################
## MAIN
###############################################################################
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
  	restart
	;;
  reload)
  	reload
	;;
  status)
  	rhstatus
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|status}"
	exit 1
esac

###############################################################################
## DONE
###############################################################################
exit $?

