_herd_complete_subcommand()
{
    local command="${COMP_WORDS[1]}"
    case "$command" in
        discover)
            complitions="guix-daemon"
            COMPREPLY=($(compgen -W "$complitions" -- "${COMP_WORDS[$COMP_CWORD]}"))
            ;;
        schedule)
            complitions="mcron"
            COMPREPLY=($(compgen -W "$complitions" -- "${COMP_WORDS[$COMP_CWORD]}"))
            ;;
        set-http-proxy)
            complitions="guix-daemon"
            COMPREPLY=($(compgen -W "$complitions" -- "${COMP_WORDS[$COMP_CWORD]}"))
            ;;
        *)
            local services="$(herd status | grep '^ +\|^ -\| \*' | cut -d ' ' -f3)"
            COMPREPLY=($(compgen -W "$services" -- "${COMP_WORDS[$COMP_CWORD]}"))
            ;;
    esac
}

_herd_complete_3()
{
    local command="${COMP_WORDS[2]}"
    case "$command" in
        guix-daemon)
            complitions="on off"
            COMPREPLY=($(compgen -W "$complitions" -- "${COMP_WORDS[$COMP_CWORD]}"))
            ;;
        *)
            ;;
    esac
}

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

_herd_complete()
{
    local word_count=${#COMP_WORDS[*]}
    local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
    case $COMP_CWORD in
        1)
	    if [ -z "$_herd_subcommands" ]
	    then
		# Cache the list of subcommands to speed things up.
                _herd_subcommands=(
                    disable
                    discover
                    enable
                    schedule
                    set-http-proxy
                    start
                    status
                    stop
                )
		_herd_subcommands="${_herd_subcommands[@]}"
	    fi
            COMPREPLY=($(compgen -W "$_herd_subcommands" -- "${COMP_WORDS[$COMP_CWORD]}"))
            ;;
        *)
            case $COMP_CWORD in
		2) _herd_complete_subcommand;;
		3) _herd_complete_3;;
	    esac
            ;;
    esac
}

complete -F _herd_complete herd
