#!/bin/bash
#
#	/etc/rc.d/init.d/updatempppathd
#
# Starts the daemon which updates all MPP LUN paths
#
# chkconfig: 2345 24 76
# description: Manage the daemon which updates all MPP LUN paths
# processname: updatempppathd 
# pidfile: /var/run/updatempppathd.pid

DAEMON=/opt/xensource/sm/updatempppathd.py
prog=`basename $DAEMON`
pidfile=/var/run/${prog}.pid
logfile=/var/log/SMlog

# Source function library.
. /etc/init.d/functions

RETVAL=0

#
# See how we were called.
#

start() {
	test -x $DAEMON || exit 5
	if [ -f $pidfile ]; then
	    pid=`cat $pidfile` 
	    ps -p $pid > /dev/null
	    if [ $? -eq 0 ]; then
	        echo "$prog already running with pid $pid"
	        return 0
	    else
		rm $pidfile
	    fi
	fi
	echo -n $"Starting $prog daemon: "
	daemon $DAEMON -d $logfile
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    for i in `pidof python`; do
		line=`ps -h -p $i -o pid,command 2> /dev/null`
		echo $line | grep $prog > /dev/null
		if [ $? -eq 0 ]; then
		    echo $i > $pidfile
		    break
		fi
	    done
	    success $"$base startup"
	else
	    failure $"$base startup"
	fi
	echo
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog daemon: "
        `test -e $pidfile`
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    PID=`cat $pidfile`
	    kill $PID
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && rm -f $pidfile
	else 
	    RETVAL=0
        fi 
	[ $RETVAL -eq 0 ] && success $"$base stop" || failure $"$base stop"
	echo 
	return $RETVAL
}

restart() {
	stop
	start
}	

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
restart)
	restart
	;;
condrestart)
	if [ -f $pidfile ]; then
	    restart
	fi
	;;
status)
	status $prog
	RETVAL=$?
	;;
*)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
	RETVAL=3
esac

exit $RETVAL
