#!/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.                                  #
#----------------------------------------------------------------------#

# Run as root

    USERS=$(who |sed 's/ .*//'|head -n1)
	ICON="/usr/share/cputhrottle/icons/cputhrottle.svg"
	USERS=$(who |sed 's/ .*//'|head -n1)
	Model=$(cat /proc/cpuinfo | grep -E "model name" | awk '{ print $0 }'| tail -n1|sed 's/^[^:]*://'| sed 's/^ *//g')
	CPU=$(cat /proc/cpuinfo | grep -m1 'vendor_id' | awk '{ print $3 }')
	CORES=$(cat /proc/cpuinfo | grep -E "cpu cores" | awk '{ print $0 }'| tail -n1|sed 's/^[^:]*://'| sed 's/^ *//g')
    DESKTOP="Desktop: $XDG_CURRENT_DESKTOP"
	KERNEL=$(cat /proc/sys/kernel/osrelease| awk '{ print $0 }')
	setting=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
	PREF=$(cat /usr/share/cputhrottle/menu/cpu_info| awk 'NR==2{print $0}')
    Celeron=$(cat /proc/cpuinfo | grep -E "Celeron" | awk '{ print $0 }'| tail -n1)
	Turbo=$(cat /sys/devices/system/cpu/cpu0/cpufreq/boost)
	CANCEL="/usr/share/cputhrottle/icons/cancel.svg"
    cpupower frequency-info > /tmp/cpufreq
    
	function menu {
	if [ "$CPU" == "GenuineIntel" ]; then
      exec $@
		else
    yad --title='CPUthrottle' --width=425 --height=100 --fixed --center --on-top \
      --text="\nThis application works only now with newer Intel CPUs!" --text-align=left \
      --window-icon=$ICON \
      --image=$ICON \
      --button=Close!"/usr/share/cputhrottle/icons/cancel.svg"
        exit 0
    fi
    if ! test -f /usr/share/cputhrottle/menu/cpu_info; then
	echo "No Menu Set" > /usr/share/cputhrottle/menu/cpu_info 
   fi
   if [ "$PREF" = "performance" ]; then
    PREF=`echo Performance` 
      echo "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
      echo "Performance" | pkexec tee >> /usr/share/cputhrottle/menu/cpu_info
   fi
   if [ "$PREF" = "ondemand" ]; then
   PREF=`echo Ondemand` 
   Ondemand=$(grep -o ondemand /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors)
   if [ $Ondemand == ondemand ]; then
      echo "ondemand" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
   fi   
   fi
   if [ "$PREF" = "powersave" ]; then
   PREF=`echo Powersave` 
      echo "powersave" | pkexec tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
      echo "Powersave" | pkexec tee >> /usr/share/cputhrottle/menu/cpu_info
 	fi
   if [ "$PREF" = "conservative" ]; then
   PREF=`echo Conservative` 
      Conservative=$(grep -o conservative /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors)
   if [ $Conservative == conservative ]; then
      echo "conservative" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
   fi 
 	fi
   if [ "$PREF" = "schedutil" ]; then
   PREF=`echo Schedutil` 
       Schedutil=$(grep -o schedutil /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors)
   if [ $Schedutil == schedutil ]; then
      echo "schedutil" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
   fi   
  	fi
   if [ "$PREF" = "userspace" ]; then
   PREF=`echo Userspace` 
    Userspace=$(grep -o userspace /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors)
   if [ $Userspace == userspace ]; then
      echo "userspace" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
   fi 
 	fi
	Turbo=$(cat /sys/devices/system/cpu/cpu0/cpufreq/boost)
	if [ $Turbo = "0" 2>/dev/null ]; then
	status=`echo Turbo disabled` 
	else
	status=`echo Turbo enabled`
	fi
	select=$(yad --height=100 --tabnum=3 --columns=3 --width=450 --height=370 --fixed --center --text="$Model\n<span font_desc='Sans Bold Italic 10' foreground='#BE7F00'>cpu cores $CORES</span>\nKernel: $KERNEL\n$DESKTOP\n\nCPU Power Status\n <span color=\"#717ED5\"><b>$status</b>\n</span> <span color=\"#BE7F00\"><b>$PREF</b></span>\n<span color=\"#6EBF40\"><b>"_______________________________________________________________"</b></span>\n\nChange CPU Power Settings\n" \
    --window-icon=$ICON --title="CPUthrottle" --text-align=center --buttons-layout=center \
    --button=Turbo!/usr/share/cputhrottle/icons/boost.svg:3 \
    --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="Powersave"'!/usr/share/cputhrottle/icons/powersave.svg!':FBTN \
	'bash -c "echo Powersave ; kill -USR2 $YAD_PID"')
	if [ $? = 6 ]; then 
	YAD_OPTIONS="--window-icon='dialog-information' --name=IxSysinfo"
	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

	# CPU tab
	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" &
	# main dialog
	DESKTOP="Desktop: $XDG_CURRENT_DESKTOP"
	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+=$"\\t\\Kernel: $(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}
	exec "/usr/bin/cputhrottle"
     fi |
	if [ $? = 7 ]; then
    exec "/usr/share/cputhrottle/menu/read_freq"
    fi |
	if [ $? = 5 ]; then 
	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`
	fi |
  	if [ -n "${select}" ]; then
    case $select in
'Performance')
	if [ $Turbo = "1" ]; then
	status=`echo Turbo enabled > /usr/share/cputhrottle/menu/cpu_info` 
	else
	status=`echo Turbo disabled > /usr/share/cputhrottle/menu/cpu_info` 
	fi
	  cpupower frequency-set -u 3200MHz
      echo "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
      echo "Performance" | pkexec tee >> /usr/share/cputhrottle/menu/cpu_info
      exec "/usr/bin/cputhrottle" &
	  su "$USERS" -c "/usr/share/cputhrottle/notify/performance.py" 
      ;;
'Schedutil')
	if [ $Turbo = "1" ]; then
	status=`echo Turbo enabled > /usr/share/cputhrottle/menu/cpu_info` 
	else
	status=`echo Turbo disabled > /usr/share/cputhrottle/menu/cpu_info` 
	fi
	  echo "schedutil" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
      echo "Schedutil" | pkexec tee >> /usr/share/cputhrottle/menu/cpu_info
      exec "/usr/bin/cputhrottle" &
	  su "$USERS" -c "/usr/share/cputhrottle/notify/schedutil.py" 
      ;;
'Userspace')
	if [ $Turbo = "1" ]; then
	status=`echo Turbo enabled > /usr/share/cputhrottle/menu/cpu_info` 
	else
	status=`echo Turbo disabled > /usr/share/cputhrottle/menu/cpu_info` 
	fi
	  echo "userspace" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
      echo "Userspace" | pkexec tee >> /usr/share/cputhrottle/menu/cpu_info
       exec "/usr/bin/cputhrottle" &
	  su "$USERS" -c "/usr/share/cputhrottle/notify/userspace.py" 
      ;;
'Ondemand')
	if [ $Turbo = "1" ]; then
	status=`echo Turbo enabled > /usr/share/cputhrottle/menu/cpu_info` 
	else
	status=`echo Turbo disabled > /usr/share/cputhrottle/menu/cpu_info` 
	fi
	  echo "ondemand" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
      echo "Ondemand" | pkexec tee >> /usr/share/cputhrottle/menu/cpu_info
       exec "/usr/bin/cputhrottle" &
	  su "$USERS" -c "/usr/share/cputhrottle/notify/ondemand.py" 
       ;;
'Conservative')
	if [ $Turbo = "1" ]; then
	status=`echo Turbo enabled > /usr/share/cputhrottle/menu/cpu_info` 
	else
	status=`echo Turbo disabled > /usr/share/cputhrottle/menu/cpu_info` 
	fi
	  echo "conservative" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
	  echo "Conservative" | pkexec tee >> /usr/share/cputhrottle/menu/cpu_info
      exec "/usr/bin/cputhrottle" &
	  su "$USERS" -c "/usr/share/cputhrottle/notify/conservative.py" 
      ;;
'Powersave')
	if [ $Turbo = "1" ]; then
	status=`echo Turbo enabled > /usr/share/cputhrottle/menu/cpu_info` 
	else
	status=`echo Turbo disabled > /usr/share/cputhrottle/menu/cpu_info` 
	fi
	  echo "Powersave" | pkexec tee >> /usr/share/cputhrottle/menu/cpu_info
      cpupower frequency-set -u 400MHz
      exec "/usr/bin/cputhrottle" &
	  su "$USERS" -c "/usr/share/cputhrottle/notify/powersave.py" 
      ;;
    esac
  fi |
	if [ $? = 3 ]; then
    if ! test -f /usr/share/cputhrottle/menu/first_run; then
yad --title='CPUthrottle' --width=450 --height=100 --fixed --center --on-top \
      --text="\n<span color=\"#BE7F00\"><b>Intel CPU Turbo Boost Information</b></span>\n\nSome Intel CPU's Do Not Support Turbo Boost
Unsupported CPU's Will be Shown as Disabled\n"\
	  --text-align=left \
      --window-icon=$ICON \
      --image="/usr/share/cputhrottle/icons/turbo-info.svg" \
      --button=Close!"/usr/share/cputhrottle/icons/cancel.svg"
	  touch /usr/share/cputhrottle/menu/first_run
        exec "/usr/bin/cputhrottle"
		  else
    NO_TURBO=$(cat /sys/devices/system/cpu/cpu0/cpufreq/boost)
  	if [ "$NO_TURBO" = "1" ]; then
	for idx in $USERS; do
    selected=$(cat /usr/share/cputhrottle/menu/cpu_info| awk 'NR==2{print $0}')
     echo "Turbo disabled" > /usr/share/cputhrottle/menu/cpu_info
     sleep 1
     echo "$selected" >> /usr/share/cputhrottle/menu/cpu_info
	 echo "0" > /sys/devices/system/cpu/cpu0/cpufreq/boost
	su "$USERS" -c "/usr/share/cputhrottle/notify/turbo_disabled.py" &
        exec "/usr/bin/cputhrottle"
     done
	fi |
  	if [ "$NO_TURBO" = "0" ]; then
    selected=$(cat /usr/share/cputhrottle/menu/cpu_info| awk 'NR==2{print $0}')
    echo "Turbo enabled" > /usr/share/cputhrottle/menu/cpu_info
    sleep 1
    echo "$selected" >> /usr/share/cputhrottle/menu/cpu_info
    echo "1" > /sys/devices/system/cpu/cpu0/cpufreq/boost
	su "$USERS" -c "/usr/share/cputhrottle/notify/turbo_enabled.py" &
     exec "/usr/bin/cputhrottle"
		fi
          fi
			fi
}
menu
pkill -f /usr/share/cputhrottle/menu/read_freq
rm -f /tmp/cpufreq >/dev/null 2>&1
exit 0
