# completion for expresso
# by i0ntempest

_expresso_completions()
{
    local cur prev opts 
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="-h --help --version -v --verbose -q --quiet -t --timeout"
    verbs="connect disconnect locations alfred repl status"

    verbused=0
    for word in "${COMP_WORDS[@]}"; do
        if [[ "${verbs}" =~ $word ]]; then
            verbused=1
        fi
    done

    if [[ "${verbs}" =~ ${prev} || "${cur}" == -* ]]; then
	    COMPREPLY=($(compgen -W "${opts}" -- "${cur}"))
	elif [[ $verbused == 0 || ${COMP_CWORD} -eq 1 ]]; then
	    COMPREPLY=($(compgen -W "${verbs}" -- "${cur}"))
    fi

    case "${prev}" in
	-t|--timeout|-h|--help|--version)
	    COMPREPLY=()
	    ;;
	connect)
	    vpnlocations="$(expresso locations)"
	    if [[ $? == 0 ]]; then
	        vpnlocations="$(sed 's/[-()]//g' <<< $vpnlocations)"
	    else
	        vpnlocations=""
	    fi
	    COMPREPLY=($(compgen -W "${vpnlocations,,} -c --change --random --toggle" -- "${cur}"))
	    ;;
	alfred)
	    COMPREPLY=($(compgen -W "-v --verbose -q --quiet --locations" -- "${cur}"))
    esac
}

complete -F _expresso_completions expresso
