#compdef pstate-frequency

# The GPLv2 License
#
#   Copyright (C) 2017  Peter Kenji Yamanaka
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

_pstate-frequency()
{
  typeset -A opt_args
  _arguments \
    "1: :_actions" \
    "*: :_options"
}

_actions()
{
  _alternative \
    "unprivilaged-actions:unprivilaged actions:_unprivilaged_actions" \
    "privilaged-actions:privilaged actions:_privilaged_actions"
}

_options()
{
 # Array size ${#words}
 local index
 let "index=${#words} - 1"
 if [ "$words[$index]" = "-S" ] || [ "$words[$index]" = "--set" ]; then
   _alternative \
     "privilaged-options:privilaged options:_privilaged_options"
 elif [ "$words[$index]" = "-G" ] || [ "$words[$index]" = "--get" ]; then
   _alternative \
     "unprivilaged-options:unprivilaged options:_unprivilaged_options"
 elif [ "$words[$index]" = "-g" ] || [ "$words[$index]" = "--governor" ]; then
   _alternative \
     ":cpu governor name:"
 elif [ "$words[$index]" = "-m" ] || [ "$words[$index]" = "--max" ]; then
   _alternative \
     ":max freq between 0 and 100:"
 elif [ "$words[$index]" = "-n" ] || [ "$words[$index]" = "--min" ]; then
   _alternative \
     ":min freq between 0 and 100:"
 elif [ "$words[$index]" = "-t" ] || [ "$words[$index]" = "--turbo" ]; then
   _alternative \
     ":turbo boost either on or off:"
 elif [ "$words[$index]" = "-p" ] || [ "$words[$index]" = "--plan" ]; then
   _alternative \
     ":power plan name :"
 else
   _actions
 fi
}

_unprivilaged_options()
{
  # Add long option support
  local options;
  options=(
    "-c:Get the current CPU values"
    "-r:Get real-time CPU frequencies"
  )
  _describe -t unprivilaged-options "unprivilaged options" options "$@"
}

_privilaged_options()
{
  # Add long option support
  local options;
  options=(
    "-p:Set a pre-defined plan"
    "-m:Set the CPU max"
    "-n:Set the CPU min"
    "-t:Set the CPU turbo"
    "-g:Set the CPU governor"
  )
  _describe -t privilaged-options "privilaged options" options "$@"
}


_unprivilaged_actions()
{
  # Add long option support
  local actions;
  actions=(
    "-H:Print the help output"
    "-V:Print the current version"
    "-G:Access the current CPU values"
    "-q:Supress non-error output"
    "-d:Enable debugging mode"
    "--delay:Delay operation for 5 seconds"
  )
  _describe -t unprivilaged-actions "unprivilaged actions" actions "$@"
}

_privilaged_actions()
{
  # Add long option support
  local actions;
  actions=(
    "-S:Modify the current CPU values"
  )
  _describe -t privilaged-actions "privilaged actions" actions "$@"
}

_pstate-frequency "$@"

