#
# -------------------------------------------------------------------------- #
#             Bash completion file for Grycap EC3 utility                    #
#          Copyright 2015, Universitat Politecnica de Valencia               #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

EC3_CLUSTER_STORE_PATH="$HOME/.ec3/clusters"

EC3_ARGS='launch list show templates ssh reconfigure destroy clone migrate stop restart'

_ec3()
{
    local cur opt
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"

    # Completion of commands.
    if [[ $COMP_CWORD == 1 ]]; then
        COMPREPLY=( $(compgen -W '$EC3_ARGS' -- $cur ) )
    return 0
    fi

    # Completion of command parameters.
    opt="${COMP_WORDS[1]}"
    case $opt in
    "launch")
        EC3_TEMPLATES=$(ls /etc/ec3/templates/*.radl | xargs -n1 basename | cut -d. -f1)
        COMPREPLY=( $(compgen -W '$EC3_TEMPLATES' -- $cur ) )
        return 0
        ;;

    "clone"|"destroy"|"migrate"|"ssh"|"reconfigure"|"show"|"restart"|"stop")
        EC3_CLUSTERS=$(ls $EC3_CLUSTER_STORE_PATH)

        COMPREPLY=( $(compgen -W '$EC3_CLUSTERS' -- $cur ) )
        return 0
        ;;

    esac
} &&
complete -F _ec3 ec3

