#compdef rc-update rc-status rc

# baselayout 1.12.11.1
#
# rc        (fully done)
# rc-update (fully done)
# rc-status (fully done)


local gentoo_runlevels
gentoo_runlevels=(/etc/runlevels/*(:t))


# Stuff for rc
_rc () {
	if (( CURRENT == 2 )); then
	    _values "runlevels" $gentoo_runlevels
	fi
}


# Stuff for rc-status
_rc-status () {
    _arguments -s                                                                       \
	    {'(--all)-a','(-a)--all'}'[Show services at all run levels]'                    \
	    {'(--list)-l','(-l)--list'}'[Show list of run levels]'                          \
	    {'(--nocolor)-nc','(-nc)--nocolor'}'[Disable color output]'                     \
	    {'(--servicelist)-s','(-s)--servicelist'}'[Show all services]'                  \
	    {'(--unused)-u','(-u)--unused'}'[Show services not assigned to any run level]'

	_values 'runlevels' $gentoo_runlevels
}


# Stuff for rc-update
_rc-update () {
    local used_init

    used_init=(${=${(M)${(f)"$(rc-update show 2>/dev/null)"}:#*|*[a-z]*}% |*})

    if (( CURRENT == 2 )); then
       _values "rc-update actions"                      \
           'add[Add script to a runlevel]'              \
           'del[Delete script from a runlevel]'         \
           'show[Show scripts lanched at a runlevel]'   \
           '-a[Add script to a runlevel]'               \
           '-d[Delete script from a runlevel]'          \
           '-s[Show scripts lanched at a runlevel]'
    elif (( CURRENT == 3 )); then
	    case "$words[2]" in
		    add|-a)
		         _values "scripts" /etc/init.d/*~*.sh(:t)
		    ;;
		    del|-d)
		        _values "scripts" $used_init
		    ;;
		    show|-s)
		        _values "runlevels" $gentoo_runlevels   \
                    '-v[Show all init scripts]'         \
                    '--verbose[Show all init scripts]'
		    ;;
	    esac
    elif (( CURRENT == 4 )); then
	    _values "runlevels" $gentoo_runlevels
    fi
}


case "$service" in
	rc-update)
		_rc-update "$@" && return 0
		;;
	rc-status)
		_rc-status "$@" && return 0
		;;
	rc)
		_rc "$@" && return 0
		;;
esac

