_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: --dry-run --force --gpg-sign --ignore-arch --log --makepkg-args: --makepkg-conf: --margs: --namcap --new --no-check --no-confirm --no-sync --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'
    default_cmds[chroot]='--bind-rw: --bind: --build --cargs: --create --directory: --makechrootpkg-args: --makepkg-args: --makepkg-conf: --margs: --pacman-conf: --path --suffix: --update -B -C: -D: -M: -U -x:'
    default_cmds[depends]='--graph --no-checkdepends --no-depends --no-makedepends --optdepends --pkgbase --pkgname --pkgname-all --table -G -a -b -n -t'
    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 -I -J -P -S -b -i -q -s -t: -u -v'
    default_cmds[repo]='--all --attr: --config: --database: --list --list-attr --list-path --list-repo --path --quiet --root: --status-file: --sync --table --upgrades -F: -S -a -c: -d: -l -q -r: -t -u'
    default_cmds[repo-filter]='--all --config: --database: --sync --sysroot: -a -d:'
    default_cmds[query]='--any --by: --exit-if-empty --raw --type: -a -b: -e -r -t:'
    default_cmds[search]='--any --checkdepends --color: --depends --desc --info --json --key: --maintainer --makedepends --name --optdepends --search --short --table --verbose -a -d -i -k: -m -n -q -r -s -v'
    default_cmds[srcver]='--buildscript: --jobs: --no-prepare -j:'
    default_cmds[sync]='--bind-rw: --bind: --chroot --continue --database: --directory: --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-ver --no-ver-argv --no-view --pacman-conf: --pkgver --prevent-downgrade --provides-from: --rebase --rebuild --rebuild-all --rebuild-tree --remove --reset --rmdeps --root: --sign --temp --upgrades --user: --verify -A -D: -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: --format: --no-patch --revision: -a:'
    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
