#!/bin/bash
# invoke as invirt-remote-proxy-$TYPE, with "TYPE" in the remctl sense.

klist -s || kinit -k

TYPE="${0##*-}"
case "$TYPE" in
    control )
	MACHINE="$1"; SERVICE="$2"; shift; shift ;;
    * )
	SERVICE="$1"; shift ;;
esac

case "$TYPE/$SERVICE" in
    web/listvms )
	invirt-remote-listvms "$@" ;;
    web/vnccert )
        invirt-remote-vnccert "$@" ;;
    web/availability )
        invirt-remote-availability "$@" ;;
    web/lvcreate | web/lvremove | web/lvrename | web/lvresize )
        if [ -f "/etc/invirt/nolvm" ]; then
            echo "LVM operations are temporarily disabled for maintenance, sorry." >&2
            exit 2
        fi
        remctl "$(invirt-getconf hosts.0.hostname)" remote "$TYPE" "$SERVICE" "$@" ;;
    control/help )
        invirt-remctl-help ;;
    control/create|control/install )
        if [ -f "/etc/invirt/nocreate" ]; then
            echo "Booting VMs is temporarily disabled for maintenance, sorry." >&2
            exit 2
        fi
	invirt-remote-create "$SERVICE" "$MACHINE" "$@" ;;
    control/listhost|control/list-host )
	invirt-remote-listhost "$MACHINE" "$@" ;;
    control/* )
	# Everything but create must go where the VM is already running.
	invirt-remote-control "$MACHINE" "$SERVICE" "$@" ;;
    * )
        echo "ERROR: invalid subcommand"
        exit 34
        ;;
esac
