#!/bin/sh
#
# /etc/init.d/init.d-xapi-domains
# Stop domains automatically when domain 0 shuts down.
#
# chkconfig: 345 99 00
# description: Start/stop XE VMs

if [ ! -e /proc/xen/privcmd ] || \
   [ ! -e /etc/xensource-inventory ] || \
   [ ! -e /opt/xensource/libexec/shutdown ] ; then
        exit 0
fi

. /etc/init.d/functions
. /etc/xensource-inventory

if [ "x${INSTALLATION_UUID}" = "x" ] ; then
    exit 1
fi

case "$1" in
    start)
        # Need this to be present so that we get run on shutdown.
        touch /var/lock/subsys/xapi-domains
        # Fork background thread to wait for xapi to complete initialisation
        # and then try and start vms with auto_poweron=true in other-config
	/opt/xensource/bin/xapi-autostart-vms &
        ;;
    stop)
        action $"Shutting down Virtual Machines: " \
	    /opt/xensource/libexec/shutdown "${INSTALLATION_UUID}"
        action $"Terminating remaining Virtual Machines: " \
	    /opt/xensource/libexec/shutdown --force "${INSTALLATION_UUID}"

        [ -f /var/lock/subsys/xapi-domains ] && rm -f /var/lock/subsys/xapi-domains
        ;;
    *)
        echo "Usage: $0 [start|stop]"
        ;;
esac
