#!/bin/bash
# sipb-xen-vmcontrol MACHINE ACTION
#
# remctl should have already verified that the user is authorized to
# control the machine.  So, we just need to execute the action requested.
#
# $0 and $1 come from the trusted remctl source.
#
# $2 and so on are user-provided, and thus sketchy.  I don't think we
# need them for this script.

ORIGMACHINE="$1"
ACTION="$2"
MACHINE="d_$ORIGMACHINE"

case "$ACTION" in
    list|vcpu-list|uptime)
        xm "$ACTION" "$MACHINE"
	;;
    destroy|shutdown)
	xm "$ACTION" "$MACHINE"
	/usr/sbin/sipb-xen-update-conserver </dev/null >/dev/null 2>&1 &
	;;
    install|create|reboot)
	shift; shift;
	if [ "$ACTION" = "reboot" ]; then
	    xm destroy "$MACHINE" 2>/dev/null
	else
	    xm list "$MACHINE" >/dev/null 2>/dev/null && echo "$MACHINE already exists" && exit 1
	fi
	if [ "$ACTION" = "install" ]; then
	    xm create sipb-database machine_name="$ORIGMACHINE" installer_options="$(printf '%q ' "$@")"
	elif [ -n "$1" ]; then
	    xm create sipb-database machine_name="$ORIGMACHINE" cdrom_image="$1"
	else
	    xm create sipb-database machine_name="$ORIGMACHINE"
	fi
	(sleep 4; /usr/sbin/sipb-xen-update-conserver) </dev/null >/dev/null 2>&1 &
	;;
    list-long)
	xm list --long "$MACHINE"
	;;
    *)
        echo "ERROR: Invalid Command"
        exit 34
        ;;
esac
