#-*- mode: shell-script;-*-

#
# echo 1 if the command line option is a longopt with '='.
#
function elementExists()
{
    opts_with_equal=(\
        --config-file \
        --controller --controller-port \
        --color \
        --cluster-id --cluster-name --nodes \
        --properties \
        --vendor --provider-version --os-user \
        --cluster-type --db-admin --db-admin-passwd \
        --account --db-name \
        )

    for i in ${opts_with_equal[@]}; do
        if [ "${opts_with_equal[$i]}" == "$1" ]; then
            echo 1
            break
        fi
    done

    return 0
}

#
# $*. The options to find.
#
# Returns true if the current command line option list has one option of the
# presented options.
#
function _s9s_has_options()
{
    local option

    for (( i=0; i < COMP_CWORD; i++ )); do
        for option in $*; do
            if [[ ${COMP_WORDS[i]} == "$option" ]]; then
                return 0
            fi
        done
    done
    
    return 1
}

#
# Returns true if the COMMAND part of the command line options is not yet
# entered.
#
function _s9s_no_command()
{
    if ! _s9s_has_options backup cluster job maint node process user script;
    then
        return 0
    fi

    return 1
}

function _s9s() 
{
    local main_opt
    local cur prev opts

    COMPREPLY=()

    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    if [[ "$prev" == "=" ]]; then
        prev="${COMP_WORDS[COMP_CWORD - 2]}"
    fi
 
    #opts="--help --verbose --version "
    #opts+=" node cluster"

    # 
    # Offering the commands if they are not entered.
    #
    if _s9s_no_command; then
        opts+="backup cluster job maint node process user script"
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi

    for (( i=0; i < COMP_CWORD; i++ )); do
        if [[ ${COMP_WORDS[i]} == "backup" ]]; then
            main_opt="--list --create --restore --delete --help"

            if ! _s9s_has_options $main_opt; then
                opts=$main_opt
            fi
            
            COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
            return 0
        elif [[ ${COMP_WORDS[i]} == "cluster" ]]; then
            main_opt="--list --stat --create --ping --rolling-restart --add-node --remove-node --drop --start --crate-account --delete-account --create-database"
            if ! _s9s_has_options $main_opt; then
                opts="$main_opt "
            elif [ "$prev" == "--cluster-id" ]; then
                prev="${cur%%=*}="
                cur=${cur#*=}
                opts=$(s9s cluster --list --long --batch | awk '{print $1}')
            elif [ "$prev" == "--cluster-name" ]; then
                prev="${cur%%=*}="
                cur=${cur#*=}
                opts=$(s9s cluster --list)
            elif [ "$prev" == "--color" ]; then
                prev="${cur%%=*}="
                cur=${cur#*=}
                opts="never always auto"
            else 
                opts=$(s9s cluster --list)
                opts+="--help --verbose --version " 
                opts+="--config-file= "
                opts+="--controller= --controller-port= "
                opts+="--rpc-tls "
                opts+="--long --print-json --color= --batch --no-header "
                opts+="--wait --log --schedule= "
                opts+="--cluster-id= --cluster-name= --nodes= "
                opts+="--properties= "
                opts+="--vendor= --provider-version= --os-user= "
                opts+="--cluster-type= --db-admin= --db-admin-passwd= "
                opts+="--account= --with-database --db-name= "
            fi 
        elif [[ ${COMP_WORDS[i]} == "job" ]]; then
            main_opt="--wait --log --list --help"

            if ! _s9s_has_options $main_opt; then
                opts=$main_opt
            fi
        elif [[ ${COMP_WORDS[i]} == "maint" ]]; then
            main_opt="--list --create --delete --help"

            if ! _s9s_has_options $main_opt; then
                opts=$main_opt
            fi
        elif [[ ${COMP_WORDS[i]} == "node" ]]; then
            main_opt="--list --set --help --list-config --change-config "
            main_opt+="--pull-config --push-config"

            if ! _s9s_has_options $main_opt; then
                opts=$main_opt
            else
                opts+="--help --verbose --version " 
                opts+="--config-file= --controller= "
                opts+="--controller-port= "
                opts+="--long --print-json --color= --batch --no-header "
                opts+="--wait --log --schedule= "
                opts+="--cluster-id= --cluster-name= --nodes= --properties= "
            fi
        elif [[ ${COMP_WORDS[i]} == "script" ]]; then
            main_opt="--execute"

            if ! _s9s_has_options $main_opt; then
                opts=$main_opt
            else
                opts+="--help --verbose --version " 
                opts+="--config-file= --controller= "
                opts+="--controller-port= "
                opts+="--long --print-json --color= --batch --no-header "
                opts+="--wait --log --schedule= "
                opts+="--cluster-id= --cluster-name= --nodes= --properties= "
                opts+=$(compgen -f  -- "${COMP_WORDS[${COMP_CWORD}]}" )
            fi
        elif [[ ${COMP_WORDS[i]} == "process" ]]; then
            main_opt="--list --top --help"

            if ! _s9s_has_options $main_opt; then
                opts=$main_opt
            fi
        elif [[ ${COMP_WORDS[i]} == "user" ]]; then
            main_opt="--list --whoami --create --help"

            if ! _s9s_has_options $main_opt; then
                opts=$main_opt
            elif [ "$prev" == "--cmon-user" ]; then
                prev="${cur%%=*}="
                cur=${cur#*=}
                opts=$(s9s user --list)
            else
                opts+="--help --verbose --version " 
                opts+="--config-file= "
                opts+="--controller= --controller-port= "
                opts+="--rpc-tls "
                opts+="--long --print-json --color= --batch --no-header "
                opts+="--wait --log --schedule= "
                opts+="--cluster-id= --cluster-name= --nodes= "
                opts+="--properties= "
                opts+="--vendor= --provider-version= --os-user= "
                opts+="--cluster-type= --db-admin= --db-admin-passwd= "
                opts+="--account= --with-database --db-name= "

                opts+="--cmon-user="
                opts+=$(s9s user --list)
            fi
        fi
    done
 
            isEqualOptions=$(elementExists "${cur}")
            if [ "${isEqualOptions:-0}" -eq 1 ]; then
                COMPREPLY=( $(compgen -W "${opts}" -S '=' -- "${cur}") )
            else
                COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
            fi

            return 0
}
complete -o nospace -F _s9s s9s
