#!/bin/bash
#	
# License GPL
# Author: D. Meiß-Wilhelm
# Create for PCLinuxOS LXQT based on GTKDIALOG
# Build time: 2024.04
# rtcwake -m off -t $(date +%s -d 'tomorrow xx:xx')					   
# Required: gtkdialog, polkit, util-linux, xterm					   
#
Encoding=UTF-8
export TEXTDOMAIN=myrtcwake
export TEXTDOMAINDIR="/usr/share/locale"

TITLE="MyRTCWAKE"
VERSION=" 1.2"
CONF="$HOME/.config/myrtcwake.conf"

if [ ! -f ~/.config/myrtcwake.conf ]; then 
	touch ~/.config/myrtcwake.conf
fi
if [ `cat $CONF | grep -c time ` == "1" ];then
TIME=`cat $CONF | grep time | cut -d ":" -f2,3`
else
TIME="06:00"
fi
if [ `cat $CONF | grep -c mode ` == "1" ];then
MODE=`cat $CONF | grep mode | cut -d ":" -f2`
else
MODE=no
fi
if [ `cat $CONF | grep -c countdown ` == "1" ];then
COUNT=`cat $CONF | grep countdown | cut -d ":" -f2`
else
COUNT="12"
fi

if [ `cat /usr/share/polkit-1/actions/org.pclinuxos.myrtcwake.policy | grep -c "<allow_active>yes</allow_active>"` == 1 ]; then 
MYRTC=false
KLL=/usr/libexec/myrtcwakekll
else
MYRTC=true
KLL=/usr/libexec/myrtcwakekll
fi

{
export ABOUT='
<window window_position="1" title="'$TITLE$VERSION'"  
 width-request="600" height-request="400" icon-name="myrtcwake">
<vbox>
	<vbox scrollable="true">
		<frame '$"About"'>
			<text use-markup="True">
				<label>"'"<b>MyRTCWAKE</b>
				
 MyRTCWAKE is a simple to used GUI for rtcwake.

 Designed for PCLinuxOS-LXQT-2024.04
 Author:	Daniel Meiß-Wilhelm
 Website: 	www.pclinuxos.com/forum
 Release: 	2024
 License: 	GPL2 
 Version:	$VERSION

This program is used to enter a system sleep state and to
automatically wake from it at a specified time.

This uses cross-platform Linux interfaces to enter a system sleep
state, and leave it no later than a specified time. It uses any
RTC framework driver that supports standard driver model wakeup
flags.
Mode are:

           no
               Don’t suspend, only set the RTC wakeup time.
           
           off
               ACPI state S5 (Poweroff). This is done by calling
               '/sbin/shutdown'. Not officially supported by ACPI, but
               it usually works.    

           standby
               ACPI state S1. This state offers minimal, though real,
               power savings, while providing a very low-latency
               transition back to a working system. This is the default
               mode.

           mem
               ACPI state S3 (Suspend-to-RAM). This state offers
               significant power savings as everything in the system is
               put into a low-power state, except for memory, which is
               placed in self-refresh mode to retain its contents.

           disk
               ACPI state S4 (Suspend-to-disk). This state offers the
               greatest power savings, and can be used even in the
               absence of low-level platform support for power
               management. This state operates similarly to
               Suspend-to-RAM, but includes a final step of writing
               memory contents to disk.
               
          disable
               Disable a previously set alarm.    
               
               
               AUTHORS
       The program was written by David Brownell
       dbrownell at users dot sourceforge.net and improved by 
       Bernhard Walle bwalle at suse dot de.
       
 GUI created with GTKDIALOG.
`gtkdialog --version`      
 
"'"</label>
			</text>
		</frame>	
	</vbox>	
		<hbox>
			<button>
				<label>'$"Close"'</label>
				<input file stock="gtk-ok"></input>
				<variable>ABOUT</variable>
				<action type="closewindow">ABOUT</action>
			</button>
		</hbox>
</vbox>
</window>
'
}

{
export MYRTCWAKE='
<window window_position="1" title="'$TITLE$VERSION'" maximize_initially="true" allow-shrink="true"  icon-name="myrtcwake">
	<vbox>
		<frame '$"Shutdown and start at xx:xx time with rtcwake"'>
				<vbox>
					<text use-markup="True" width-chars="30"><label>"<b>'$"Wake up time:"'</b>"</label></text>
						<entry>
							<default>'$TIME'</default>
							<variable>NTR</variable>
						</entry>
						<vbox>
							<text use-markup="True" width-chars="30"><label>"<b>'$"Mode:"'</b>"</label></text>
							<comboboxentry width-request="60" case-sensitive="false" value-in-list="false" active="0">
								<variable>FNCTN</variable>
								<item>'$MODE'</item>
								<item>no</item>
								<item>off</item>
								<item>mem</item>	
								<item>disk</item>
								<item>standby</item>
								<item>disable</item>
							</comboboxentry>
						</vbox>
						<vbox>
							<text use-markup="True" width-chars="30"><label>"<b>'$"Today or tomorrow:"'</b>"</label></text>
							<comboboxentry width-request="60" case-sensitive="false" value-in-list="false" active="0">
								<variable>DYTG</variable>
								<item>tomorrow</item>
								<item>today</item>
							</comboboxentry>
						</vbox>
						<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 tooltip-text="'$"Enter a countdown in minutes to shutdown"'">
						<checkbox>
							<label>'$"Countdown"'</label>
							<variable>SND</variable>
							<action>if true enable:STRT</action>
							<action>if true enable:SLP</action> 
							<action>if true disable:SLPT</action>
							<action>if false disable:STRT</action>
							<action>if false disable:SLP</action>
							<action>if false enable:SLPT</action>
							<action type="refresh">STRT</action>      
						</checkbox>
						<text><label>'$"Minutes:"'</label></text>
							<entry>
								<default>'$COUNT'</default>
								<variable>STRT</variable>
								<visible>disabled</visible>
							</entry>
						</hbox>	
						<hbox>
							<button>
								<label>'$"About"'</label>
								<input file stock="gtk-help"></input>
								<action type="launch">ABOUT</action>
							</button>
							<button tooltip-text="'$"Make sure you have completed all work and saved all open files! 
Shut down when the countdown is over."'">
								<input file stock="gtk-ok"></input>
								<label>'$"Okay +"'</label>
								<variable>SLP</variable>
								<visible>disabled</visible>
								<action>echo time:$NTR > '$CONF'</action>
								<action>echo mode:$FNCTN >> '$CONF'</action>
								<action>echo countdown:$STRT >> '$CONF'</action>
								<action>sleep "$STRT"m && rtcwake -m $FNCTN -t $(date +%s -d "$DYTG $NTR") &</action>
								<action type="exit">MYRTCWAKE</action>
							</button>
							<button tooltip-text="'$"Make sure you have all work done, and all open files saved!"'">
								<input file stock="gtk-ok"></input>
								<label>'$"Okay"'</label>
								<variable>SLPT</variable>
								<action>echo time:$NTR > '$CONF'</action>
								<action>echo mode:$FNCTN >> '$CONF'</action>
								<action>echo countdown:$STRT >> '$CONF'</action>
								<action>rtcwake -m $FNCTN -t $(date +%s -d "$DYTG $NTR")</action>
							</button>
							<button tooltip-text="'$"May You want to test the mode, it starts the system in 30 seconds.
Make sure you have all work done, and all open files saved!"'">
								<input file stock="gtk-ok"></input>
								<label>'$"Test"'</label>
								<action>rtcwake -m $FNCTN -s 30</action>
							</button>
							<button tooltip-text="'$"Print alarm information in format: 'alarm: off|on
               (time)'. The time is in ctime() output format, e.g.,
               'alarm: on Tue Nov 16 04:48:45 2010'."'">
								<input file stock="gtk-find"></input>
								<label>'$"Show"'</label>
								<action>xterm -geometry 60x3  -title "Alarm Information" -hold -e rtcwake -m show</action>
							</button>
							<button>
								<label>'$"Close"'</label>
								<input file stock="gtk-quit"></input>
								<action type="exit">MYRTCWAKE</action>
							</button>
						</hbox>	
				</vbox> 	
		</frame>				
	</vbox>	
</window>
'
}
gtkdialog --program=MYRTCWAKE #> /dev/null 2>&1

