# bash completion for rolectl                             -*- shell-script -*-

# Copyright (C) 2014 Red Hat, Inc.
#
# Authors:
# Thomas Woerner <twoerner@redhat.com>
#
# 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, see <http://www.gnu.org/licenses/>.
#

COMMANDS="version list settings deploy redeploy status start stop restart update decommission reset-error sanitize"

_contains() {
    local y z=$1; shift
    for y in "$@"; do
	[[ "$y" = "$z" ]] && return 0
    done
    return 1
}

_list_roles() {
    rolectl list roles
}

_list_instances() {
    rolectl list instances
}

_settings_keys() {
    rolectl settings "$what" | awk '{print $1}'
}

_rolectl()
{
    local command cur prev comps i x roles instances
    _init_completion -s -n : || return
    _expand || return 0

    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}
    command=""
    comps=""

    for ((i=1; i<COMP_CWORD; i++)); do
	for x in ${COMMANDS}; do
	    if [[ "${COMP_WORDS[i]}" = "$x" ]]; then
		command=$x
		what=${COMP_WORDS[i+1]}
		break;
	    fi
	done
    done

    if [ -z "$command" ]; then
	comps="${COMMANDS}"

    elif [ "$command" = "list" ]; then
	if [ "$what" != "roles" -a "$what" != "instances" ]; then
	    comps="roles instances"
	fi

    elif [ "$command" = "settings" ]; then
	roles=$(_list_roles)
	instances=$(_list_instances)

	if ! _contains "$what" $roles && ! _contains "$what" $instances; then
	    comps="$roles $instances"
	else
	    if [ "$prev" = "--key" ]; then
		comps=$(_settings_keys)
	    elif ! _contains "--key" ${COMP_WORDS[*]}; then
		comps="--key"
	    fi
	fi

    elif [ "$command" = "deploy" ]; then
	roles=$(_list_roles)

	if ! _contains "$what" $roles; then
	    comps="$roles"
	else
	    if [ "$prev" = "--name" ]; then
		comps=""
	    elif [ "$prev" = "--settings-file" ]; then
		compopt +o nospace
		_filedir
		return 0
	    else
		comps=""
		if ! _contains "--deferred" ${COMP_WORDS[*]}; then
			comps+="--deferred "
		fi
		if ! _contains "--name" ${COMP_WORDS[*]}; then
		    comps+="--name "
		fi
		if ! _contains "--settings-file" ${COMP_WORDS[*]} && \
			! _contains "--settings-stdin" ${COMP_WORDS[*]}; then
		    comps+="--settings-file --settings-stdin "
		fi
	    fi
	fi

    elif [ "$command" = "redeploy" ]; then
	instances=$(_list_instances)

	if ! _contains "$what" $instances; then
	    comps="$instances"
	else
	    if [ "$prev" = "--settings-file" ]; then
		compopt +o nospace
		_filedir
		return 0
	    else
		comps=""
		if ! _contains "--settings-file" ${COMP_WORDS[*]} && \
			! _contains "--settings-stdin" ${COMP_WORDS[*]}; then
		    comps+="--settings-file --settings-stdin "
		fi
	    fi
	fi

    elif [ "$command" = "status" ]; then
	instances=$(_list_instances)

	if ! _contains "$what" $instances; then
	    comps="$instances"
	fi

    elif [  "$command" = "start" ]; then
	instances=$(_list_instances)

	if ! _contains "$what" $instances; then
	    comps="$instances"
	fi

    elif [ "$command" = "stop" ]; then
	instances=$(_list_instances)

	if ! _contains "$what" $instances; then
	    comps="$instances"
	fi

    elif [ "$command" = "restart" ]; then
	instances=$(_list_instances)

	if ! _contains "$what" $instances; then
	    comps="$instances"
	fi

    elif [ "$command" = "update" ]; then
	instances=$(_list_instances)

	if ! _contains "$what" $instances; then
	    comps="$instances"
	fi

    elif [ "$command" = "sanitize" ]; then
	instances=$(_list_instances)

	if ! _contains "$what" $instances; then
	    comps="$instances"
	fi

    elif [ "$command" = "reset-error" ]; then
	instances=$(_list_instances)

	if ! _contains "$what" $instances; then
	    comps="$instances"
	fi

    elif [ "$command" = "decommission" ]; then
	instances=$(_list_instances)

	if ! _contains "$what" $instances; then
	    comps="$instances"
	else
	    if ! _contains "--force" ${COMP_WORDS[*]}; then
		comps="--force"
	    fi
	fi
    fi

    COMPREPLY=($(compgen -o filenames -W '${comps}' -- "$cur"))

    return 0
} &&
complete -F _rolectl rolectl
