#
#  /etc/bash_completion.d/xen-tools
#
# Completion functions for Bash.  
#
# This file offers basic support for all the command line options, along with
# some specialist support to complete filesystem types, distribution targets,
# virtual images and hostnames.
#
# Hostname completion will only work if 'dir=xxx' has been filled out
# within the configuration file /etc/xen-tools/xen-tools.conf
#
#  References on command line completion:
#
#    http://www.debian-administration.org/articles/316
#    http://www.debian-administration.org/articles/317
#    http://dev.gentoo.org/~plasmaroo/devmanual/tasks-reference/completion/
#
# Steve
# --
# http://www.steve.org.uk
#
# $Id: xen-tools,v 1.17 2006/01/07 21:41:16 steve Exp $
#



#
#  Completion for xen-create-image
#
#  Completes the command line flags, and will allow tab completion of
# the supported filesystems
#
_xen_create_image()
{
    local cur prev ip roles

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}
    opts='--broadcast --boot --debug --debootstrap --dhcp --dir --dist --fs --gateway --help --hostname --ip --manual --memory --mirror --passwd --role --size --swap --version'
    
    
    #
    # Complete the initial part of the IP in the configuration file.
    ip=`grep ^gateway /etc/xen-tools/xen-tools.conf | awk -F'= '  '{print $2}'`

    case "$prev" in
	--dir)
	    _filedir -d
	    return 0
	    ;;
	--dist)
	    COMPREPLY=( $( compgen -W 'sid sarge etch'  -- "${COMP_WORDS[COMP_CWORD]}" ) )
	    return 0
	    ;;
	--fs)
	    COMPREPLY=( $( compgen -W 'xfs ext3 reiserfs'  -- "${COMP_WORDS[COMP_CWORD]}" ) )
	    return 0
	    ;;
	--ip)
	    ip=`echo ${ip} | sed -e 's/[.][^.]*$/./'`
	    COMPREPLY=( $(compgen -W "${ip}" -- ${cur}) )
	    return 0
	    ;;
	--role)
	    roles=$(for x in `ls -1 /etc/xen-tools/role.d`; do echo ${x} ; done )
	    COMPREPLY=( $( compgen -W '${roles}'  -- "${COMP_WORDS[COMP_CWORD]}" ) )
	    return 0
	    ;;
    esac
    
    if [[ ${cur} == -* ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}
complete -F _xen_create_image xen-create-image




#
#  Completion for xen-duplicate-image
#
_xen_duplicate_image() 
{
    local cur prev opts base ip
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--broadcast --dhcp --dir --from --gateway --help --hostname --ip --manual --netmask --version"

    #
    # Complete the initial part of the IP in the configuration file.
    ip=`grep ^gateway /etc/xen-tools/xen-tools.conf | awk -F'= '  '{print $2}'`
 
    #
    # First of all the base directory comes from the configuration file.
    #
    base=`grep ^dir /etc/xen-tools/xen-tools.conf | awk -F'= '  '{print $2}'`
    base=${base}/domains/

    case "${prev}" in
        --dir)
	    _filedir -d
            return 0
            ;;
        --from)
	    if [[ ${base} != '/domains/' ]] ; then
		local names=$(for x in `ls -1 ${base}`; do echo ${x} ; done )
		COMPREPLY=( $(compgen -W "${names}" -- ${cur}) )
	    fi
            return 0
            ;;
	--ip)
	    ip=`echo ${ip} | sed -e 's/[.][^.]*$/./'`
	    COMPREPLY=( $(compgen -W "${ip}" -- ${cur}) )
	    return 0
	    ;;
    esac

    if [[ ${cur} == -* ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}
complete -F _xen_duplicate_image xen-duplicate-image



#
#  Completion for xen-delete-image
#
_xen_delete_image() 
{
    local cur prev opts base names
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    #
    # First of all the base directory comes from the configuration file.
    #
    base=`grep ^dir /etc/xen-tools/xen-tools.conf | awk -F'= '  '{print $2}'`
    base=${base}/domains/

    #
    # If we have a base which isn't empty then find all the hosts
    #
    if [[ ${base} != '/domains/' ]]; then
	names=$(for x in `ls -1 ${base}`; do echo -n "${x} " ; done )
    fi

    #
    # Complete the options + all available hostnames.
    #
    opts="--dir --help --manual --test --version ${names}"

    case "${prev}" in
        --dir)
	    _filedir -d
	    return 0
            ;;
        --hostname)
	    if [[ ${base} != '/domains/' ]] ; then
		
		COMPREPLY=( $(compgen -W "${names}" -- ${cur}) )
	    fi
            ;;
    esac

    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
}
complete -F _xen_delete_image xen-delete-image



#
#  Completion for xen-update-image
#
_xen_update_image() 
{
    local cur prev opts base
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--dir --hostname --help --manual --version"

    #
    # First of all the base directory comes from the configuration file.
    #
    base=`grep ^dir /etc/xen-tools/xen-tools.conf | awk -F'= '  '{print $2}'`
    base=${base}/domains/

    case "${prev}" in
        --dir)
	    _filedir -d
	    return 0
            ;;
        --hostname)
	    if [[ ${base} != '/domains/' ]] ; then
		local names=$(for x in `ls -1 ${base}`; do echo ${x} ; done )
		COMPREPLY=( $(compgen -W "${names}" -- ${cur}) )
                return 0
	    fi
            ;;
    esac

    if [[ ${cur} == -* ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi

}
complete -F _xen_update_image xen-update-image



#
#  Completion for xen-list-images
#
_xen_list_images() 
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--dir --help --manual --test --version"

    case "${prev}" in
        --dir)
	    _filedir -d
	    return 0
            ;;
    esac

    if [[ ${cur} == -* ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}
complete -F _xen_list_images xen-list-images
