#!/bin/bash
# ScreenSaver Control 3.2

export TEXTDOMAIN=sscontrol
export OUTPUT_CHARSET=UTF-8
. gettext.sh

APPDIR=/usr/local/dcontrol
ICONDIR=$APPDIR/icons
PREFDIR=$APPDIR/preferences
export APPDIR ICONDIR PREFDIR

[ "`which gtkdialog4 2>/dev/null`" ] && GTKDIALOG=gtkdialog4 || GTKDIALOG=gtkdialog
export GTKDIALOG

# set icon-name
[ ! -f /usr/share/icons/hicolor/48x48/apps/dcontrol.svg ] && \
ln -sf $ICONDIR/monitor.svg /usr/share/icons/hicolor/48x48/apps/dcontrol.svg && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

# set header
XML_INFO_COLOR='#EDEBD7' # background color
XML_INFO_OPACITY=0.5 # background opacity
. $APPDIR/xml_info_dcontrol gtk > /dev/null # build bg_pixmap for gtk-theme

BOX_HEIGHT=90 # HEADER
ICON=$ICONDIR/screensaver.svg
ICON_HEIGHT=60
MSG_1="<b><span size='"'x-large'"'>$(gettext "Screen Saver")</span></b>"
MSG_2="<b>$(gettext "Set screen blanking delay")</b>"
ALIGN=left # center or left
HEADER="
<hbox height-request="'"${BOX_HEIGHT}"'">
$(. $APPDIR/xml_info_dcontrol "$ICON" "$ICON_HEIGHT" "$MSG_1" "$MSG_2" "$ALIGN")
</hbox>"

# apply screensaver activation delay
func_apply_delay(){
	if [ "$SCREENSAVERDELAY" = "0" ]; then
		echo false > $PREFDIR/ss-status
	else
		echo true > $PREFDIR/ss-status
	fi
	$APPDIR/func
}
export -f func_apply_delay

# round seconds to nearest minute increment (pupx compatibility)
round60(){
	echo $(( ((${1%.*}+30)/60)*60 ))
}
export -f round60

# define current screensaver activation delay
SCREENSAVERDELAY=$(cat $PREFDIR/timeout 2>/dev/null)
if [ ! "$SCREENSAVERDELAY" ]; then
	if [ -n "$WAYLAND_DISPLAY" ]; then
		. /etc/environment
		SCREENSAVERDELAY="$XWAYLAND_SCREENSAVER_DELAY"
	else
		SCREENSAVERDELAY=$(xset q | grep ' timeout: ' | grep ' cycle: ' | tr -s ' ' | cut -d " " -f3)
	fi
	[ ! "$SCREENSAVERDELAY" ] && SCREENSAVERDELAY=0
	SCREENSAVERDELAY=$(round60 $SCREENSAVERDELAY)
	SCREENSAVERDELAY=$(expr $SCREENSAVERDELAY / 60)
fi

# ScreenSaver Delay dlg...
export SS_CONTROL="
<window title=\"$(gettext 'SS-Control')\" icon-name=\"dcontrol\" resizable=\"false\" window-position=\"3\">
 <vbox>
   $HEADER

   <text height-request=\"10\"><label>\"\"</label></text>
   <text use-markup=\"true\"><label>\"$(gettext '     SS-Control blanks the screen, and     
     enables DPMS (Power Save Mode)    
     for Energy-Star displays and video     
     cards. The activation delays below     
     are for screen blanking and DPMS.    ')\"</label></text>

   <text height-request=\"10\"><label>\"\"</label></text>

   <hbox homogeneous=\"true\">
   <vbox>
      <text><label>$(gettext 'Delay (minutes)')</label></text>
      <spinbutton width-request=\"175\" range-min=\"0\" range-max=\"300\" range-step=\"1\" range-value=\"5\" tooltip-text=\"$(gettext ' Select an activation delay (1-300 minutes) 
 for screen blanking and Power Save Mode. 
 Selecting 0 will set the screensaver to Off. ')\">
         <variable>SCREENSAVERDELAY</variable>
         <input>echo $SCREENSAVERDELAY</input>
         <action>echo \$SCREENSAVERDELAY > $PREFDIR/timeout</action>
      </spinbutton>
   </vbox>
   </hbox>

   <text height-request=\"10\"><label>\"\"</label></text>

   <hseparator></hseparator>

   <hbox space-expand=\"true\" space-fill=\"true\" homogeneous=\"true\">
      <button has-focus=\"true\" height-request=\"35\" width-request=\"110\" tooltip-text=\"$(gettext ' Set screensaver activation delay ')\" use-underline=\"true\">
         <label>$(gettext '_Apply')</label>
         <input file stock=\"gtk-apply\"></input>
         <action>func_apply_delay</action>
         <action>$APPDIR/dcontrol-screensaver &</action>
         <action type=\"exit\">quit_now</action>
      </button>
   </hbox>
 </vbox>
</window>"

$GTKDIALOG -p SS_CONTROL --styles=/tmp/dcontrol/gtkrc_xml_info.css

unset SS_CONTROL

exit 0
