comp_include _get_cword _services


_service()
{
    local cur sysvdir

    COMPREPLY=()
    prev=${COMP_WORDS[COMP_CWORD-1]}
    cur=`_get_cword`

    # don't complete for things like killall, ssh and mysql if it's
    # the standalone command, rather than the init script
    [[ ${COMP_WORDS[0]} != @(*init.d/!(functions|~)|service) ]] && return 0

    # don't complete past 2nd token
    [ $COMP_CWORD -gt 2 ] && return 0

    [ -d /etc/rc.d/init.d ] && sysvdir=/etc/rc.d/init.d \
                || sysvdir=/etc/init.d

    if [[ $COMP_CWORD -eq 1 ]] && [[ $prev == "service" ]]; then
        _services
    else
        COMPREPLY=( $( compgen -W '`sed -ne "y/|/ /; \
                s/^.*Usage.*{\(.*\)}.*$/\1/p" \
                $sysvdir/${prev##*/} 2>/dev/null`' -- $cur ) )
    fi

    return 0
}
