#!/bin/bash
#
# problems and hints to Tomas Cech <tcech@suse.cz> or through IRC
# License: GPLv2

if [ -f /etc/l3vm_maintenance_lock ]; then
    echo "Maintenance of the ebola. Not operating"
    echo "My head hurts, please leave."
    exit 0
fi

MYSELF="${0##*/}"
# lets source all the work already done
source /usr/lib/l3vm/l3vm_lib.sh

PATH="$PATH:/sbin"

if [ ! -u /sbin/lvm ]; then
	error "Set-user-ID bit is not set for /sbin/lvm"
	sudo chmod +s /sbin/lvm || exit 1
fi

usage_handler() {
	FUNC="$1"
	shift
	args=" $* "
	help_regex=' (--help|-h) '
	if [[ $args =~ $help_regex ]]; then
		# where I'm called from
		#echo "${FUNCNAME[@]}"
		case "${FUNC}" in
			main)
				echo "$MYSELF    < list | ls | clone | destroy | start | stop | suspend | resume | address | console | rename | --help >"
				echo
				echo "   list, ls       print machines names or status"
				echo "   clone          clone template"
				echo "   destroy        destroy cloned machine"
				echo "   start          start machine (cloned or template)"
				echo "   stop           stop machine (cloned or template)"
				echo "   suspend        suspend machine (cloned or template)"
				echo "   resume         resume machine (cloned or template)"
				echo "   hibernate      save current state of VM to disk and stop it (cloned or template)"
				echo "   address        print last DNS name or IP address of machine"
				echo "   rename         rename virtual machine"
				echo "   console        attach serial console of the machine";;
			list)
				echo "$MYSELF    < list | ls > < --templates | --clones | --all-clones | --all >"
				echo
				echo "   --templates	print templates names"
				echo "   --clones	print your machines you have cloned from templates"
				echo "   --all-clones	print all machines cloned from templates"
				echo "   --all      	print all machines";;
			start)
				echo "$MYSELF    start < machine > [machine ...]"
				echo
				echo "Starts already defined machine (can be both template or clone).";;
			stop)
				echo "$MYSELF    stop [ --force ] < machine > [machine ...]"
				echo
				echo "   --force        forcefully shutdown machine"
				echo "Stop running machine (can be both template or clone) by sending ACPI power button event."
				echo "When forced, stop machine immediately.";;
			suspend)
				echo "$MYSELF    suspend < machine > [machine ...]"
				echo
				echo "Suspend running machine (can be both template or clone).";;
			resume)
				echo "$MYSELF    resume < machine > [machine ...]"
				echo
				echo "Resume running machine (can be both template or clone).";;
			hibernate)
				echo "$MYSELF    hibernate < machine > [machine ...]"
				echo
				echo "Save state of running machine to disk and stop running (can be both template or clone).";;
			clone)
				echo "$MYSELF    clone [-D] < template > [ new_machine_name ]"
				echo
				echo "Clones template and start it. If parameter \`-D' is passed, new machine won't be using"
				echo "LVM snapshot but real LVM volume and copying takes some time."
				echo
				echo "   template               virtual machine template to be used as original"
				echo "   new_machine_name	you can specify name of the machine here"
				echo
				echo "If new_machine_name ommited, template name will be used with -cloneX suffix.";;
			destroy)
				echo "$MYSELF    destroy [ -f ] < machine > [machine ...]"
				echo
				echo "Undefines machine and remove it's virtual disk from LVM"
				echo
				echo "   -f	doesn't ask for logical volume remove";;
			console)
				echo "$MYSELF    console < machine >"
				echo
				echo "Attach screen to virtual serial port so it can be used as console.";;
			rename)
				echo "$MYSELF    rename < old_name > < new_name >"
				echo
				echo "Rename virtual machine from old_name to new_name. It also renames main LVM used for this machine";;
			address)
				echo "$MYSELF    address < machine >"
				echo
				echo "Return last DNS name or IP address of specified machine.";;
		esac
		echo
		exit 0

	fi
}

list_my_clones() {
	MY_CLONES=( $([ -f ~/.l3vm ] && cat ~/.l3vm) )
}

list() {
	usage_handler "$@"
	local some_set=""
	if [ -z "$1" ]; then
		set -- --clones
	fi
	while [ "${1:0:1}" = - ]; do
		case "$1" in
			--templates)
				shift
				list_templates > /dev/null
				SET=( "${TEMPLATES[@]}" ) ;;
			--clones)
				shift
				list_my_clones > /dev/null
				SET=( "${MY_CLONES[@]}" ) ;;
			--all-clones)
				shift
				list_clones > /dev/null
				SET=( "${CLONES[@]}" ) ;;
			--all)
				shift
				list_machines > /dev/null
				SET=( "${MACHINES[@]}" );;
			*)
				error "Unknown parameter '$1'"; exit 1;;
		esac
	done
	if [ "${#SET[@]}" -eq 0 ]; then
		echo 'No machines.'
		exit 0
	fi
	vm_stat ${SET//-/_}
}


case "$1" in
	--help)
		usage_handler main "$@";;
	ls|list)
		usage_handler list "$@"
		shift; list "$@";;
	start)
		usage_handler start "$@"
		shift; start "$@";;
	stop)
		usage_handler stop "$@"
		shift; stop "$@";;
	clone)
		usage_handler clone "$@"
		shift; clone "$@" ;;
	destroy)
		usage_handler destroy "$@"
		shift; destroy "$@" ;;
	console)
		usage_handler console "$@"
		shift; get_console "$@" ;;
	suspend)
		usage_handler suspend "$@"
		shift; suspend_ "$@" ;;
	resume)
		usage_handler resume "$@"
		shift; resume "$@" ;;
	hibernate)
		usage_handler hibernate "$@"
		shift; hibernate "$@" ;;
	rename)
		usage_handler rename "$@"
		shift; rename "$@" ;;
	address)
		usage_handler address "$@"
		shift; get_address "$@" ;;
	*)
		error "Unknown command" "$1";;
esac


