#compdef nitroctl

# zsh completion for nitroctl
# Leah Neukirchen <leah@vuxu.org>, public domain
#
# To use with "sudo nitroctl", run:
#   zstyle ':completion:*:nitroctl/*' gain-privileges yes

_arguments \
  '-v[verbose]' \
  '-t[timeout]:seconds' \
  '1: :->command' \
  '*:: :->options'

case $state in
  (command)
    if [[ $words[CURRENT] = r ]]; then
      compadd -M 'l:|=*' fast-restart
      return
    fi
    _values "sv command" \
      'up[request services to be UP]' \
      'down[request services to be DOWN]' \
      'pause[send SIGSTOP to services]' \
      'cont[send SIGCONT to services]' \
      'hup[send SIGHUP to services]' \
      'alarm[send SIGALRM to services]' \
      'interrupt[send SIGINT to services]' \
      'quit[send SIGQUIT to services]' \
      'term[send SIGTERM to services]' \
      'kill[send SIGKILL to services]' \
      '1[send SIGUSR1 to services]' \
      '2[send SIGUSR2 to services]'
    # above allow one-character shortcuts, complete these first
    [[ $words[CURRENT] = [udpchaiqtk12] ]] ||
    _values "sv command" \
      'list[print service status]' \
      {scan,rescan}'[rescan the service list from the file system]' \
      'start[start, wait for services to be UP]' \
      'stop[stop, wait for services to be DOWN]' \
      'restart[restart, wait for services to be UP]' \
      'fast-start[start, wait for services to be STARTING]' \
      'fast-stop[stop, wait for services to be SHUTDOWN]' \
      'fast-restart[restart, wait for services to be RESTART]' \
      'ready[mark services to be UP if they are STARTING]' \
      'wait-up[wait for services to be UP]' \
      'wait-down[wait for services to be DOWN]' \
      'wait-starting[wait for services to be STARTING]' \
      'pidof[print PIDs of services]' \
      'check[exit with status 0 if services are running]' \
      'Reboot[reboot the system]' \
      'Shutdown[shutdown the system]'
  ;;
  (options)
    case $words[1] in
      (scan|rescan|Reboot|Shutdown)
        return 1
      ;;
      (*)
        [[ $words[CURRENT] = */* ]] && _directories && ret=0
        local -a services=( ${${${(f)"$(_call_program -p services nitroctl list 2>/dev/null)"}#* }%% *} )
        _describe -t services "nitro service" services
      ;;
    esac
  ;;
esac
