#!/bin/sh
#	
# License GPL
# Author: D. Meiß-Wilhelm
# Create for PCLinuxOS LXQT based on GTKDIALOG
# Build time: 2024.04				   
# Required: gtkdialog, polkit, coreutils, cpupower

TITLE="CPUPowerset"
VERSION=" 1.2"

if [ `cat /usr/share/polkit-1/actions/org.pclinuxos.cpupowerset.policy | grep -c "<allow_active>yes</allow_active>"` == 1 ]; then 
MYRTC=false
KLL=/usr/libexec/cpupowersetkll
else
MYRTC=true
KLL=/usr/libexec/cpupowersetkll
fi
if [ `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor` == "performance" ]; then
PEF=true
else
PEF=false
fi
if [ `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor` == "ondemand" ]; then
OND=true
else
OND=false
fi
if [ `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor` == "powersave" ]; then
POW=true
else
POW=false
fi
[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog

CPUPOWERSET='
<window window_position="1" title="'$TITLE$VERSION'"
 width-request="300" height-request="200" icon-name="cpu">
	<vbox>
		<frame CPU Power Set>
			<radiobutton active="'$PEF'">
				<label>Peformence</label>
				<variable>SET1</variable>
				<action>echo Peformence is $SET1 now.</action>
				<action>if true cpupower -c all frequency-set -g performance</action>
				 <action>if true refresh:STRT</action>
			</radiobutton>
			<radiobutton active="'$OND'">
				<label>Ondemand</label>
				<variable>SET2</variable>
				<action>echo Ondemand is $SET2 now.</action>
				<action>if true cpupower -c all frequency-set -g ondemand</action>
				 <action>if true refresh:STRT</action>
			</radiobutton>
			<radiobutton active="'$POW'">
				<label>Powersave</label>
				<variable>SET3</variable>
				<action>echo Powersave is $SET3 now.</action>
				<action>if true cpupower -c all frequency-set -g powersave</action>
				 <action>if true refresh:STRT</action>
			</radiobutton>
		</frame>
		<text><label>'$"CPU current state:"'</label></text>
			<entry>
				<input>echo `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`</input>
				<variable>STRT</variable>
			</entry>
			<vbox>
				<button tooltip-text="'$"It will start myrtcwake without input the root password.
Works at next start of myrtcwake."'">
					<label>"'$"Remember root privileges"'"</label>
					<action>'$KLL'</action>
				</button>
				<visible>'$MYRTC'</visible>
			</vbox>				
			<hbox>
				<button>
					<input file stock="gtk-ok"></input>
					<label>'$"Show"'</label>
					<action>xterm  -geometry 90x3 -e watch -n.1 "grep \"^[c]pu MHz\" /proc/cpuinfo | head -n 1" &</action>
				</button>
				<button>
					<input file stock="gtk-close"></input>
					<label>'$"Exit"'</label>
					<variable>CLOSE</variable>
					<action type="closewindow">CLOSE</action>
				</button>
			</hbox>
	</vbox>
</window>
'
export CPUPOWERSET

case $1 in
	-d | --dump) echo "CPUPOWERSET" ;;
	*) $GTKDIALOG --program=CPUPOWERSET ;;
esac
