#compdef rfkill
# ------------------------------------------------------------------------------
# Copyright (c) 2014 Vincent Bernat <bernat@luffy.cx>
# Copyright (c) 2014 Github zsh-users - http://github.com/zsh-users
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for rfkill (http://wireless.kernel.org/en/users/Documentation/rfkill)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Vincent Bernat <bernat@luffy.cx>
#
# ------------------------------------------------------------------------------

_rfkill_types() {
  declare -a devicetypes
  devicetypes=(all
    "wifi:Wireless LAN" "wlan:Wireless LAN"
    "bluetooth:Bluetooth"
    "uwb:Ultrawide Band"
    "ultrawideband:Ultrawide Band"
    "wimax:Wimax"
    "wwan:3G"
    "gps:GPS"
    "fm:FM Radio"
    "nfc:NFC")
  _describe -t device-types "device types" devicetypes
}

_rfkill_devices() {
  declare -a devices
  devices=(${(M)${(f)"$(rfkill list)"}:#[0-9]*})
  _rfkill_types
  _describe -t devices "devices" devices
}

_rfkill_commands () {
  declare -a subcommands
  subcommands=(help event list block unblock)
  _describe -t rfkill-commands "rfkill command" subcommands
}

_rfkill_subcommand () {
  case "$words[1]" in
    (help|event)
      ;;
    (list)
      _arguments ':types:_rfkill_types'
      ;;
    (block|unblock)
      _arguments ':device:_rfkill_devices'
      ;;
    (*)
      _message 'Unknown subcommand'
  esac
}

_rfkill () {
  local curcontext="$curcontext" state line
  typeset -A opt_args

  _arguments -C \
    '--version[get version]:' \
    '(-): :->command' \
    '(-)*:: :->arguments'

  case $state in
    (command)
      _rfkill_commands
      ;;
    (arguments)
      curcontext=${curcontext%:*:*}:rfkill-$words[1]:
      _rfkill_subcommand
      ;;
  esac
}

_rfkill "$@"

# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et
