#!/bin/bash completion for nova-status

_nova-status(){
    local cur prev
    local -A ARGS MAP FORCE OPTS OPTS_SUB MULTI

    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    OPTS=([config_dir]='--config_dir' [config_file]='--config_file' [use_json]='--use_json' [syslog_log_facility]='--syslog_log_facility' [use_journal]='--use_journal' [use_syslog]='--use_syslog' [watch_log_file]='--watch_log_file' [log_dir]='--log_dir' [log_file]='--log_file' [log_date_format]='--log_date_format' [log_config_append]='--log_config_append' [debug]='--debug -d' [shell_completion]='--shell_completion' [help]='--help -h' [version]='version' [bash-completion]='bash-completion' [upgrade]='upgrade')
    OPTS_SUB=([version]='help="-h --help"' [bash-completion]='help="-h --help"' [upgrade]='help="-h --help"' )
    ARGS=([config_dir]=' ' [config_file]=' ' [use_json]=' ' [syslog_log_facility]=' ' [use_journal]=' ' [use_syslog]=' ' [watch_log_file]=' ' [log_dir]=' ' [log_file]=' ' [log_date_format]=' ' [log_config_append]=' ' [debug]=' ' [shell_completion]='bash zsh')
    MAP=([--config_dir]=config_dir [--config_file]=config_file [--use_json]=use_json [--syslog_log_facility]=syslog_log_facility [--use_journal]=use_journal [--use_syslog]=use_syslog [--watch_log_file]=watch_log_file [--log_dir]=log_dir [--log_file]=log_file [--log_date_format]=log_date_format [--log_config_append]=log_config_append [--debug]=debug [-d]=debug [--shell_completion]=shell_completion [-h]=help [--help]=help)
    MULTI=()

    if [ ! -z "$prev" ]; then
        # if is an argument complete with list of choice if define
        prev_key=${MAP[$prev]}
        if [ ! -z $prev_key ] && [ ! -z "${ARGS[$prev_key]}" ]; then
            COMPREPLY=($(compgen -W "${ARGS[$prev_key]}" -- "${cur}"))
            return 0
        fi
    fi
    for in_use in ${COMP_WORDS[@]:1}; do
        key=${MAP[$in_use]}
        IFS='|'
        if [[ -v OPTS_SUB[$key] ]];then
        # If is a subcommand redefine completion
            unset OPTS
            local -A OPTS
            for el in ${OPTS_SUB[$key]}; do
                IFS='='
                read k v <<< ${el}
                IFS='|'
                OPTS+=( [${k}]="${v}" )
            done
        fi
        unset IFS
        # Unset option that is already use
        if [[ -z "MULTI[$key]" ]]; then
            unset OPTS[$key]
            unset ARGS[$key]
        fi
    done
    compl="${OPTS[@]}"
    COMPREPLY=($(compgen -W "${compl}" -- "${cur}"))
    return 0
}

complete -F _nova-status nova-status

