#!/bin/sh
# Startup script for msgagt
#
# chkconfig: 2345 95 05
# description: Starts and stops promise msgagt daemons \
#	       used to provide promise network services.
# processname: msgagt

# Source function library.
if [ -e /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
fi

[ -f /usr/sbin/msgagt ] || exit 0

RETVAL=0
prog="msgagt"

EXENAME=msgagtd

# Source networking configuration.
#. /etc/sysconfig/network

# First set up a default search path.
export PATH="/sbin:/usr/sbin:/bin:/usr/bin:"

# Check if $pid (could be plural) are running
checkpid() {
	while [ "$1" ]; do
	   [ -d /proc/$1 ] && return 0
	   shift
	done
	return 1
}

# A function to start a program.
daemon() {
	# Test syntax.
	nicelevel=0
	while [ "$1" != "${1##-}" -o "$1" != "${1##+}" ]; do
	  case $1 in
	    '')    echo '$0: Usage: daemon [+/-nicelevel] {program}'
	           return 1;;
	    -*|+*) nicelevel=$1
	           shift
		   ;;
	     *)    nicelevel=0
	     	   ;;
	  esac
	done

	# Save basename.
	base=`basename $1`

	# See if it's already running.
	pidlist=`pidofproc $1`
	
	pid=
	for apid in $pidlist ; do
	   [ -d /proc/$apid ] && pid="$pid $apid"
	done

	[ -n "$pid" ] && return

	# And start it up.
	success "$base startup"
	msgagtd & >/dev/null 2>&1
}

# A function to stop a program.
killproc() {

	RC=0
	# Test syntax.
	if [ $# = 0 ]; then
		echo "Usage: killproc {program} [signal]"
		return 1
	fi

	notset=0
	# check for second arg to be kill level
	if [ "$2" != "" ] ; then
		killlevel=$2
	else
		notset=1
		killlevel="-9"
	fi

	# Save basename.
	base=`basename $1`

	# Find pid.
	pidlist=`pidofproc $1`

	pid=
	for apid in $pidlist ; do
	   [ -d /proc/$apid ] && pid="$pid $apid"
	done

	# Kill it.
	if [ "$pid" != "" ] ; then
		if [ "$notset" = "1" ] ; then
			if checkpid $pid 2>&1; then
				# TERM first, then KILL if not dead
				kill -TERM $pid
				#=========================
				usleep 100000	#sleep 0.1 second
				if checkpid $pid >/dev/null 2>&1 ; then
					sleep 1
					if checkpid $pid >/dev/null 2>&1 ; then
						kill -KILL $pid
						usleep 100000
					fi
				fi
				#==========================
			fi

			checkpid $pid >/dev/null 2>&1
			RC=$?
			[ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
  			RC=$((! $RC))
			# use specified level only
		else
			if checkpid $pid >/dev/null 2>&1; then
				kill $killlevel $pid
				RC=$?
				[ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
			fi
		fi
	else
		failure "$base shutdown"
	fi

        # Remove pid file if any.
	if [ "$notset" = "1" ]; then
            rm -f /var/run/$base.pid
	fi
	return $RC
}

# A function to find the pid of a program.
pidofproc() {
	base=`basename $1`

	# Test syntax.
	if [ $# = 0 ] ; then
		echo "Usage: pidofproc {program}"
		return 1
	fi

	# First try "/var/run/*.pid" files
	if [ -f /var/run/${base}.pid ] ; then
	        pid=`cat /var/run/${base}.pid | { read foo ; echo $foo ; }`
	        if [ "$pid" != "" ] ; then
	                echo $pid
	                return 0
	        fi
	fi

	# Next try "pidof"
	pid=`pidof -o $$ -o $PPID -o %PPID -x ${base}`
	if [ "$pid" != "" ] ; then
	        echo $pid
	        return 0
	fi
}

status() {
	base=`basename $1`

	# Test syntax.
	if [ $# = 0 ] ; then
		echo "Usage: status {program}"
		return 1
	fi

	# First try "pidof"
	pid=`pidof -o $$ -o $PPID -o %PPID -x ${base}`
	if [ "$pid" != "" ] ; then
	        echo "${base} (pid $pid) is running..."
	        kill -USR2 $pid
			#echo "the current connected msgsvr is listed:"
	        return 0
	fi

	# Next try "/var/run/*.pid" files
	if [ -f /var/run/${base}.pid ] ; then
	        pid=`cat /var/run/${base}.pid | { read foo ; echo $foo ; }`
	        if [ "$pid" != "" ] ; then
	                echo "${base} dead but pid file exists"
	                return 1
	        fi
	fi
	# See if /var/lock/subsys/${base} exists
	if [ -f /var/lock/subsys/${base} ]; then
		echo "${base} dead but subsys locked"
		return 2
	fi
	echo "${base} is stopped"
	return 3
}

echo_success() {
  echo -n "[ OK ]"
  echo -ne "\r"
  return 0
}

echo_failure() {
  echo -n "[ FAILED ]"
  echo -ne "\r"
  return 1
}

# Log that something succeeded
success() {
  echo_success
  return 0
}

# Log that something failed
failure() {
  rc=$?
  echo_failure
  return $rc
}


# Check that networking is up.
[ x${NETWORKING} = x"no" ] && exit 0

# Check that MsgAgt.rc exists.
if ! [ -f /etc/promise/MsgAgt.rc ] ; then
	echo "Config file : MsgAgt.rc doesn't exist!";
	echo "Please install properly at first!";
	exit 0
fi

RETVAL=0

start() {
	echo -n "Starting msgagt services: "
	daemon $EXENAME
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/msgagt || \
	   RETVAL=1
	return $RETVAL
}	
stop() {
	echo -n "Shutting down msgagt services: "
	killproc $EXENAME
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/msgagt
	echo ""
	return $RETVAL
}	
restart() {
	stop
	start
}	
rhstatus() {
	status $EXENAME
}	

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

exit $?
