#! /bin/bash
#################################################################
#this GUI is designed for a script what was posted from longtom #
#on: March 01, 2011	as rotator, in PCLinuxOS-Forums.			#
#Designed for PCLinuxOS-LXDE.									#
#Date: March 23, 2015											#
#Licence: GPLv2													#
#Author: D.Meiß-Wilhelm (Leiche) 								#
#Don't remove this header										#
#################################################################
Encoding=UTF-8
#
export TEXTDOMAIN=rotator_gui
export TEXTDOMAINDIR="/usr/share/locale"
# create config directory for user
if [ ! -d $HOME/.config/lxrotator ]; then
	mkdir $HOME/.config/lxrotator
	echo 0 > $HOME/.config/lxrotator/pid
	echo "config directory ready"
fi

#  variables for simple handling
TITLE=LXRotatorGUI
VERSION=" 1.6"
CONF=$HOME/.config/lxrotator
dirname="$(dirname "$(readlink -e "$0")")"
if [ ! -f $CONF/lxrotator ]; then
cp $dirname/lxrotator $CONF
fi
RESTART=$dirname/rotator_gui
AUTOSTARTER=$dirname/starter
CHANGER=$CONF/lxrotator
STARTER=/usr/share/applications/lxrotator.desktop
ICON=/usr/share/icons/lxrotator.png
ICONSIZE=24x24
BUTTONICON=$dirname/icons
SYSTRAY=$dirname/rotator_tray
if [ -f $CONF/timer ];then
	MINTIME=`cat $CONF/timer | grep minutes | cut -d "=" -f2`
	HOURTIME=`cat $CONF/timer | grep hours | cut -d "=" -f2`
	else
	MINTIME=30
	HOURTIME=0
fi	
SETTIME=`cat $CHANGER | grep SET -m1 | cut -d "=" -f2`
echo $"interval time is $SETTIME minutes"
DIRNAME=`cat $CHANGER | grep -w "DIR" -m1 | cut -d "=" -f2`
echo $"Wallpapers are saved in $DIRNAME"
if [ "$XDG_CURRENT_DESKTOP" == "LXQt" ]; then
	if [ `cat ~/.config/pcmanfm-qt/lxqt/settings.conf | grep -c -w "Wallpaper=" | cut -d "=" -f2` -gt 0 ]; then
		PAPER=`cat ~/.config/pcmanfm-qt/lxqt/settings.conf | grep Wallpaper= | cut -d "=" -f2`
		else
		PAPER=`cat ~/.config/pcmanfm-qt/lxqt/settings.conf | grep Wallpaper0= | cut -d "=" -f2`
	fi
	else
	if [ `cat ~/.config/pcmanfm/LXDE/desktop-items-0.conf | grep -c -w "wallpaper=" | cut -d "=" -f2` -gt 0 ]; then
		PAPER=`cat ~/.config/pcmanfm/LXDE/desktop-items-0.conf | grep wallpaper= | cut -d "=" -f2`
		else
		PAPER=`cat ~/.config/pcmanfm/LXDE/desktop-items-0.conf | grep wallpaper0= | cut -d "=" -f2`
	fi
fi			
echo $"Current wallpaper is `basename "$PAPER"`"
convert "$PAPER" -resize 256x256 $CONF/thump.jpg
# set pid to kill wallpaper-changer, you can use
# kill `cat ~/.config/lxde-rotator/pid`. A gui is in progress
if [ $(grep -c systray=1 $CHANGER) -gt 0  > /dev/null 2>&1 ]; then
	SYSTRAY_STATUS=true
	echo systrue
	else
	SYSTRAY_STATUS=false
	echo sysfalse
fi
if [ -f "$HOME/.config/autostart/lxro_start.desktop" ]; then
	AUTOSTART_STATUS=true
	echo autotrue
	#exec $HOME/.config/lxrotator/lxrotator &
	sleep 1
	else
	AUTOSTART_STATUS=false
	echo autofalse
fi	
PIDKILL=` pgrep lxrotator`
echo $PIDKILL
DELAY_STATUS=`cat $CHANGER | grep delay -m1 | cut -d "=" -f2`
echo $DELAY_STATUS

# about/help
export ABOUT='
<window window_position="1" icon-name="info" width-request="600"  title="'$"About"'">
	<vbox wrap-mode="1">
		<frame>
			<text use-markup="true" xalign="0" wrap-mode="0">
				<input file>/usr/share/doc/lxrotator/README</input>
			</text>
		</frame>	
		<vbox><hbox>  
			<button>
				<label>OK</label>
				<variable>ABOUT</variable>
				<action type="closewindow">ABOUT</action>
			</button>
		</hbox></vbox>
	</vbox>		
</window>
'	
# program
	export LXROTATOR='
<window window_position="1" title="'$TITLE$VERSION'" width-request="750" icon-name="lxrotator">
	<vbox homogeneous="false">
		<frame '$"Current Settings"'>
			<hbox border-width="1" homogeneous="false" --space-expand="true" xalign="0" --space-fill="true">
				<vbox wrap-mode="0" width-request="400">
					##<vbox>
					##	<text use-markup="True" xalign="0">
					##		<label>"'$"<b>Desktop:</b> "'"</label>
					##	</text>
					##	<text xalign="0">
					##		##<variable>DISP1</variable>
					##		<input> echo $DESKTOP_SESSION</input>
					##	</text>
					##</vbox>	
					<vbox>
						<text use-markup="True" xalign="0">
							<label>"'$"<b>LXRotator is:</b> "'"</label>
						</text>
						<text xalign="0">
							<variable>DISP1</variable>
							<input> if [ "`pgrep lxrotator`" -gt 1 > /dev/null 2>&1 ];then echo $"active" ;else echo $"disabled" ;fi </input>
						</text>
					</vbox>	
					<vbox>
						<text use-markup="True" xalign="0">
							<label>"'$"<b>Start delay is:</b> "'"</label>
						</text>
						<text xalign="0">
							<input> cat '$CHANGER' | grep delay -m1 | cut -d "=" -f2</input>
						</text>
					</vbox>				
					<vbox>
						<text use-markup="True" xalign="0">
							<label>"'$"<b>Wallpaper directory:</b> "'"</label>
						</text>
						<text xalign="0">
							<input>cat '$CHANGER' | grep -w "DIR" -m1 | cut -d "=" -f2</input>
						</text>	
					</vbox>
					<vbox>
						<text use-markup="True" xalign="0">
							<label>"'$"<b>Interval:</b> "'"</label>
						</text>
						<text xalign="0">
							<input>cat '$CHANGER' | grep SET -m1 | cut -d "=" -f2</input>
						</text>
					</vbox>	
					<vbox>	
						<text use-markup="True" xalign="0">
							<label>"'$"<b>Wallpaper:</b> "'"</label>
						</text>	
						<text xalign="0" >
							<input>basename '$PAPER'</input>
						</text>
					</vbox>		
				</vbox>	
				<vseparator></vseparator>
				<pixmap>
					<input file>"'$CONF'/thump.jpg"</input>
				</pixmap>
			</hbox>
		</frame>
		<vbox>
			<frame '$"Choose Wallpaper Directory"'>
				<hbox tooltip-text="'$"Set a default directory to load"'">
					<text><label>'$"Directory:"'</label></text>
						<entry accept="directory">
							<default>'$DIRNAME'</default>
							<variable>LOAD</variable>
						</entry>
					<button>
						<label>'$"Open"'</label>
						<input file>'$BUTTONICON'/open24.png</input>
						<variable>FILE_BROWSE_FILENAME</variable>
						<action type="fileselect">LOAD</action>
					</button>      
				</hbox>
			</frame>
		</vbox>	
		<hbox>
			<frame '$"Set Interval Time In Minutes"'>
				<hscale width-request="10" range-min="0" range-max="60" range-step="1" value-pos="0" inverted="false">
					<default>'$MINTIME'</default>
					<variable>MINSET</variable>
					<item>"1 | 2 | <span> 1 </span>"</item>
					<item>"2 | 2 | <span>  </span>"</item>
					<item>"3 | 2 | <span>  </span>"</item>
					<item>"4 | 2 | <span>  </span>"</item>
					<item>"5 | 2 | <span> 5 </span>"</item>
					<item>"6 | 2 | <span>  </span>"</item>
					<item>"7 | 2 | <span>  </span>"</item>
					<item>"8 | 2 | <span>  </span>"</item>
					<item>"9 | 2 | <span>  </span>"</item>
					<item>"10 | 2 | <span> 10 </span>"</item>
					<item>"11 | 2 | <span>  </span>"</item>
					<item>"12 | 2 | <span>  </span>"</item>
					<item>"13 | 2 | <span>  </span>"</item>
					<item>"14 | 2 | <span>  </span>"</item>
					<item>"15 | 2 | <span> 15 </span>"</item>
					<item>"16 | 2 | <span>  </span>"</item>
					<item>"17 | 2 | <span>  </span>"</item>
					<item>"18 | 2 | <span>  </span>"</item>
					<item>"19 | 2 | <span>  </span>"</item>
					<item>"20 | 2 | <span> 20 </span>"</item>
					<item>"21 | 2 | <span>  </span>"</item>
					<item>"22 | 2 | <span>  </span>"</item>
					<item>"23 | 2 | <span>  </span>"</item>
					<item>"24 | 2 | <span>  </span>"</item>
					<item>"25 | 2 | <span> 25 </span>"</item>
					<item>"26 | 2 | <span>  </span>"</item>
					<item>"27 | 2 | <span>  </span>"</item>
					<item>"28 | 2 | <span>  </span>"</item>
					<item>"29 | 2 | <span>  </span>"</item>
					<item>"30 | 2 | <span> 30 </span>"</item>
					<item>"31 | 2 | <span>  </span>"</item>
					<item>"32 | 2 | <span>  </span>"</item>
					<item>"33 | 2 | <span>  </span>"</item>
					<item>"34 | 2 | <span>  </span>"</item>
					<item>"35 | 2 | <span> 35 </span>"</item>
					<item>"36 | 2 | <span>  </span>"</item>
					<item>"37 | 2 | <span>  </span>"</item>
					<item>"38 | 2 | <span>  </span>"</item>
					<item>"39 | 2 | <span>  </span>"</item>
					<item>"40 | 2 | <span> 40 </span>"</item>
					<item>"41 | 2 | <span>  </span>"</item>
					<item>"42 | 2 | <span>  </span>"</item>
					<item>"43 | 2 | <span>  </span>"</item>
					<item>"44 | 2 | <span>  </span>"</item>
					<item>"45 | 2 | <span> 45 </span>"</item>
					<item>"46 | 2 | <span>  </span>"</item>
					<item>"47 | 2 | <span>  </span>"</item>
					<item>"48 | 2 | <span>  </span>"</item>
					<item>"49 | 2 | <span>  </span>"</item>
					<item>"50 | 2 | <span> 50 </span>"</item>
					<item>"51 | 2 | <span>  </span>"</item>
					<item>"52 | 2 | <span>  </span>"</item>
					<item>"53 | 2 | <span>  </span>"</item>
					<item>"54 | 2 | <span>  </span>"</item>
					<item>"55 | 2 | <span> 55 </span>"</item>
					<item>"56 | 2 | <span>  </span>"</item>
					<item>"57 | 2 | <span>  </span>"</item>
					<item>"58 | 2 | <span>  </span>"</item>
					<item>"59 | 2 | <span>  </span>"</item>
					<item>"60 | 2 | <span> 60 </span>"</item>
				</hscale>
			</frame>
		</hbox>	
		<hbox>
			<frame '$"Set Interval Time In Hours"'>
				<hscale width-request="10" range-min="0" range-max="24" range-step="1" value-pos="0" inverted="false">
					<default>'$HOURTIME'</default>
					<variable>HOURSET</variable>
					<item>"1 | 2 | <span> 1 </span>"</item>
					<item>"2 | 2 | <span>  </span>"</item>
					<item>"3 | 2 | <span>  </span>"</item>
					<item>"4 | 2 | <span>  </span>"</item>
					<item>"5 | 2 | <span>  </span>"</item>
					<item>"6 | 2 | <span> 6 </span>"</item>
					<item>"7 | 2 | <span>  </span>"</item>
					<item>"8 | 2 | <span>  </span>"</item>
					<item>"9 | 2 | <span>  </span>"</item>
					<item>"10 | 2 | <span>  </span>"</item>
					<item>"11 | 2 | <span>  </span>"</item>
					<item>"12 | 2 | <span> 12 </span>"</item>
					<item>"13 | 2 | <span>  </span>"</item>
					<item>"14 | 2 | <span>  </span>"</item>
					<item>"15 | 2 | <span>  </span>"</item>
					<item>"16 | 2 | <span>  </span>"</item>
					<item>"17 | 2 | <span>  </span>"</item>
					<item>"18 | 2 | <span>  18 </span>"</item>
					<item>"19 | 2 | <span>  </span>"</item>
					<item>"20 | 2 | <span>  </span>"</item>
					<item>"21 | 2 | <span>  </span>"</item>
					<item>"22 | 2 | <span>  </span>"</item>
					<item>"23 | 2 | <span>  </span>"</item>
					<item>"24 | 2 | <span> 24 </span>"</item>
				</hscale>
			</frame>
		</hbox>	
		<vbox>
			<hbox>	
				<text>
					<label> "'$"Start Delay: "'"</label>
				</text>
				<comboboxentry width-request="60" case-sensitive="false" value-in-list="false">
					<variable>DELAY</variable>
					<item>'$DELAY_STATUS'</item>
					<item>5</item>
					<item>10</item>
					<item>15</item>
					<item>20</item>
					<item>25</item>
					<item>30</item>
					<item>35</item>
					<item>40</item>
					<item>45</item>
					<item>50</item>
					<item>55</item>
					<item>60</item>
				</comboboxentry>
				<checkbox active="'$SYSTRAY_STATUS'">
					<label>'$"Activate Systray"'</label>
					<action>if true sed -i "s:systray=0:systray=1:g" '$CHANGER'</action>
					<action>if true exec '$SYSTRAY'</action>
					<action>if false  sed -i "s:systray=1:systray=0:g" '$CHANGER'</action>
					<action>if false kill `cat '$CONF'/pid`</action>
				</checkbox>
				<checkbox active="'$AUTOSTART_STATUS'">
					<label>'$"Autostart"'</label>
					##<action>if true cp '$STARTER' '$HOME'/.config/autostart</action>
					<action>if true  '$AUTOSTARTER'</action>
					##<action>if false  rm -r '$HOME'/.config/autostart/lxrotator.desktop</action>
					<action>if false  rm -r '$HOME'/.config/autostart/lxro_start.desktop</action>
				</checkbox>
			</hbox>	
		</vbox>	
		<hseparator></hseparator>
##----------------- buttons area ----------------------------------------
		<hbox homogeneous="true">
			<button>
				<label>'$"About"'</label>
				##<input file icon="help"></input>
				<input file>'$BUTTONICON'/help24.png</input>
				<action type="launch">ABOUT</action>
			</button>
			<button>
				<label>'$"Save"'</label>
				##<input file icon="gtk-save"></input>
				<input file>'$BUTTONICON'/save24.png</input>
				<action>kill ` pgrep lxrotator`</action>
				<action>cp '$CHANGER' '$CHANGER'.bak</action>
				<action>echo > '$CONF'/timer</action>
				<action>echo minutes=$MINSET > '$CONF'/timer</action>
				<action>echo hours=$HOURSET >> '$CONF'/timer</action>
				<action>NEWSET=$(echo $(($HOURSET*60+$MINSET))) && sed -e "{s/delay="'$DELAY_STATUS'"/delay="$DELAY"/g;s/SET="'$SETTIME'"/SET="$NEWSET"/g;s:DIR="'$DIRNAME'":DIR="$LOAD":}" '$CHANGER' > '$CHANGER'1</action>
				<action>mv '$CHANGER'1 '$CHANGER'</action>
				<action>chmod 744 '$CHANGER'</action>
				<action>exec '$CHANGER''$SESSION' &</action>
				<action>exec '$RESTART' &</action>
				##<action type="refresh">DISPLAY</action>
				<action type="exit">rotator</action>
			</button>
			
			<button>
				<label>'$"Start"'</label>
				##<input file icon="music-player"></input>
				<input file>'$BUTTONICON'/play24.png</input>
				<action>exec '$CHANGER''$SESSION' &</action>
				##<action>cp '$STARTER' '$HOME'/.config/autostart</action>
				<action>sleep 1 </action>
				<action type="refresh">DISP1</action>
			</button>
			
			<button>
				<label>'$"Stop"'</label>
				##<input file icon="process-stop"></input>
				<input file>'$BUTTONICON'/stop24.png</input>
				<action>kill ` pgrep lxrotator`</action>
				##<action>rm -r '$HOME'/.config/autostart/lxrotator.desktop</action>
				<action type="refresh">DISP1</action>
			</button>
			
			<button>
				<label>'$"Close"'</label>
				##<input file icon="application-exit"></input>
				<input file>'$BUTTONICON'/exit24.png</input>
				<action type="exit">rotator</action>
			</button>
		</hbox>
	</vbox>
</window>
'
export GUI="`echo "$LXROTATOR" | sed -e 's/##.*//'`" 
gtkdialog -p GUI #> /dev/null 2>&1

