#compdef enable disable emergency login-update-utmpx normal per-user-manager power(off|cycle) preset reboot rescue reset shutdown start stop utx -p (init|system|rc)ctl (invoke|update)-rc.d (tel|sys|)init chk(config|service) halt(|sys) rc-(service|update) sv(show|ok|up|stat|c|scan|cadm) -P service-(status|control|manager|-is-(up|ok|enabled)|show|dt-scanner) system-(control|manager) 
## **************************************************************************
## For copyright and licensing terms, see the file named COPYING.
## **************************************************************************
#
# Z Shell command-line completion

function _system_control_subcommands()
{
	local -a commands0 commands1 commands2 commands3 commands4
	commands0=(
	'is-service-manager-client:query whether the service manager is running'
	'version:output version information'
	)
	_describe -t system-control-command0 'system-control sub-commands' commands0
	commands1=(
	'emergency:start emergency mode'
	'halt:halt the system'
	'init:run initial action'
	'normal:start normal mode'
	'poweroff:power off the system'
	'powercycle:power cycle the system'
	'reboot:reboot the system'
	'rescue:start rescue mode'
#	'sysinit:run initial action'
	)
	_describe -t system-control-command1 'system-control system managament sub-commands' commands1
	commands2=(
	'activate:start a service'
	'condrestart:send a TERM signal to a running service'
	'deactivate:stop a service'
	'force-reload:send a TERM signal to a running service'
	'hangup:send a HUP signal to a running service'
	'is-active:query whether a service is started'
	'is-enabled:query whether a service is enabled'
	'is-loaded:query whether a service is loaded'
	'isolate:start a service'
	'reset:reset a service to its preset state'
	'start:start a service'
	'status:display service status'
	'stop:stop a service'
	'try-restart:send a TERM signal to a running service'
	'unload-when-stopped:set a service to unload when it next stops'
	)
	_describe -t system-control-command2 'system-control service control/status sub-commands' commands2
	commands3=(
	'cat:print a service bundle'\''s control scripts'
	'disable:configure a service to not auto-start'
	'enable:configure a service to auto-start'
	'find:locate a service'\''s service directory'
	'preset:configure a service auto-start according to config files'
	'print-service-env:display a standard-format env/ directory for a service'
	'print-service-scripts:print a service bundle'\''s control scripts'
	'set-service-env:change a standard-format env/ directory for a service'
	'show-json:print machine-readable service status'
	'show:print machine-readable service status'
	)
	_describe -t system-control-command3 'system-control service configuration sub-commands' commands3
	commands4=(
	'convert-fstab-services:convert fstab to service bundles'
	'convert-systemd-units:convert systemd unit files to a service bundle'
	'escape:escape names as the conversion utilities do'
	'get-mount-what:find the /etc/fstab mount "what" from its "where"'
	'get-mount-where:find the /etc/fstab mount "where" from its "what"'
	'load-kernel-module:load a kernel module'
	'nagios-check-service:nagios plug-in for checking services'
	'unload-kernel-module:unload a kernel module'
	'write-volume-service-bundles:convert an fstab record to service bundles'
	)
	_describe -t system-control-command4 'system-control utility sub-commands' commands4
}

function _chkconfig_subcommands()
{
	local -a commands
	commands=(
	'off:configure a service to not auto-start'
	'on:configure a service to auto-start'
	'reset:configure a service auto-start according to config files'
	)
	_describe -t command 'sub-commands' commands
}

#function _update_rc_d_subcommands()
#{
#	local -a commands
#	commands=(
#	'enable:configure a service to not auto-start'
#	'disable:configure a service to auto-start'
#	'defaults:configure a service auto-start according to config files'
#	)
#	_describe -t command 'sub-commands' commands
#}

function _rc_update_subcommands()
{
	local -a commands
	commands=(
	'add:configure a service to not auto-start'
	'del:configure a service to auto-start'
	)
	_describe -t command 'sub-commands' commands
}

function _initctl_subcommands()
{
	local -a initctl_commands
	initctl_commands=(
	'show-config:print machine-readable service status'
	'start:start a service'
	'status:display service status'
	'stop:stop a service'
	'version:show the version'
	)
	_describe -t initctl_command 'initctl sub-commands' initctl_commands
	_system_control_subcommands
}

function _svcadm_subcommands()
{
	local -a svcadm_commands
	svcadm_commands=(
	'enable:start a service'
	'disable:stop a service'
	'version:show the version'
	)
	_describe -t svcadm_command 'svcadm sub-commands' svcadm_commands
	_system_control_subcommands
}

function _rcctl_subcommands()
{
	local -a rcctl_commands
	rcctl_commands=(
	'get:display a standard-format env/ directory for a service'
	'set:change a standard-format env/ directory for a service'
	)
	_describe -t rcctl_command 'rcctl sub-commands' rcctl_commands
	_system_control_subcommands
}

function _runlevels()
{
	local -a runlevels
	runlevels=(
	'0:poweroff'
	'1:rescue'
	'2:normal'
	'3:normal'
	'4:normal'
	'5:normal'
	'6:reboot'
	'm:normal'
	'auto:normal'
	'C:powercycle'
	'c:powercycle'
	'H:halt'
	'h:halt'
	'S:rescue'
	's:rescue'
	'single:rescue'
	'B:emergency'
	'b:emergency'
	'emergency:emergency'
	)
	_describe -t runlevel 'run levels' runlevels
}

function _utx_types()
{
	local -a types
	types=(
	'boot:add boot-time entry'
	'reboot:add boot-time entry'
	'shutdown:add shutdown entry'
	)
	_describe -t type 'types' types
}

function _service_bundles()
{
	local -a dir
	dir=(/usr/local/etc/service-bundles/services /usr/local/etc/service-bundles/targets /var/local/service-bundles/services /var/local/service-bundles/targets /var/local/sv /etc/service-bundles/services /etc/service-bundles/targets /run/service-bundles/services /run/service-bundles/targets /var/service-bundles/services /var/service-bundles/targets /var/sv /var/svc.d /service)
	_path_files -/ -W dir
}

function _system_control_common()
{
	local lstate state
	local -a common

	common=(-s '--help[Display option help]' '--usage[Display option usage]')

	case "$service" in
		emergency|rescue|normal|halt|power(off|cycle)|reboot|convert-fstab-services)
			_arguments -S $common -- ;;
		init)
			_arguments -S $common '1:run level:_runlevels' -- ;;
		(un|)load-kernel-module)
			_arguments -S $common '1:module name:_files' -- ;;
		write-volume-service-bundle)
			_arguments -A '-*' $common '1:fstype:' '2:what:_files' '3:where:_directories' '4:options:' -- ;;
		get-mount-what)
			_arguments -A '-*' $common '1:where:_directories' -- ;;
		get-mount-where)
			_arguments -A '-*' $common '1:what:_files' -- ;;
		set-service-env)
			_arguments -A '-*' $common '1:service name:_service_bundles' '2:name:' '3:value:' -- ;;
		print-service-env)
			_arguments -A '-*' $common '1:service name:_service_bundles' '2:value:' -- ;;
		convert--units)
			_arguments -A '-*' $common '1:unit name:_files' -- ;;
		convert-systemd-units)
			_arguments -A '-*' $common '1:unit name:_files' -- ;;
		escape)
			_arguments -A '-*' $common '*:name:' -- ;;
		version)
			_arguments -A '-*' $common -- ;;
		*)
			_arguments -A '-*' $common ${1:+'*:service name:_service_bundles'} -- ;;
	esac
}

function _system_control_subargs()
{
	local service="$words[1]"
	local curcontext="${curcontext%:*:*}:system-control-$words[1]:" 
	_system_control_common "$@"
}

function _rcctl()
{
	local service="$words[1]"
	local curcontext="${curcontext%:*:*}:system-control-$words[1]:" 
	case "$service" in
		get)	service=print-service-env ;;
		set)	service=set-service-env ;;
	esac
	_system_control_common "$@"
}

function _initctl()
{
	local service="$words[1]"
	local curcontext="${curcontext%:*:*}:system-control-$words[1]:" 
	case "$service" in
		show-config)	service=show-json ;;
	esac
	_system_control_common "$@"
}

function _svcadm()
{
	local service="$words[1]"
	local curcontext="${curcontext%:*:*}:system-control-$words[1]:" 
	case "$service" in
		enable) 	service=start ;;
		disable)	service=stop ;;
	esac
	_system_control_common "$@"
}

function _update_rc_d_subcommands()
{
	local service="$words[1]"
	local curcontext="${curcontext%:*:*}:system-control-$words[1]:" 
	case "$service" in
		defaults)	service=preset ;;
	esac
	_system_control_common "$@"
}

function _system_control()
{
	local lstate state
	local -a common
	common=(-s '--help[Display option help]' '--usage[Display option usage]')
	local next='*:::next prog:_normal'

	case "${service}" in
		(service|per-user|system)-manager)
			_message -r "${service} has no options or parameters." ;;
		systemctl|system-control) 
			_arguments -A '-*' $common '1:subcommand:_system_control_subcommands' '*::subargs:_system_control_subargs YES' -- ;;
		rcctl)
			_arguments -A '-*' $common '1:subcommand:_rcctl_subcommands' '*::subargs:_rcctl' -- ;;
		initctl)
			_arguments -A '-*' $common '1:subcommand:_initctl_subcommands' '*::subargs:_initctl' -- ;;
		svcadm)
			_arguments -A '-*' $common '1:subcommand:_svcadm_subcommands' '*::subargs:_svcadm' -- ;;
		update-rc.d) 
			_arguments -A '-*' $common '1:service name:_service_bundles' '2:subcommand:_update_rc_d_subcommands' -- ;;
		rc-update) 
			_arguments -A '-*' $common '1:service name:_service_bundles' '2:subcommand:_rc_update_subcommands' -- ;;
		chkconfig) 
			_arguments -A '-*' $common '1:service name:_service_bundles' '2:subcommand:_chkconfig_subcommands' -- ;;
		(rc-|)service|invoke-rc.d)
			_arguments -A '-*' $common '1:service name:_service_bundles' '*:::subargs:_system_control' -- ;;
		(tel|)init)
			_arguments -S $common '1:runlevel:_runlevels' -- ;;
		utx|login-update-utmpx)
			_arguments -S $common '1:type:_utx_types' -- ;;
		service-(control|status|is-(ok|up|enabled)|show)|sv(up|c|ok|show|stat))
			_arguments -A '-*' '*:service directory:_directories' -- ;;
		svscan|service-dt-scanner)
			_arguments -A '-*' $common '1:scan directory:_directories' -- ;;
		shutdown)
			_arguments -A '-*' $common '1:time:' '2:message:' -- ;;
		*)
			_arguments -A '-*' $common -- ;;
	esac
}

_system_control "$@"
