# bash completion for SubGit

have subgit &&
_subgit()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    executable="${COMP_WORDS[0]}"

    commands="configure install register uninstall help"
    
    if [ "$COMP_CWORD" = 1 ] ; then
        opts="--help --version"
        COMPREPLY=( $(compgen -W "${commands} ${opts}" -- ${cur}) )
        return 0
    fi

    command="${COMP_WORDS[1]}"

    case "${command}" in
    configure)
        opts="--help --minimal-revision"
        ;;
    install)
        opts="--help --rebuild"
        ;;
    register)
        opts="--help --key"
        ;;
    uninstall)
        opts="--help --purge"
        ;;
        help)
        opts="--help"
        ;;
        *)
        ;;
    esac

    #options that require an argument
    if [ x"${command}" = x"register" -a x"${prev}" = x"--key" ] ; then
        COMPREPLY=( $(compgen -o filenames -f -- ${cur}) )
        _filedir
        return 0
    fi
    
    if [ x"${command}" = x"help" ] ; then 
      COMPREPLY=( $(compgen -W "${opts} ${commands}" -- ${cur}) )
    else
      #all other commands require directory as an argument
      COMPREPLY=( $(compgen -W "${opts}" -d -- ${cur}) )
      _filedir -d
    fi
    return 0
    
} &&
complete -F _subgit subgit
