#
#  /etc/bash_completion.d/xm
#
# Bash completion function for the 'xm' binary supplied with Xen.
#
# Steve
# --
# http://www.steve.org.uk
#


_xm() 
{
    local cur prev opts base running
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    #
    #  The basic options we'll complete.
    #
    opts="console create destroy help info list pause reboot shutdown top unpause"


    #
    #  If we're called by root then we can see which instances
    # are running.
    #
    if [ $UID == 0 ]; then
	 running=$(for x in `xm list --long | grep \(name | grep -v Domain-0 | awk '{ print $2 }' | tr -d \)`; do echo ${x} ; done )
    fi



    #
    #  Complete the arguments to some of the basic commands.
    #
    case "${prev}" in
	console)
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
	    return 0
            ;;
        create)
	    local names=$(for x in `ls -1 /etc/xen/*.cfg 2>/dev/null`; do echo ${x/\/etc\/xen\//} ; done )
	    COMPREPLY=( $(compgen -W "${names}" -- ${cur}) )
            return 0
            ;;
        destroy)
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
            return 0
            ;;
        help)
	    COMPREPLY=( $(compgen -W "--long" -- ${cur}) )
            return 0
            ;;
        list)
	    COMPREPLY=( $(compgen -W "--long" -- ${cur}) )
            return 0
            ;;
        pause)
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
            return 0
            ;;
        reboot)
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
            return 0
            ;;
        shutdown)
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
            return 0
            ;;
        unpause)
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
            return 0
            ;;
        *)
        ;;
    esac

   COMPREPLY=($(compgen -W "${opts}" -- ${cur}))  
   return 0
}
complete -F _xm xm
