# Copyright 2011 Red Hat, Inc.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


#
# This is the bash auto completion script for the rhc command
# 
_rhc()
{
    local cur opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"

    if [ $COMP_CWORD -eq 1 ]; then
        opts="domain app sshkey help"
        COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
    elif [ $COMP_CWORD -eq 2 ]; then
        case "${COMP_WORDS[1]}" in
            domain)
                opts="create show alter status destroy"
                ;;
            app)
                opts="create show start stop force-stop restart reload status destroy tidy add-alias remove-alias threaddump snapshot tail cartridge"
                ;;
            sshkey)
                opts="add update remove list"
                ;;
            *)
                opts=""
                ;;
        esac

    elif [ $COMP_CWORD -eq 3 ]; then
        case "${COMP_WORDS[1]}" in
            domain)
                case "${COMP_WORDS[2]}" in
                    create | alter | destroy)
                        opts="--debug --help --rhlogin --password --namespace --config --timeout"
                        ;;
                    show | status)
                        opts="--debug --help --rhlogin --password --config --timeout"
                        ;;
                    *)
                        ;;
                esac
                ;;
            app)
                case "${COMP_WORDS[2]}" in
                    create)
                        opts="--debug --help --rhlogin --password --no-dns --nogit --app --repo --type --enable-jenkins --config --timeout"
                        ;;
                    show | start | stop | force-stop | restart | reload | status | destroy | tidy | add-alias | remove-alias | threaddump | destroy)
                        opts="--debug --help --rhlogin --password --app --alias --bypass --config --timeout"
                        ;;
                    tail)
                        opts="--debug --help --rhlogin --password -app --opts --filepath --config --timeout"
                        ;;
                    snapshot)
                        opts="save restore"
                        ;;
                    cartridge)
                        opts="add remove stop start restart status reload list"
                        ;;
                    *)
                        ;;
                esac
                ;;
            sshkey)
                case "${COMP_WORDS[2]}" in
                    add | update)
                        opts="--debug --help --rhlogin --password --identifier --ssh --config --timeout"
                        ;;
                    remove)
                        opts="--debug --help --rhlogin --password --identifier --config --timeout"
                        ;;
                    list)
                        opts="--debug --help --rhlogin --password --config --timeout"
                        ;;
                    *)
                        ;;
                esac
                ;;
        esac

    elif [ $COMP_CWORD -eq 4 ]; then
        case "${COMP_WORDS[1]}" in
            app)
                case "${COMP_WORDS[2]}" in
                    snapshot)
                        case "${COMP_WORDS[3]}" in
                            save | restore)
                                opts="--debug --help --rhlogin --password -app --filepath --config --timeout"
                                ;;
                            *)
                                ;;
                        esac
                        ;;
                    cartridge)
                        case "${COMP_WORDS[3]}" in
                            add | remove | stop | start | restart | status | reload | list)
                                opts="--debug --help --rhlogin --password -app --cartridge --config --timeout"
                                ;;
                            *)
                                ;;
                        esac
                        ;;
                esac
                ;;
        esac

    fi

   if [ ${#opts[@]} -gt 0 ]; then COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ); fi
   return 0
}
complete -o default -F _rhc rhc