#!/usr/bin/env bash
# ABOUTME: Walker-dmenu CPU governor switcher using auto-cpufreq.
# ABOUTME: Allows switching between performance, powersave, and reset modes.

declare -A LABELS
declare -A COMMANDS

###
# List of defined 'bangs'

COMMANDS["󰓅"]="pkexec auto-cpufreq --force=performance"
LABELS["󰓅"]="Performance"

COMMANDS["󰌪"]='pkexec auto-cpufreq --force=powersave'
LABELS["󰌪"]="Powersave"

COMMANDS[""]='pkexec auto-cpufreq --force=reset'
LABELS[""]="Reset"


################################################################################
# do not edit below
################################################################################
##
# Generate menu
##
function print_menu()
{
    for key in ${!LABELS[@]}
    do
    #  echo "$key    ${LABELS}"
        echo "$key  ${LABELS[$key]}"
     # my top version just shows the first field in labels row, not two words side by side
    done
}
##
# Show rofi.
##
function start()
{
    print_menu | sort | walker -d -p "󰓅  CPU Mode"
}


# Run it
value="$(start)"

# Split input.
# grab upto first space.
choice=${value%%\ *}

##
# Cancelled? bail out
##
if test -z "${choice}"
then
    exit
fi

# check if choice exists
if test "${COMMANDS[$choice]+isset}"
then
    ${COMMANDS[$choice]}

    notify-send -h string:x-canonical-private-synchronous:cpugov -i cpu "CPU Mode" "Set to $choice ${LABELS[$choice]}"

    # Signal Waybar to refresh the cpugov module (SIGRTMIN+10)
    pkill -RTMIN+10 waybar
else
    notify-send -u critical "CPU Governor" "Unknown command: ${choice}"
fi
