[ -z "$BASH_VERSION" ] && return

_filedir()
{
	local IFS=$'\t\n' xspec #glob

	#glob=$(set +o|grep noglob) # save glob setting.
	#set -f		 # disable pathname expansion (globbing)

	xspec=${1:+"!*.$1"}	# set only if glob passed in as $1
	COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -f -X "$xspec" -- "$cur" ) \
		    $( compgen -d -- "$cur" ) )
	#eval "$glob"    # restore glob setting.
}

_vbuilder()
{
    local opts cur prev first
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    first="${COMP_WORDS[1]}"

    ## The basic options we'll complete.
    options="--version --arch --category --dist-upgrade --unionfs --target --with-compat32"
    actions="clean build build-rpm install-rpm remove-rpm show-info"
    opts="$options $actions"

    _arch=$(rpm --eval %_arch)

    in_options=0

    ## Complete the arguments to some of the basic commands.
    case "${prev}" in
	--version)
	    if [ "$_arch" = "x86_64" ]; then
		local running="VineSeed VineSeed_i386 5.1 5.1_i386 4.2_i386"
	    else
		local running="VineSeed 5.1 4.2"
	    fi
	    COMPREPLY=( $(compgen -W "${running}" -- "${cur}") )
	    ;;

	--arch)
	    local running="i386 ppc x86_64"
	    COMPREPLY=( $(compgen -W "${running}" -- "${cur}") )
	    ;;

	--category)
	    local running="main proposed-updates,main plus,main nonfree,plus,main test,nonfree,plus,main test,plus,main test,main"
	    COMPREPLY=( $(compgen -W "${running}" -- "${cur}") )
	    ;;

	--target)
	    local running="$(cat /usr/lib/rpm/rpmrc | grep arch_canon: | sed -e "s/arch_canon:[[:blank:]]*\(.*\):.*/\1/")"
	    COMPREPLY=( $(compgen -W "${running}" -- "${cur}") )
	    ;;

	--build-rpm|build-rpm)
	    if [ $COMP_CWORD -eq 1 -o "${COMPREPLY+set}" != "set" ]; then
		_filedir 'src.rpm'
	    fi
	    ;;

	--install-rpm|install-rpm|--remove-rpm|remove-rpm)
	    if [ $COMP_CWORD -eq 1 -o "${COMPREPLY+set}" != "set" ]; then
		_filedir 'rpm'
	    fi
	    ;;

	*)
	    COMPREPLY=($(compgen -W "${opts}" -- "${cur}"))
	    #return 0
	    ;;
    esac

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

}

complete -o filenames -o nospace -F _vbuilder vbuilder

### end of file