# This completion file has been inspired by the completion files of the Git and
# the Docker projects.

__consulcomp() {
    local all c s=$'\n' IFS=' '$'\t'$'\n'
    local cur="${COMP_WORDS[COMP_CWORD]}"

    for c in $1; do
        case "$c$4" in
            --*=*) all="$all$c$4$s" ;;
            *)     all="$all$c$4 $s" ;;
        esac
    done
    IFS=$s
    COMPREPLY=($(compgen -P "$2" -W "$all" -- "$cur"))
    return
}

__consul_agent() {
    local subcommands="
    -advertise
    -advertise-wan
    -atlas
    -atlas-join
    -atlas-token
    -atlas-endpoint
    -bootstrap
    -bind
    -http-port
    -bootstrap-expect
    -client
    -config-file
    -config-dir
    -data-dir
    -recursor
    -dc
    -encrypt
    -join
    -join-wan
    -retry-join
    -retry-interval
    -retry-max-wan
    -log-level
    -node
    -protocol
    -rejoin
    -server
    -syslog
    -ui
    -ui-dir
    -pid-file
    "
    __consulcomp "-help $subcommands"
}

__consul_configtest() {
    local subcommands="
    -config-file
    -config-dir
    "
    __consulcomp "-help $subcommands"
}

__consul_event() {
    local subcommands="
    -http-addr
    -datacenter
    -name
    -node
    -service
    -tag
    -token
    "
    __consulcomp "-help $subcommands"
}

__consul_exec() {
    local subcommands="
    -http-addr
    -datacenter
    -prefix
    -node
    -service
    -tag
    -wait
    -wait-repl
    -token
    "

    __consulcomp "-help $subcommands"
}

__consul_force_leave() {
    __consulcomp "-help -rpc-addr"
}

__consul_info() {
    __consulcomp "-help -rpc-addr"
}

__consul_join() {
    local subcommands="
    -rpc-addr
    -wan
    "

    __consulcomp "-help $subcommands"
}

__consul_keygen() {
    # NOTE: left empty on purpose.
    return
}

__consul_keyring() {
    local subcommands="
    -install
    -list
    -remove
    -token
    -use
    -rpc-addr
    "

    __consulcomp "-help $subcommands"
}

__consul_leave() {
    __consulcomp "-help -rpc-addr"
}

__consul_lock() {
    local subcommands="
    -http-addr
    -n
    -name
    -token
    -pass-stdin
    -try
    -monitor-retry
    -verbose
    "

    __consulcomp "-help $subcommands"
}

__consul_maint() {
    local subcommands="
    -enable
    -disable
    -reason
    -service
    -token
    -http-addr
    "

    __consulcomp "-help $subcommands"
}

__consul_members() {
    local subcommands="
    -detailed
    -rpc-addr
    -status
    -wan
    "

    __consulcomp "-help $subcommands"
}

__consul_monitor() {
    local subcommands="
    -log-level
    -rpc-addr
    "

    __consulcomp "-help $subcommands"
}

__consul_reload() {
    __consulcomp "-help -rpc-addr"
}

__consul_rtt() {
    local subcommands="
    -wan
    -http-addr
    "

    __consulcomp "-help $subcommands"
}

__consul_version() {
    # NOTE: left empty on purpose.
    return
}

__consul_watch() {
    local subcommands="
    -http-addr
    -datacenter
    -token
    -key
    -name
    -passingonly
    -prefix
    -service
    -state
    -tag
    -type
    "

    __consulcomp "-help $subcommands"
}

__consul() {
    local c=1 command
    while [ $c -lt $COMP_CWORD ]; do
        cmd="${COMP_WORDS[c]}"
        case "$cmd" in
            -*) ;;
            *) command="$cmd"
        esac
        c=$((++c))
    done

    local cmds="
    agent
    configtest
    event
    exec
    force-leave
    info
    join
    keygen
    keyring
    leave
    lock
    maint
    members
    monitor
    reload
    rtt
    version
    watch
    "

    local globalflags="--help --version"

    # Complete a command.
    if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
        case "${COMP_WORDS[COMP_CWORD]}" in
            -*|--*) __consulcomp "$globalflags" ;;
            *)      __consulcomp "$cmds" ;;
        esac
        return
    fi

    # Command options.
    local completion_func="__consul_${command//-/_}"
    declare -f $completion_func >/dev/null && $completion_func && return
}

complete -o default -o nospace -F __consul consul
