# @(#)bash_completion
#
#
#  Copyright (C) 2013, GC3, University of Zurich. All rights
#  reserved.
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2 of the License, or (at your
#  option) any later version.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

# Source this file when running bash in order to have tab completion
# for elasticluster.

__elasticluster_list_clusters () {
    /bin/ls ~/.elasticluster/storage/*.pickle  | sed 's:.*/::; s:.pickle::'
}

__elasticluster_list_templates () {
    elasticluster list-templates 2>/dev/null | grep ^name:|sed 's/name: *//g'
}

_elasticluster () {
    local cur prev opts
    local commands="start stop setup list list-nodes list-templates ssh sftp resize"
    local global_options="-h --help -v -s --storage -c --config --version --verbose"
    local start_options="-h --help -v --verbose -n --name --nodes --no-setup"
    local stop_options="-h --help -v --verbose --force --yes"
    local resize_options="-h --help -v --verbose -a --add -r --remove -t --template --no-setup --yes"
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    case ${prev} in
        elasticluster)
            if [ "`expr substr \"${cur}\" 1 1`" = "-" ]
            then
                COMPREPLY=($(compgen -W "${global_options}" -- ${cur}))
            else
                COMPREPLY=($(compgen -W "${commands}" -- ${cur}))
            fi
            ;;
        -s|--storage)
            COMPREPLY=($(compgen -o dirnames -o nospace -- ${cur}))
            ;;
        -c|--config)
            COMPREPLY=($(compgen -o default -o nospace -- ${cur}))
            ;;
        start)
            if [ "`expr substr \"${cur}\" 1 1`" = "-" ]
            then
                COMPREPLY=($(compgen -W "${start_options}" -- ${cur}))
            else
                COMPREPLY=($(compgen -W "$(__elasticluster_list_templates)" -- ${cur}))
            fi
            ;;
        stop)
            if [ "`expr substr \"${cur}\" 1 1`" = "-" ]
            then
                COMPREPLY=($(compgen -W "${stop_options}" -- ${cur}))
            else
                COMPREPLY=($(compgen -W "$(__elasticluster_list_clusters)" -- ${cur}))
            fi
            ;;
        resize)
            if [ "`expr substr \"${cur}\" 1 1`" = "-" ]
            then
                COMPREPLY=($(compgen -W "${resize_options}" -- ${cur}))
            else
                COMPREPLY=($(compgen -W "$(__elasticluster_list_clusters)" -- ${cur}))
            fi
            ;;
        setup|list-templates)
            COMPREPLY=($(compgen -W "$(__elasticluster_list_templates)" -- ${cur}))
            ;;
        setup|list-nodes|ssh|sftp)
            COMPREPLY=($(compgen -W "$(__elasticluster_list_clusters)" -- ${cur}))
            ;;
        *)
            COMPREPLY=()
            ;;
    esac
}

complete -F _elasticluster elasticluster
