_s3sync() {
    local i cur prev opts cmd
    COMPREPLY=()
    if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
        cur="$2"
    else
        cur="${COMP_WORDS[COMP_CWORD]}"
    fi
    prev="$3"
    cmd=""
    opts=""

    for i in "${COMP_WORDS[@]:0:COMP_CWORD}"
    do
        case "${cmd},${i}" in
            ",$1")
                cmd="s3sync"
                ;;
            *)
                ;;
        esac
    done

    case "${cmd}" in
        s3sync)
            opts="-v -q -h -V --dry-run --show-no-progress --server-side-copy --aws-config-file --aws-shared-credentials-file --source-profile --source-access-key --source-secret-access-key --source-session-token --source-region --source-endpoint-url --source-accelerate --source-request-payer --source-force-path-style --target-profile --target-access-key --target-secret-access-key --target-session-token --target-region --target-endpoint-url --target-accelerate --target-request-payer --target-force-path-style --storage-class --filter-mtime-before --filter-mtime-after --filter-smaller-size --filter-larger-size --filter-include-regex --filter-exclude-regex --filter-include-content-type-regex --filter-exclude-content-type-regex --filter-include-metadata-regex --filter-exclude-metadata-regex --filter-include-tag-regex --filter-exclude-tag-regex --remove-modified-filter --check-size --check-etag --check-mtime-and-etag --check-additional-checksum --check-mtime-and-additional-checksum --additional-checksum-algorithm --full-object-checksum --enable-additional-checksum --disable-multipart-verify --disable-etag-verify --disable-additional-checksum-verify --worker-size --max-parallel-uploads --max-parallel-listings --max-parallel-listing-max-depth --object-listing-queue-size --allow-parallel-listings-in-express-one-zone --rate-limit-objects --rate-limit-bandwidth --multipart-threshold --multipart-chunksize --auto-chunksize --cache-control --content-disposition --content-encoding --content-language --content-type --expires --metadata --website-redirect --no-sync-system-metadata --no-sync-user-defined-metadata --tagging --disable-tagging --sync-latest-tagging --enable-versioning --point-in-time --sse --sse-kms-key-id --source-sse-c --source-sse-c-key --source-sse-c-key-md5 --target-sse-c --target-sse-c-key --target-sse-c-key-md5 --verbose --quiet --report-sync-status --report-metadata-sync-status --report-tagging-sync-status --json-tracing --aws-sdk-tracing --span-events-tracing --disable-color-tracing --aws-max-attempts --initial-backoff-milliseconds --force-retry-count --force-retry-interval-milliseconds --operation-timeout-milliseconds --operation-attempt-timeout-milliseconds --connect-timeout-milliseconds --read-timeout-milliseconds --warn-as-error --ignore-symlinks --head-each-target --acl --no-guess-mime-type --max-keys --put-last-modified-metadata --auto-complete-shell --disable-stalled-stream-protection --disable-payload-signing --disable-content-md5-header --disable-express-one-zone-additional-checksum --delete-excluded --if-match --copy-source-if-match --if-none-match --max-delete --ignore-glacier-warnings --preprocess-callback-lua-script --event-callback-lua-script --filter-callback-lua-script --allow-lua-os-library --lua-vm-memory-limit --lua-callback-timeout-milliseconds --allow-lua-unsafe-vm --delete --allow-both-local-storage --help --version [SOURCE] [TARGET]"
            if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
                COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
                return 0
            fi
            case "${prev}" in
                --aws-config-file)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --aws-shared-credentials-file)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --source-profile)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --source-access-key)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --source-secret-access-key)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --source-session-token)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --source-region)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --source-endpoint-url)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --target-profile)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --target-access-key)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --target-secret-access-key)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --target-session-token)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --target-region)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --target-endpoint-url)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --storage-class)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-mtime-before)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-mtime-after)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-smaller-size)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-larger-size)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-include-regex)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-exclude-regex)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-include-content-type-regex)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-exclude-content-type-regex)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-include-metadata-regex)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-exclude-metadata-regex)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-include-tag-regex)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-exclude-tag-regex)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --check-additional-checksum)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --check-mtime-and-additional-checksum)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --additional-checksum-algorithm)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --worker-size)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --max-parallel-uploads)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --max-parallel-listings)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --max-parallel-listing-max-depth)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --object-listing-queue-size)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --rate-limit-objects)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --rate-limit-bandwidth)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --multipart-threshold)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --multipart-chunksize)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --cache-control)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --content-disposition)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --content-encoding)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --content-language)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --content-type)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --expires)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --metadata)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --website-redirect)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --tagging)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --point-in-time)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --sse)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --sse-kms-key-id)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --source-sse-c)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --source-sse-c-key)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --source-sse-c-key-md5)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --target-sse-c)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --target-sse-c-key)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --target-sse-c-key-md5)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --aws-max-attempts)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --initial-backoff-milliseconds)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --force-retry-count)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --force-retry-interval-milliseconds)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --operation-timeout-milliseconds)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --operation-attempt-timeout-milliseconds)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --connect-timeout-milliseconds)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --read-timeout-milliseconds)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --acl)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --max-keys)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --auto-complete-shell)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --max-delete)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --preprocess-callback-lua-script)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --event-callback-lua-script)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --filter-callback-lua-script)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --lua-vm-memory-limit)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --lua-callback-timeout-milliseconds)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                *)
                    COMPREPLY=()
                    ;;
            esac
            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
            return 0
            ;;
    esac
}

if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then
    complete -F _s3sync -o nosort -o bashdefault -o default s3sync
else
    complete -F _s3sync -o bashdefault -o default s3sync
fi
