#!/bin/bash
#----------------------------------------------------------------------#
# CPUthrottle - Set Intel and AMD cpu power options -------------------#
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License.                #
#----------------------------------------------------------------------#

# Run as root
USERS=$(who | awk '{print $1}' | head -n1)
ICON="/usr/share/cputhrottle/icons/cputhrottle.svg"
MODEL=$(awk -F: '/model name/ {gsub(/^ +/, "", $2); model=$2} END{print model}' /proc/cpuinfo)
CPU=$(awk '/vendor_id/ {print $3; exit}' /proc/cpuinfo)
DESKTOP="Desktop: $XDG_CURRENT_DESKTOP"
KERNEL=$(< /proc/sys/kernel/osrelease)
SETTING=$(< /sys/devices/system/cpu/cpufreq/policy0/scaling_governor)
PREF=$(cat /usr/share/cputhrottle/menu/cpu_info| sed -r 's/([[:space:]]+).*/\1/')
CANCEL="/usr/share/cputhrottle/icons/cancel.svg"
CORES=$(cat /proc/cpuinfo | grep -E "cpu cores" | awk '{ print $0 }'| tail -n1|sed 's/^[^:]*://'| sed 's/^ *//g')
cpupower --cpu 0 frequency-info g | grep "analyzing\|governors" > /tmp/governors

function menu {
  if [ "$CPU" == "AuthenticAMD" ]; then
    exec "$@"
  else
    yad --title='CPUthrottle' --width=425 --height=100 --fixed --center --on-top \
      --text="\nThis application works only with newer AMD CPUs!" --text-align=left \
      --window-icon="$ICON" \
      --image="$ICON" \
      --button=Close!"$CANCEL"
    exit 0
  fi

  if [ ! -f /usr/share/cputhrottle/menu/cpu_info ]; then
    echo "No Menu Set" > /usr/share/cputhrottle/menu/cpu_info
  fi

  case "$PREF" in
    performance)
      echo performance > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor
      echo "Performance enabled" > /usr/share/cputhrottle/menu/cpu_info
      ;;
    ondemand)
      echo ondemand > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor
      echo "Ondemand enabled" > /usr/share/cputhrottle/menu/cpu_info
      ;;
    userspace)
      echo userspace > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor
      echo "Userspace enabled" > /usr/share/cputhrottle/menu/cpu_info
      ;;
    powersave)
      echo powersave > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor
      echo "Powersave enabled" > /usr/share/cputhrottle/menu/cpu_info
      ;;
  esac

  select=$(yad --height=100 --tabnum=3 --columns=3 --width=450 --height=300 --fixed --center --text="$MODEL\n<span font_desc='Sans Bold Italic 10' foreground='#BE7F00'>cpu cores $CORES</span>\nKernel: $KERNEL\n$DESKTOP\n <span color=\"#717ED5\"><b>$status</b>\n</span> <span color=\"#BE7F00\"><b>$PREF</b></span>\n<span color=\"#6EBF40\"><b>_______________________________________________________________</b></span>\nChange CPU Power Settings\n" \
    --window-icon="$ICON" --title="CPUthrottle" --text-align=center --buttons-layout=center \
    --button=System!"/usr/share/cputhrottle/icons/system.svg:6" \
    --button=Freq!"/usr/share/cputhrottle/icons/stats.svg:7" \
    --button=Tray!"/usr/share/cputhrottle/icons/tray.svg:5" \
    --button=Close!"/usr/share/cputhrottle/icons/cancel.svg:1" --form \
    --field="Performance"'!/usr/share/cputhrottle/icons/performance.svg!':FBTN \
    'bash -c "echo Performance ; kill -USR2 $YAD_PID"' \
    --field="Ondemand"'!/usr/share/cputhrottle/icons/ondemand.svg!':FBTN \
    'bash -c "echo Ondemand ; kill -USR2 $YAD_PID"' \
    --field="Userspace"'!/usr/share/cputhrottle/icons/userspace.svg!':FBTN \
    'bash -c "echo Userspace ; kill -USR2 $YAD_PID"' \
    --field="Powersave"'!/usr/share/cputhrottle/icons/powersave.svg!':FBTN \
    'bash -c "echo Powersave ; kill -USR2 $YAD_PID"')
  if [ $? -eq 6 ]; then 
    # System info window
    KEY=$RANDOM
    function show_mod_info {
      TXT="\\n<span face='Monospace'>$(modinfo "$1" | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g')</span>"
      yad --title="Module information" --button="$CANCEL" --width=500 \
        --image="application-x-addon" --text="$TXT"
    }
    export -f show_mod_info

    lscpu | sed -r "s/:[ ]*/\n/" | \
      yad --plug=$KEY --tabnum=1 --image=$ICON --text="<span color=\"#BE7F00\">CPU information</span>" \
      --list --no-selection --column="Parameter" --column="Value" &

    TXT="\t$MODEL\n"
    TXT+="\t<span font_desc='Sans Bold Italic 10' foreground='#BE7F00'>cpu cores $CORES</span>\n"
    TXT+="\t$(lsb_release -ds)\n"
    TXT+="\tKernel: $(uname -sr)\n"
    TXT+="\t$DESKTOP\n\n"

    yad --window-icon=$ICON --notebook --on-top --center \
      --width=600 --height=200 --title="CPUthrottle" --text="$TXT" \
      --button=Cancel!"$CANCEL:1" --key=$KEY --tab="<span color=\"#BE7F00\">CPU</span>" \
      --active-tab=${1:-1}

   /usr/bin/cputhrottle
  fi |
  if [ $? -eq 7 ]; then
 /usr/share/cputhrottle/menu/read_freq
  fi |
  if [ $? -eq 5 ]; then
    for idx in $USERS; do
      pkill -f CPUthrottle >/dev/null 2>&1 &   
      sleep 1
      cp -r /usr/share/cputhrottle/menu/CPUthrottle.desktop /etc/xdg/autostart
     `sh -c /usr/share/cputhrottle/menu/CPUthrottle`
    done
  fi

  if [ -n "${select}" ]; then
    case "$select" in
      'Performance')
        echo performance > /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor 
        echo "Performance enabled" > /usr/share/cputhrottle/menu/cpu_info
        su "$USERS" -c /usr/share/cputhrottle/notify/performance.py &
        pkexec cputhrottle
        ;;
      'Ondemand')
        echo ondemand > /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor 
        echo "Ondemand enabled" > /usr/share/cputhrottle/menu/cpu_info
        su "$USERS" -c /usr/share/cputhrottle/notify/powersave.py &
        pkexec cputhrottle
        ;;
      'Userspace')
        echo userspace > /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor 
        echo "Userspace enabled" > /usr/share/cputhrottle/menu/cpu_info
        su "$USERS" -c /usr/share/cputhrottle/notify/powersave.py &
        pkexec cputhrottle
        ;;
      'Powersave')
        SELGOV=$(grep -o powersave /tmp/governors)
        if [ "$SELGOV" ]; then
          echo "active" > /sys/devices/system/cpu/amd_pstate/status
          echo "powersave" > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor
          cpupower frequency-set -u 400MHz  
          echo "Powersave enabled" > /usr/share/cputhrottle/menu/cpu_info
           su "$USERS" -c  /usr/share/cputhrottle/notify/powersave.py &
          pkexec cputhrottle
        else
          yad --title='CPUthrottle' --width=425 --height=100 --fixed --center --on-top \
            --text="\nPowersave option is not available for this cpu!\n\nAvailable: ondemand userspace performance" --text-align=left \
            --window-icon="$ICON" --image="$ICON" --button=Close!"$CANCEL"
          pkexec cputhrottle
        fi
        ;;
    esac
  fi
}

menu
pkill -f /usr/share/cputhrottle/menu/read_freq 
exit 0

