#!/bin/sh
#
# xend		Script to start and stop the Xen control daemon.
#
# Author:       Keir Fraser <keir.fraser@cl.cam.ac.uk>
#
# chkconfig: 2345 98 01
# description: Starts and stops the Xen control daemon.

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

if ! [ -e /proc/xen/privcmd ]; then
	exit 0
fi

await_daemons_up()
{
    	retval=$1
	i=1
	rets=10
	/usr/sbin/xend status
	while [ $? -ne 0 -a $i -lt $rets ]; do
	    sleep 1
	    i=$(($i + 1))
	    /usr/sbin/xend status > /dev/null 2>&1
	done
}

RETVAL=0

case "$1" in
  start)
  	if [ ! -f /var/lock/subsys/xend ]; then
    	    msg_starting "xend"
    	    daemon /usr/sbin/xend start
	    RETVAL=$?
    	    await_daemons_up
	    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/xend
	else
	    msg_already_running xend
	fi
	;;
  stop)
  	if [ -f /var/lock/subsys/xend ]; then
	    msg_stopping "xend"
    	    daemon /usr/sbin/xend stop
	    rm -f /var/lock/subsys/xend >/dev/null 2>&1
	else
	    msg_not_running "xend"
	fi
	;;
  status)
	/usr/sbin/xend status
	;;
  restart|force-reload)
  	$0 stop
	$0 start
	;;
  *)
	# do not advertise unreasonable commands that there is no reason
	# to use with this device
	echo $"Usage: $0 {start|stop|status|restart|force-reload}"
	exit 3
esac

exit $RETVAL

