_aur_completion()
{
    local cur prev words cword
    _get_comp_words_by_ref cur prev words cword

    declare -A default_cmds

    default_cmds[build]='--arg-file: --bind-rw: --bind: --buildscript: --checkpkg --chroot --clean --database: --directory: --force --ignore-arch --log --makepkg-args: --makepkg-conf: --namcap --new --no-check --no-confirm --no-sync --pacman-conf: --pkgver --prevent-downgrade --remove --repo: --results: --rmdeps --root: --sign --syncdeps --temp --user: --verify -A -C -D: -L -N -R -S -T -U: -a: -c -d: -f -n -r -s -v'
    default_cmds[chroot]='--bind-rw: --bind: --build --buildscript: --create --directory: --makepkg-conf: --packagelist --pacman-conf: --suffix: --update -B -C: -D: -M: -U -x:'
    default_cmds[depends]='--no-checkdepends --no-depends --no-makedepends --pkgbase --pkgname --pkgname-all --table -a -b -n -t'
    default_cmds[fetch]='--existing --recurse --results: --sync: -S -r'
    default_cmds[pkglist]='--fixed-strings --info --perl-regexp --pkgbase --plain --search --time: -F -I -P -S -b -t:'
    default_cmds[repo]='--all --config: --database: --list --path --path-list --quiet --repo-list --repo: --root: --status-file: --sync --table --upgrades -S -a -c: -d: -l -q -r: -t -u'
    default_cmds[repo-filter]='--all --database: --repo: --sync --sysroot: -a -d:'
    default_cmds[query]='--by: --type: -b: -t:'
    default_cmds[search]='--any --checkdepends --depends --desc --info --key: --maintainer --makedepends --name --optdepends --raw --search --short --table --verbose -a -d -i -k: -m -n -q -r -s -v'
    default_cmds[srcver]='--jobs: --no-prepare -j:'
    default_cmds[sync]='--bind-rw: --bind: --chroot --continue --database: --directory: --force --format: --ignore-arch --ignore-file: --ignore: --log --makepkg-args: --makepkg-conf: --new --no-build --no-check --no-confirm --no-graph --no-provides --no-ver --no-ver-argv --no-view --pacman-conf: --pkgver --prevent-downgrade --provides-from: --rebuild --rebuild-all --rebuild-tree --remove --repo: --results: --rm-deps --root: --sign --temp --upgrades --verify -A -D: -L -R -S -T -c -d: -f -n -o -r -u -v'
    default_cmds[vercmp]='--all --current --path: --quiet --upair: -a -c -p: -q -u:'
    default_cmds[graph]=''

    # complete subcommands
    if [[ $cword -eq 1 ]]; then
        COMPREPLY=( $(compgen -W "${!default_cmds[*]}" -- "$cur") )
        return
    fi

    # If there's an override for subcommand, use it
    if declare -F "_aurutils_${words[1]/-/_/}" >/dev/null ; then
        "_aurutils_${words[1]}"
        return
    fi

    # Complete with the generated opts stored above, unless the previous option
    # is stored with an : suffix, because the option requires an argument.
    # Fallback to default (files) completion in such cases.

    _fallback_completion
}

_fallback_completion(){
    opts=(${default_cmds[${words[1]}]})
    if [[ ${opts[*]} != *$prev:* ]] || [[ $cword -eq 2 ]]; then
        COMPREPLY=($(compgen -W "${opts[*]%:}" -- "$cur"));
    fi
}

_complete_with_repos() {
    opts=($(aur repo --repo-list))
    COMPREPLY=($(compgen -W "${opts[*]}" -- "$cur"))
}

_aurutils_build() {
    case $prev in
        -d|--database|--repo)
            _complete_with_repos ;;
        *) _fallback_completion ;;
    esac
}

_aurutils_repo() {
    case $prev in
        -d|--database|--repo)
            _complete_with_repos ;;
        *) _fallback_completion ;;
    esac
}

_aurutils_repo_filter() {
    case $prev in
        -d|--database|--repo)
            _complete_with_repos ;;
        *) _fallback_completion ;;
    esac
}

_aurutils_sync() {
    case $prev in
        -d|--database|--repo)
            _complete_with_repos ;;
        *) _fallback_completion ;;
    esac
}

complete -o bashdefault -o default -o nosort -F _aur_completion aur
