# bash completion for xm
# $Id: xen 392 2007-06-02 17:42:33Z guillomovitch $

_xm()
{
	local cur prev command options

	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	case $prev in
		-c)
			_filedir
			COMPREPLY=( ${COMPREPLY[@]:-} \
				$( command ls /etc/xen | grep "^$cur" ) )
			return 0
			;;
	esac

	commands='console create destroy domid domname list mem-max mem-set \
		migrate pause reboot rename restore save shutdown sysrq top \
		unpause vcpu-list vcpu-pin vcpu-set dmesg info log top \
		sched-bvt sched-bvt-ctxallow sched-sedf block-attach \
		block-detach block-list network-attach network-detach \
		network-list nvet-kist vnet-create vnet-delete help'
	
	if [[ $COMP_CWORD -eq 1 ]] ; then
		COMPREPLY=( $( compgen -W "$commands" -- $cur ) )
	else
		command=${COMP_WORDS[1]}
		if [[ "$cur" == -* ]]; then
			# possible options for the command
			case $command in
				create)
					options='-c'
					;;
				dmesg)
					options='--clear'
					;;
				list)
					options='--long'
					;;
				reboot)
					options='-w -a'
					;;
				shutdown)
					options='-w -a -R -H'
					;;
			esac
			COMPREPLY=( $( compgen -W "$options" -- $cur ) )
		else
			case $command in
				help)
					COMPREPLY=( $(compgen -W "$commands" -- $cur) )
					;;
				@(console|destroy|domnamde|list|mem-@(set|max)|migrate|pause|reboot|rename|save|shutdown|sysrq|unpause|vcpu-@(list|pin|set)|block-@(attach|detach|list)|network-@(attach|detach|list)))
					# complete on dom ID
					COMPREPLY=( $(compgen -W "$( xm list | awk '{ print $1 }' | grep -v -e Name -e Domain-0  )" -- $cur) )
					;;
				create)
					COMPREPLY=( $(compgen -W "$( command ls /etc/xen/*.cfg 2>/dev/null )" -- $cur) )
					;;
			esac
		fi
	fi

	return 0
}
complete -F _xm xm
