#!/bin/sh
#
# xend		Script to start and stop the Xen control daemon.
#
# chkconfig:	2345 95 05
# description:	Starts and stops the Xen control daemon.
#
### BEGIN INIT INFO
# Provides:          xend
# Required-Start:    $syslog $remote_fs xenstored xenconsoled
# Should-Start:
# Required-Stop:     $syslog $remote_fs xenstored xenconsoled
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop xend
# Description:       Starts and stops the Xen control daemon.
### END INIT INFO

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

start() {
	if [ -f /var/lock/subsys/xend ]; then
		msg_already_running "Xen control daemon"
		return
	fi
	if [ ! -f /var/lock/subsys/xenconsoled -o ! -f /var/lock/subsys/xenstored ]; then
		echo "xenconsoled and xenstored must be started first"
		return
	fi
	msg_starting "Starting Xen control daemon"
	daemon /usr/sbin/xend
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/xend
}

stop() {
	if [ ! -f /var/lock/subsys/xend ]; then
		msg_not_running "Xen control daemon"
		return
	fi
	msg_stopping "Stopping Xen control daemon"
	killproc xend
	rm -f /var/lock/subsys/xend
}

reload() {
	if [ ! -f /var/lock/subsys/xend ]; then
		msg_not_running "Xen control daemon"
		RETVAL=7
	else
		msg_reloading "Reloading Xen control daemon"
		killproc /usr/sbin/xend -HUP
		RETVAL=$?
	fi
}

condrestart() {
	if [ ! -f /var/lock/subsys/xend ]; then
		msg_not_running "Xen control daemon"
		RETVAL=$1
	else
		stop
		start
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  reload|force-reload)
        reload
        ;;
  status)
	status xend
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|status}"
	exit 3
esac

exit $RETVAL
