#!/bin/sh  
###############################################################################
#
#          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-2009 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: 35 97 03
#  description: Provides access to system management functions using an \
#               industry standard web browser.
#
### BEGIN INIT INFO
# Provides: dsm_om_connsvc
# Should-Start: dataeng
# Required-Start: $localfs $remotefs $syslog
# Required-Stop: $localfs $remotefs $syslog
# Should-Stop: dataeng
# Default-Start: 3 5
# Default-Stop: 1 2
# Short-Description: DSM OM Connection Service
# Description: Provides access to system management functions using an \
#              industry standard web browser.
### END INIT INFO
#
#
###############################################################################

## Definitions
##
PROGRAM_NAME="DSM SA Connection Service"
PROGRAM_BIN=dsm_om_connsvcd
PROGRAM_DAEMON="/opt/dell/srvadmin/sbin/${PROGRAM_BIN}"
PROGRAM_LOCK_FILE="/var/lock/subsys/dsm_om_connsvc"
PROGRAM_INI_ROOT="/opt/dell/srvadmin/etc/openmanage/oma/ini"


sLibPath="" #global variable to hold LD_LIBRARY_PATH



##
# Status function
##
DaemonStatus()
{
	DAEMON=${1##*/}

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

	# Get list of pids using pidof
	PIDLIST=`/sbin/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
	PID_FILE="/var/run/${DAEMON}.pid"
	if [ -f ${PID_FILE} ];
	then
		echo "${DAEMON} is dead and ${PID_FILE} pid file exists"
		return 1
	fi

	# Check for lock file in standard location
	if [ -f ${PROGRAM_LOCK_FILE} ];
	then
		echo "${DAEMON} is dead and ${PROGRAM_LOCK_FILE} 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

## Initialize return value
##
RETVAL=0

###############################################################################
##
###############################################################################
GetOMAlibraryPATH()
{
JAVA_BIN="/bin/java" #java executable presence is checked for JRE availability in the system
currentjrepath=`grep omajvmpath ${PROGRAM_INI_ROOT}/omprv.ini`
currentjrepath=${currentjrepath/omajvmpath=/} #current jre path
currentjreversion=`grep omajvmversion ${PROGRAM_INI_ROOT}/omprv.ini` #current jre version of this form omajvmversion=1.7.0_23
bundledjrepath=`grep BundledJREPath ${PROGRAM_INI_ROOT}/omprv.ini`
bundledjrepath=${bundledjrepath/BundledJREPath=/} #bundle jre path
bundledjreversion=`grep BundledJREVersion ${PROGRAM_INI_ROOT}/omprv.ini`
bundledjreversion=${bundledjreversion/BundledJREVersion=/} #bundle jre version
JAVA_FILE="${currentjrepath}${JAVA_BIN}"

## prepare LD_LIBRARY_PATH

 if [ -f ${JAVA_FILE} ];
 then
		sLibPath=`grep omalibrarypath ${PROGRAM_INI_ROOT}/omprv.ini`
		sLibPath=${sLibPath/omalibrarypath=/}
		sLibPath=${sLibPath//,/:}
		
 else
		sed -i "s%$currentjrepath%$bundledjrepath%g" ${PROGRAM_INI_ROOT}/omprv.ini  #replace all occurences of currentjrepath with bundledjre
		sLibPath=`grep omalibrarypath ${PROGRAM_INI_ROOT}/omprv.ini`
		sLibPath=${sLibPath/omalibrarypath=/}
		sLibPath=${sLibPath//,/:}
		sed -i "s%$currentjreversion%omajvmversion=${bundledjreversion}%g" ${PROGRAM_INI_ROOT}/omprv.ini #replace currentjreversion with bundlejre version
		
 fi

}
start() 
{
	STATUS ${PROGRAM_DAEMON} >/dev/null
	RETVAL=$?
	PID_FILE="/var/run/${DAEMON}.pid"
	if [ $RETVAL -eq 1 ] && [ -f ${PID_FILE} ];
	then
		rm -f ${PID_FILE}
	fi

	if [ $RETVAL -eq 0 ];
	then
		echo -n "${PROGRAM_NAME} is already started"
		echo
		return 2
	fi

	if [ ! -r ${PROGRAM_DAEMON} ]
	then
		return 0
	fi
##
##378181,Changing the security context for the daemon,if selinux is enabled 
##  
  if [ -f '/usr/sbin/selinuxenabled' ];
	then
	  		/usr/sbin/selinuxenabled >/dev/null 2>&1
   	 	if [ $? == 0 ]
		then
			ls -aZ ${PROGRAM_DAEMON} | grep execmem_exec_t > /dev/null 2>&1
   	 		if [ $? != 0 ]
			then
		      	chcon -t unconfined_execmem_exec_t  ${PROGRAM_DAEMON} >/dev/null 2>&1
			fi
        fi
    fi 
        echo -n $"Starting ${PROGRAM_NAME}: "
        GetOMAlibraryPATH
	LD_LIBRARY_PATH=$sLibPath START_DAEMON ${PROGRAM_DAEMON} -run
	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 
		fi
	else
		# log the error
		if [ -f /lib/lsb/init-functions ];
		then
			LOG_FAILURE
		    	echo
		else
			echo -en \\033[45G
			echo 
		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}: "
	killproc ${PROGRAM_DAEMON}
	
	COUNTER=0
	STATUS ${PROGRAM_DAEMON} >/dev/null
	STATUSVAL=$?
	#wait for the process to close down if porcess is running or the pid file is there
	while  ([ ${STATUSVAL} == 1 ] || [ ${STATUSVAL} == 0 ]) && [ ${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} == 1 ] || [ ${STATUSVAL} == 0 ]
	then
		killproc ${PROGRAM_DAEMON} >/dev/null
	fi
	
	STATUS ${PROGRAM_DAEMON} >/dev/null
	STATUSVAL=$?
	
	RETVAL=0
	
	
	if ([ ${STATUSVAL} == 1 ] || [ ${STATUSVAL} == 0 ])
	then
		RETVAL=1
	fi

	# remove the lockfile 
	if [ ${STATUSVAL} == 2  ];
	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 
	fi
	else
		# log the error
		if [ -f /lib/lsb/init-functions ];
		then
			LOG_FAILURE
		    	echo
		else
			echo -en \\033[45G
			echo 
		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 $?

