_connect_complete_subcommand ()
{
    local command="${COMP_WORDS[1]}"
    local subcommands="$(${COMP_WORDS[0]} $command --help 2> /dev/null | awk '/^    [a-z]/ { print $1 }')"
    COMPREPLY=($(compgen -W "$subcommands" -- "${COMP_WORDS[$COMP_CWORD]}"))
}

_connect_complete_host ()
{
    local prefix="$1"
    if [ -z "$_connect_host" ]
    then
	# Cache the complete list because it rarely changes and makes
	# completion much faster.
	_connect_host="$((awk '/\.intr/ { print $1 }' $HOME/.ssh/known_hosts | cut -d: -f 1 | sed -e 's/\[//' -e 's/\]//' | cut -d, -f 1) 2> /dev/null)"
    fi
    COMPREPLY=($(compgen -W "$_connect_host" -- "$prefix"))
}

_connect_complete_option ()
{
    local subcommand
    case "${COMP_WORDS[2]}" in
	-*)     subcommand="";;
	[a-z]*) subcommand="${COMP_WORDS[2]}";;
    esac
    local options="$(${COMP_WORDS[0]} ${COMP_WORDS[1]} $subcommand --help 2> /dev/null \
                            | grep '^  \+-' \
                            | sed -e's/^.*--\([a-zA-Z0-9_-]\+\)\(=\?\).*/--\1\2/g')"
    compopt -o nospace
    COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[${#COMP_WORDS[*]} - 1]}"))
}

_connect_is_command ()
{
    local word
    local result="false"
    for word in ${COMP_WORDS[*]}
    do
	if [ "$word" = "$1" ]
	then
	    result=true
	    break
	fi
    done
    $result
}

_connect_complete()
{
    local word_count=${#COMP_WORDS[*]}
    local word_at_point="${COMP_WORDS[$COMP_CWORD]}"

    if [ "$COMP_CWORD" -gt 1 ]
    then
	case "$word_at_point" in
	    -*)
		_connect_complete_option "$word_at_point"
		return
		;;
	esac
    fi

    case $COMP_CWORD in
        1)
	    if [ -z "$_connect_subcommands" ]
	    then
		# Cache the list of subcommands to speed things up.
		_connect_subcommands="$(connect --help 2> /dev/null | awk '/^    [a-z]/ { print $1 }')"
	    fi
            COMPREPLY=($(compgen -W "$_connect_subcommands" -- "$word_at_point"))
            ;;
        *)
            if _connect_is_command "containers"                 \
                    || _connect_is_command "deploy"             \
                    || _connect_is_command "emacs"              \
                    || _connect_is_command "filter"             \
                    || _connect_is_command "firewall"           \
                    || _connect_is_command "images"             \
                    || _connect_is_command "interfaces"         \
                    || _connect_is_command "io"                 \
                    || _connect_is_command "ip"                 \
                    || _connect_is_command "mysql"              \
                    || _connect_is_command "net"                \
                    || _connect_is_command "nginx"              \
                    || _connect_is_command "nginx-config"       \
                    || _connect_is_command "ping"               \
                    || _connect_is_command "php"                \
                    || _connect_is_command "protected"          \
                    || _connect_is_command "route"              \
                    || _connect_is_command "sg"                 \
                    || _connect_is_command "shell"              \
                    || _connect_is_command "sniff"              \
                    || _connect_is_command "ssh"                \
                    || _connect_is_command "sshrc"              \
                    || _connect_is_command "te"                 \
                    || _connect_is_command "traceroute"         \
                    || _connect_is_command "unfilter"           \
                    || _connect_is_command "uptime"
            then
                _connect_complete_host "$word_at_point"
            elif _connect_is_command "br1-mr14.intr" \
                    || _connect_is_command "sr1-mr13-14.intr" \
                    || _connect_is_command "sr1-dh507-508.intr"
            then
                COMPREPLY=($(compgen -W "configuration bgp log" -- "${COMP_WORDS[$COMP_CWORD]}"))
            elif _connect_is_command "ip-filter"
            then
                case $COMP_CWORD in
		    2) _connect_complete_subcommand;;
                esac
            fi
            ;;
    esac
}

complete -F _connect_complete connect
