#!/bin/bash completion for nova-manage

_nova-manage(){
    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' [log-dir]='--log-dir' [log-file]='--log-file' [log-date-format]='--log-date-format' [log-config-append]='--log-config-append' [debug]='--debug -d' [post-mortem]='--post-mortem' [shell_completion]='--shell_completion' [help]='--help -h' [version]='version' [bash-completion]='bash-completion' [api_db]='api_db' [cell_v2]='cell_v2' [db]='db' [placement]='placement' [libvirt]='libvirt' [volume_attachment]='volume_attachment' [image_property]='image_property' [limits]='limits')
    OPTS_SUB=([version]='help="-h --help"' [bash-completion]='help="-h --help"' [api_db]='help="-h --help"' [cell_v2]='help="-h --help"' [db]='help="-h --help"' [placement]='help="-h --help"' [libvirt]='help="-h --help"' [volume_attachment]='help="-h --help"' [image_property]='help="-h --help"' [limits]='help="-h --help"' )
    ARGS=([config-dir]=' ' [config-file]=' ' [use-json]=' ' [syslog-log-facility]=' ' [use-journal]=' ' [use-syslog]=' ' [log-dir]=' ' [log-file]=' ' [log-date-format]=' ' [log-config-append]=' ' [debug]=' ' [post-mortem]=' ' [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 [--log-dir]=log-dir [--log-file]=log-file [--log-date-format]=log-date-format [--log-config-append]=log-config-append [--debug]=debug [-d]=debug [--post-mortem]=post-mortem [--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-manage nova-manage

