#!/bin/bash
#----------------------------------------------------------------------#
# CPUthrottle - Set Intel 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 as published by #
# the Free Software Foundation, either version 3 of the License, or    #
# (at your option) any later version.                                  #
#----------------------------------------------------------------------#

USERS=$(who | awk '{print $1}' | head -n1) 
ICON="/usr/share/cputhrottle/icons/cputhrottle.svg"
Model=$(cat /proc/cpuinfo | grep -E "model name" | awk '{ print $0 }'| tail -n1|sed 's/^[^:]*://'| sed 's/^ *//g')
DESKTOP="Desktop: $XDG_CURRENT_DESKTOP"
KERNEL=$(cat /proc/sys/kernel/osrelease| awk '{ print $0 }')
CORES=$(cat /proc/cpuinfo | grep -E "cpu cores" | awk '{ print $0 }'| tail -n1|sed 's/^[^:]*://'| sed 's/^ *//g')
CANCEL="/usr/share/cputhrottle/icons/cancel.svg"
SETTINGS=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
PREF=$(cat /usr/share/cputhrottle/menu/cpu_info| sed -r 's/([[:space:]]+).*/\1/')
   function freq {
   if [ "$PREF" = "performance" ]; then
    PREF=`echo Performance` 
   fi
   if [ "$PREF" = "ondemand" ]; then
   PREF=`echo Ondemand` 
   fi
   if [ "$PREF" = "powersave" ]; then
   PREF=`echo Powersave` 
 	fi
   if [ "$PREF" = "conservative" ]; then
   PREF=`echo Conservative` 
 	fi
   if [ "$PREF" = "schedutil" ]; then
   PREF=`echo Schedutil` 
 	fi
   if [ "$PREF" = "userspace" ]; then
   PREF=`echo Userspace` 
 	fi
i=0
for c in  /sys/devices/system/cpu/cpu*/cpufreq; do
  CPU+=(--field "cpu$((i++))" )
done
while read $(eval echo cpu{1..${i}}) \
  <<<$(paste /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq)
 do
   for f in $(eval echo "\$cpu{1..${i}}")
     do
        echo "${f%???} ${f#"${f%}"} MHz" 2>/dev/null
   done
 sleep 2
done \
| $(yad --form --tabnum=2 --columns=2 --on-top --text="$Model\nKernel: $KERNEL\n$DESKTOP\n\n<span color=\"#717ED5\"><b>$status</b></span>\n<span color=\"#BE7F00\"><b>$PREF</b></span>\n\n<span font_desc='Sans Bold Italic 10' >cpu cores ${CORES}</span>" ${CPU[*]} --title="CPUthrottle" --window-icon=$ICON --center --fixed --width=475 --text-align=center --cycle-read --button=gtk-cancel:"1")
if [ $? = "1" ]; then
exit 0
fi
}
freq




