# bash completion for autodist-distroquery

_autodist_distroquery() {
    local cur prev words cword
    _init_completion || return

    local repos='' archs=''
    if [ -r /etc/autodist/config ]; then
        eval "$(grep -E '^AUTODIST_REPOSITORIES=|^AUTODIST_ARCHS=' /etc/autodist/config 2>/dev/null)"
        repos="${AUTODIST_REPOSITORIES[*]}"
        archs="${AUTODIST_ARCHS[*]}"
    fi

    local commands="package package-all-archs package-binary package-needrebuild
                    repository repository-log repository-problems repository-srclist
                    recent-html search search-html social-log"

    local command="${words[1]}"

    if [ $cword -eq 1 ]; then
        COMPREPLY=($(compgen -W "$commands" -- "$cur"))
        return
    fi

    case "$command" in
        package)
            case $cword in
                2) COMPREPLY=($(compgen -W "$repos" -- "$cur")) ;;
                4) COMPREPLY=($(compgen -W "$archs" -- "$cur")) ;;
            esac ;;
        package-all-archs)
            [ $cword -eq 2 ] && COMPREPLY=($(compgen -W "$repos" -- "$cur")) ;;
        package-binary|package-needrebuild)
            case $cword in
                2) COMPREPLY=($(compgen -W "$repos" -- "$cur")) ;;
                4) COMPREPLY=($(compgen -W "$archs" -- "$cur")) ;;
            esac ;;
        repository)
            case $cword in
                2) COMPREPLY=($(compgen -W "$repos" -- "$cur")) ;;
                3) COMPREPLY=($(compgen -W "name buildtime" -- "$cur")) ;;
                4) COMPREPLY=($(compgen -W "asc desc" -- "$cur")) ;;
            esac ;;
        repository-log|repository-srclist|recent-html)
            [ $cword -eq 2 ] && COMPREPLY=($(compgen -W "$repos" -- "$cur")) ;;
        repository-problems)
            case $cword in
                2) COMPREPLY=($(compgen -W "$repos" -- "$cur")) ;;
                3) COMPREPLY=($(compgen -W "$archs" -- "$cur")) ;;
            esac ;;
        search|search-html)
            case $cword in
                5) COMPREPLY=($(compgen -W "name buildtime" -- "$cur")) ;;
                6) COMPREPLY=($(compgen -W "asc desc" -- "$cur")) ;;
                7) COMPREPLY=($(compgen -W "$archs" -- "$cur")) ;;
            esac ;;
        social-log)
            [ $cword -eq 2 ] && COMPREPLY=($(compgen -W "get post" -- "$cur")) ;;
    esac
}
complete -F _autodist_distroquery autodist-distroquery
