#!/bin/sh

CURRENT=$(sudo cpupower frequency-info | grep 'current policy: frequency should be within 400 MHz and' | awk '{printf "%.2f%s", $10, substr($11, 1, length($11)-1)}')

set_cmd() {
  sudo cpupower frequency-set --max $1MHz
  notify-send --hint int:transient:1 -t 800 "$1MHz CPU Power"
}

if [ -z "$1" ]; then
  if [ $CURRENT = "400.00MHz" ]; then
    set_cmd 600
    exit 0
  fi

  if [ $CURRENT = "600.00MHz" ]; then
    set_cmd 1000
    exit 0
  fi

  if [ $CURRENT = "1000.00MHz" ]; then
    set_cmd 2000
    exit 0
  fi

  if [ $CURRENT = "2.00GHz" ]; then
    set_cmd 3000
    exit 0
  fi

  if [ $CURRENT = "3.00GHz" ]; then
    set_cmd 4200
    exit 0
  fi

  if [ $CURRENT = "4.20GHz" ]; then
    set_cmd 400
    exit 0
  fi
else
  if [ "$1" = "g" ]; then
    case $CURRENT in
      "400.00MHz") echo 1 ;;
      "600.00MHz") echo 2 ;;
      "1000.00MHz") echo 3 ;;
      "2.00GHz") echo 4 ;;
      "3.00GHz") echo 5 ;;
      "4.20GHz") echo 6 ;;
      *) echo "Unknown frequency" ;;
    esac
    exit 0
  fi
  case $1 in
    1) set_cmd 400 ;;
    2) set_cmd 600 ;;
    3) set_cmd 1000 ;;
    4) set_cmd 2000 ;;
    5) set_cmd 3000 ;;
    6) set_cmd 4200 ;;
  esac
  exit 0
fi

notify-send --hint int:transient:1 CPU-Power Error: value is $CURRENT
exit 1
