_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: --cargs: --checkpkg --chroot --clean --cleanbuild --database: --dbext: --directory: --force --gpg-sign --ignore-arch --log --makechrootpkg-args: --makepkg-args: --makepkg-conf: --makepkg-gnupghome: --margs: --namcap --new --no-check --no-confirm --no-sync --null --pacman-conf: --pkgver --prevent-downgrade --remove --rmdeps --root: --sign --syncdeps --temp --user: --verify -A -C -D: -L -N -R -S -T -U: -a: -c -d: -f -n -r -s -v -z'
    default_cmds[chroot]='--bind-rw: --bind: --build --cargs: --checkpkg --create --directory: --ignorearch --makechrootpkg-args: --makepkg-args: --makepkg-conf: --margs: --namcap --nocheck --pacman-conf: --suffix: --temp --update --user: -A -B -C: -D: -M: -N -T -U -x:'
    default_cmds[fetch]='--auto --discard --existing --ff --ff-only --merge --no-commit --no-ff --rebase --recurse --reset --results: -S -e -f -r'
    default_cmds[pkglist]='--fixed-strings --info --perl-regexp --pkgbase --plain --quiet --search --systime --ttl: --users --verify -F -P -b -i -q -s -t: -u -v'
    default_cmds[repo]='--all --attr: --config: --database: --dbext: --delim: --format: --ignore-by: --ignore: --json --jsonl --list --list-attr --list-path --list-repo --missing --path --quiet --root: --search-by: --search: --sync --table --upgrades -F: -J -S -a -c: -d: -f: -i: -l -m -q -r: -s: -t -u'
    default_cmds[repo-filter]='--all --config: --database: --sync --sysroot: -a -d:'
    default_cmds[srcver]='--arg-file: --buildscript: --jobs: --makepkg-args: --margs: --no-prepare --null -a: -j: -z'
    default_cmds[sync]='--auto-key-retrieve --bind-rw: --bind: --chroot --clean --cleanbuild --columns --continue --database: --directory: --exclude: --ff --force --format: --ignore-arch --ignore-file: --ignore: --keep-going: --log --makepkg-args: --makepkg-conf: --new --no-build --no-check --no-confirm --no-graph --no-provides --no-sync --no-ver --no-ver-argv --no-view --pacman-conf: --pkgver --prefix --prevent-downgrade --provides-from: --rebase --rebuild --rebuild-all --rebuild-tree --remove --reset --rmdeps --root: --save: --sign --temp --upgrades --user: --verify -A -C -D: -K -L -R -S -T -U: -c -d: -f -k: -n -o -r -u -v'
    default_cmds[vercmp]='--all --current --path: --quiet --upair: -a -c -p: -q -u:'
    default_cmds[view]='--arg-file: --confirm --exclude: --format: --no-patch --prefix --revision: -a:'
    default_cmds[graph]=''
    default_cmds[format]=''
    default_cmds[repo-parse]=''
    default_cmds[query]=''
    default_cmds[depends]=''
    default_cmds[search]=''

    # 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
