# 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()
{
  COMPREPLY=()
  local cur="${COMP_WORDS[COMP_CWORD]}"
  local prev="${COMP_WORDS[COMP_CWORD-1]}"
  local short_actions="-V -H -S -G -d -q"
  local long_actions="--quiet --version --help --set --get --quiet --delay"
  local short_set_options="-m -n -t -p -g"
  local short_get_options="-c -r"
  local long_set_options="--max --min --turbo --plan --governor"
  local long_get_options="--current --real"
  local governors=""
  governors="$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors)"

  local plans=""
  local prefix='??-'
  local suffix='.plan'
  local POWER_PLAN_CONFIG_DIR="/etc/pstate-frequency.d"
  for plan in "${POWER_PLAN_CONFIG_DIR}"/*.plan; do
    # Strip all the fancy stuff from the file name to just get the plan name
    stripped="${plan#$POWER_PLAN_CONFIG_DIR/}"
    stripped="${stripped#$prefix}"
    stripped="${stripped%$suffix}"
    plans="$plans ${stripped}"
  done

  if [[ ${cur} = --* ]]; then
    if [[ ${prev} = "--set" ]] || [[ ${prev} = "-S" ]]; then
      COMPREPLY=( $(compgen -W "${long_set_options}" -- "${cur}") )
    elif [[ ${prev} = "--get" ]] || [[ ${prev} = "-G" ]]; then
      COMPREPLY=( $(compgen -W "${long_get_options}" -- "${cur}") )
    elif [[ ${prev} = "--plan" ]] || [[ ${prev} = "-p" ]]; then
      COMPREPLY=( $(compgen -W "${plans}" -- "${cur}") )
    else
      COMPREPLY=( $(compgen -W "${long_actions}" -- "${cur}") )
    fi
  elif [[ ${cur} = * ]]; then
    if [[ ${prev} = "--set" ]] || [[ ${prev} = "-S" ]]; then
      COMPREPLY=( $(compgen -W "${short_set_options}" -- "${cur}") )
    elif [[ ${prev} = "--get" ]] || [[ ${prev} = "-G" ]]; then
      COMPREPLY=( $(compgen -W "${short_get_options}" -- "${cur}") )
    elif [[ ${prev} = "--plan" ]] || [[ ${prev} = "-p" ]]; then
      COMPREPLY=( $(compgen -W "${plans}" -- "${cur}") )
    elif [[ ${prev} = "--governor" ]] || [[ ${prev} = "-g" ]]; then
      COMPREPLY=( $(compgen -W "${governors}" -- "${cur}") )
    else
      COMPREPLY=( $(compgen -W "${short_actions}" -- "${cur}") )
    fi
  fi

  unset cur
  unset prev
  unset short_actions
  unset long_actions
  unset short_set_options
  unset short_get_options
  unset long_set_options
  unset long_get_options
  unset plans
  unset governors
}

complete -F _pstate-frequency pstate-frequency
