#!/bin/bash
#***********************************************************************
# xcompmgr_settings simple script to manage xcompmgr
# Generate under PCLinuxOS-LXDE for LXDE-Sessions
# Author: Daniel Meiß-Wilhelm (leiche) 
# Lincence: GPL
# Web site: www.pclinuxos.com/forum
# Required: yad bash xcompmgr xcompmgr_start gtkdialog
#***********************************************************
export TEXTDOMAIN=xcompmgr_settings
export TEXTDOMAINDIR="/usr/share/locale"
#
# some variables
#
CONF_DIR=~/.xcompmgr
INFO=`cat "$CONF_DIR/main"`
TITLE=xcompmgr-start
VERSION=0.04
ICON=/usr/share/pixmaps/xcompmgr.png
IMAGE="/usr/share/doc/lxautostart/Image.png"

RUN=`ps -e | grep -w xcompmgr | cut -d ":" -f3 | cut -d " " -f2`
#
# check if xcompmgr running
#
if [ "$RUN" = "xcompmgr" ];then
	echo $"xcompmgr is still running...!"
	yad --window-icon=$ICON --button="gtk-ok:0" \
		--title="$TITLE"" $VERSION" --width=250 \
		--text=$"xcompmgr is still running, will now stop!\n
<b>Notice:</b> the new settings are loaded after a restart of xcompmgr!
xcompmgr will automatically started after termination of xcompmgr-settings"
		if [ "$?" = 0 ];then 
			killall xcompmgr
		fi
fi
if [ -f ~/.config/autostart/xcompmgr_start.desktop ];then
	echo $"on" > $CONF_DIR/status
	AUTO=`cat $CONF_DIR/status`
	else
	echo $"off" > $CONF_DIR/status
	AUTO=`cat $CONF_DIR/status`
fi	
#
# read settings
#
function read_settings(){
	if [ -f $CONF_DIR/s01 ]; then S01=$(cat $CONF_DIR/s01); fi
	if [ "$S01" == "true" ]; then 
	SX01="-CcfF -I-.015 -O-.03 -D1 -t5 -l5 -r4.2 -o.5"; fi
	
	if [ -f $CONF_DIR/s0 ]; then S0=$(cat $CONF_DIR/s0); fi
	if [ "$S0" == "true" ]; then SX0="-a"; fi

	if [ -f $CONF_DIR/s1 ]; then S1=$(cat $CONF_DIR/s1); fi
	if [ "$S1" == "true" ]; then SX1="-n"; fi
	
	if [ -f $CONF_DIR/s2 ]; then S2=$(cat $CONF_DIR/s2); fi
	if [ "$S2" == "true" ]; then SX2="-s"; fi
	
	if [ -f $CONF_DIR/s3 ]; then S3=$(cat $CONF_DIR/s3); fi
	if [ "$S3" == "true" ]; then SX3="-c"; fi
	
	if [ -f $CONF_DIR/s4 ]; then S4=$(cat $CONF_DIR/s4); fi
	if [ "$S4" == "true" ]; then SX4="-f"; fi	
	
	if [ -f $CONF_DIR/s5 ]; then S5=$(cat $CONF_DIR/s5); fi
	if [ "$S5" == "true" ]; then SX5="-C"; fi
	
	if [ -f $CONF_DIR/s6 ]; then S6=$(cat $CONF_DIR/s6); fi
	if [ "$S6" == "true" ]; then SX6="-F"; fi
	
	if [ -f $CONF_DIR/s7 ]; then S7=$(cat $CONF_DIR/s7); fi
	if [ "$S7" == "true" ]; then SX7="-S"; fi	
	
	if [ -f $CONF_DIR/s8 ]; then S8=$(cat $CONF_DIR/s8); fi
	if [ "$S8" == "true" ]; then SX8="-d "; fi
	
	if [ -f $CONF_DIR/s9 ]; then SX9=$(cat $CONF_DIR/s9); fi
	
	if [ -f $CONF_DIR/s10 ]; then S10=$(cat $CONF_DIR/s10); fi
	if [ "$S10" == "true" ]; then SX10="-r"; fi
	
	if [ -f $CONF_DIR/s11 ]; then SX11=$(cat $CONF_DIR/s11); fi
	
	if [ -f $CONF_DIR/s12 ]; then S12=$(cat $CONF_DIR/s12); fi
	if [ "$S12" == "true" ]; then SX12="-o"; fi
	
	if [ -f $CONF_DIR/s13 ]; then SX13=$(cat $CONF_DIR/s13); fi
	
	if [ -f $CONF_DIR/s14 ]; then S14=$(cat $CONF_DIR/s14); fi
	if [ "$S14" == "true" ]; then SX14="-l"; fi
	
	if [ -f $CONF_DIR/s15 ]; then SX15=$(cat $CONF_DIR/s15); fi
	
	if [ -f $CONF_DIR/s16 ]; then S16=$(cat $CONF_DIR/s16); fi
	if [ "$S16" == "true" ]; then SX16="-t"; fi
	
	if [ -f $CONF_DIR/s17 ]; then SX17=$(cat $CONF_DIR/s17); fi	
	
	if [ -f $CONF_DIR/s18 ]; then S18=$(cat $CONF_DIR/s18); fi
	if [ "$S18" == "true" ]; then SX18="-I"; fi
	
	if [ -f $CONF_DIR/s19 ]; then SX19=$(cat $CONF_DIR/s19); fi
	
	if [ -f $CONF_DIR/s20 ]; then S20=$(cat $CONF_DIR/s20); fi
	if [ "$S20" == "true" ]; then SX20="-O"; fi
	
	if [ -f $CONF_DIR/s21 ]; then SX21=$(cat $CONF_DIR/s21); fi	
	
	if [ -f $CONF_DIR/s22 ]; then S22=$(cat $CONF_DIR/s22); fi
	if [ "$S22" == "true" ]; then SX22="-D"; fi
	
	if [ -f $CONF_DIR/s23 ]; then SX23=$(cat $CONF_DIR/s23); fi
	
	if [ -f $CONF_DIR/s24 ]; then SX24=$(cat $CONF_DIR/s24); fi

	
	echo $SX01 $SX0 $SX1 $SX2 $SX3 $SX4 $SX5 $SX6 $SX7 $SX8 $SX9 \
	$SX10 $SX11 $SX12 $SX13 $SX14 $SX15 $SX16 $SX17 $SX18 $SX19 \
	$SX20 $SX21 $SX22 $SX23 $SX24 > $CONF_DIR/main 
}	
#
# main dialog
#
{
export ABOUT='
<window title="xcompmgr-settings" resizable="false" icon-name="xcompmgr">
<vbox>
    <frame>
      <text use-markup="True" width-chars="30">
        <label>"<span color='"'blue'"'><b>xcompmgr-settings</b></span>"</label>
      </text>
      <text use-markup="True" width-chars="30">
        <label>"<b>Version:</b> '"$VERSION"'"</label>
      </text>
      <text>
        <label>'$"Created for"'</label>
      </text>
      <pixmap>
      <input file>'"$IMAGE"'</input>
	  </pixmap>
      <text>
        <label>**************************************************</label>
      </text>    
      <text>
        <label>'$"xcompmgr_settings is a simple gui to manage xcompmgr commands"'</label>
      </text>
      <text use-markup="True" width-chars="30">
        <label>"<b>'$"A command looks as sample:</b>"'"</label>
      </text>
      <text>  
        <label>'"xcompmgr -CcfF -I-.015 -O-.03 -D1 -t5 -l5 -r4.2 -o.5 "'</label>
      </text>
      <text>
        <label>**************************************************</label>
      </text>    
      <text use-markup="True" width-chars="30">  
        <label>"'$"<b>Author:</b> D.M-Wilhelm [leiche]"'"</label>
      </text>
      <text>
        <label>'$"Released 2012"'</label>
      </text>
      <text>
        <label>'$"Under GPL License"'</label>
      </text>
      <text use-markup="True">
        <label>"'$"<b>Translators are:</b> ghostbunny (DE)"'"</label>
      </text>
      <text use-markup="True" width-chars="30">
        <label>"'$"<a href='http://www.pclinuxos.com'>PCLinuxOS Web Site</a>"'"</label>
      </text>
      <button>
        <label>OK</label>
         <variable>ABOUT</variable>
          <action type="closewindow">ABOUT</action>
      </button>
    </frame>
  </vbox>
</window>
'
}

export MAIN_DIALOG='
<window title="xcompmgr-settings" resizable="false" icon-name="xcompmgr">
<vbox spacing="25">
	<hbox>
	<vbox>
	<frame '$" Main function "'>
	    <checkbox>
			<label>"'$"Default setting, posted by LKJ (can not be selected with different settings)"'"</label>
			<variable>S01</variable>
		</checkbox>	
		<checkbox>
			<label>"'$"Use automatic server-side compositing. Faster, but no special effects"'"</label>
			<variable>S0</variable>
		</checkbox>	
		<checkbox>
			<label>"'$"Normal client-side compositing with transparency support"'"</label>
			<variable>S1</variable>
		</checkbox>
		<checkbox>
			<label>"'$"Draw server-side shadows with sharp edges"'"</label>
			<variable>S2</variable>
		</checkbox>
		<checkbox>
			<label>"'$"Draw client-side shadows with fuzzy edges"'"</label>
			<variable>S3</variable>
		</checkbox>		
		<checkbox>
			<label>"'$"Fade windows in/out when opening/closing"'"</label>
			<variable>S4</variable>
		</checkbox>	
		<checkbox>
			<label>"'$"Avoid drawing shadows on dock/panel windows"'"</label>
			<variable>S5</variable>
		</checkbox>	
		<checkbox>
			<label>"'$"Fade windows during opacity changes"'"</label>
			<variable>S6</variable>
		</checkbox>	
		<checkbox>
			<label>"'$"Enable synchronous operation (for debugging)"'"</label>
			<variable>S7</variable>
		</checkbox>	
	</frame>
	<frame '$"Separate Settings"'>
		<checkbox>
			<label>"'$"Add a command, with copy'n'paste (can not be selected with different settings)."'"</label>
			<variable>ADD</variable>
				<action>if true enable:S24</action>
				<action>if false disable:S24</action>
		</checkbox>	
		<hbox>
			<text><label>'$"Entry:"'</label></text>
				<entry>
					<variable>S24</variable>
				<visible>disabled</visible>
			</entry>
		</hbox>						
	</frame>
	</vbox>
	<frame '$"Advanced Settings"'>
		<checkbox>
			<label>"'$"Specifies which display should be managed"'"</label>
			<variable>S8</variable>
				<action>if true enable:S9</action>
				<action>if false disable:S9</action>
		</checkbox>	
		<hbox>
			<text><label>'$"Display:"'</label></text>
				<entry>
					<variable>S9</variable>
				<visible>disabled</visible>
			</entry>
		</hbox>
		<checkbox>
			<label>"'$"Specifies the blur radius for client-side shadows "'"</label>
			<variable>S10</variable>
				<action>if true enable:S11</action>
				<action>if false disable:S11</action>
		</checkbox>	
		<hbox>
			<text><label>'$"Radius (default 12):"'</label></text>
				<entry>
					<variable>S11</variable>
				<visible>disabled</visible>
			</entry>
		</hbox>	
		<checkbox>
			<label>"'$"Specifies the translucency for client-side shadows "'"</label>
			<variable>S12</variable>
				<action>if true enable:S13</action>
				<action>if false disable:S13</action>
		</checkbox>	
		<hbox>
			<text><label>'$"Opacity (default .75):"'</label></text>
				<entry>
					<variable>S13</variable>
				<visible>disabled</visible>
			</entry>
		</hbox>	
		<checkbox>
			<label>"'$"Specifies the left offset for client-side shadows "'"</label>
			<variable>S14</variable>
				<action>if true enable:S15</action>
				<action>if false disable:S15</action>
		</checkbox>	
		<hbox>
			<text><label>'$"Left-Offset (default -15):"'</label></text>
				<entry>
					<variable>S15</variable>
				<visible>disabled</visible>
			</entry>
		</hbox>	
		<checkbox>
			<label>"'$"Specifies the top offset for clinet-side shadows "'"</label>
			<variable>S16</variable>
				<action>if true enable:S17</action>
				<action>if false disable:S17</action>
		</checkbox>	
		<hbox>
			<text><label>'$"Top-Offset (default -15):"'</label></text>
				<entry>
					<variable>S17</variable>
				<visible>disabled</visible>
			</entry>
		</hbox>		
		<checkbox>
			<label>"'$"Specifies the opacity change between steps while fading in "'"</label>
			<variable>S18</variable>
				<action>if true enable:S19</action>
				<action>if false disable:S19</action>
		</checkbox>	
		<hbox>
			<text><label>'$"Fade-In-Step (default 0.028):"'</label></text>
				<entry>
					<variable>S19</variable>
				<visible>disabled</visible>
			</entry>
		</hbox>			
		<checkbox>
			<label>"'$"Specifies the opacity change between steps while fading out "'"</label>
			<variable>S20</variable>
				<action>if true enable:S21</action>
				<action>if false disable:S21</action>
		</checkbox>	
		<hbox>
			<text><label>'$"Fade-Out-Step (default 0.03):"'</label></text>
				<entry>
					<variable>S21</variable>
				<visible>disabled</visible>
			</entry>
		</hbox>	
		<checkbox>
			<label>"'$"Specifies the time between steps in a fade in milliseconds "'"</label>
			<variable>S22</variable>
				<action>if true enable:S23</action>
				<action>if false disable:S23</action>
		</checkbox>	
		<hbox>
			<text><label>'$"Fade-Delta-Time (default 10):"'</label></text>
				<entry>
					<variable>S23</variable>
				<visible>disabled</visible>
			</entry>
		</hbox>				
	</frame>
	</hbox>
	<hbox start="true">
	 <vbox>
      <text justify="1" use-markup="True" width-chars="60">
        <label>"<b>'$"Autostart status:"'</b> '$AUTO'"</label>
      </text>	
      <text justify="1" use-markup="True" width-chars="60">
        <label>"<b>'$"Current settings:"'</b> '$INFO'"</label>
      </text>
     </vbox>
        <button>
	        <input file icon="gtk-help"></input>
            <label>'$"About"'</label>
            <action type="launch">ABOUT</action>
        </button>
        <button>
	        <input file icon="gtk-execute"></input>
            <label>'$"Autostart"'</label>
            <action>cp /usr/share/applications/xcompmgr_start.desktop /'$HOME'/.config/autostart/ ; if [ -f /'$HOME'/.config/autostart/compton_start.desktop ]; then rm -rf /'$HOME'/.config/autostart/compton_start.desktop ; fi </action>
        </button>    
		<button>
			<label>"'$" Save "'"</label>
			<input file icon="document-save"></input>
			<action type="exit">save</action>
		</button>
		<button>
			<label>"'$" Cancel "'"</label>
			<input file icon="gtk-cancel"></input>
			<action type="exit">cancel</action>
		</button>
	</hbox>
</vbox>
</window>
'

#	Eval gtkdialog stdout into bash variables

I=$IFS; IFS=""
for STATEMENT in  $(gtkdialog --center --program=MAIN_DIALOG); do
	eval $STATEMENT
done
IFS=$I
	
	if [ -d $CONF_DIR ];then echo -e $"$CONF_DIR directory found\n\n"; 
	else 
		if [ ! -d $HOME/.xcompmgr ]; then mkdir $HOME/.xcompmgr; fi 
		mkdir $CONF_DIR && echo $"Creating directory $CONF_DIR " && if \
		[ -d $CONF_DIR ];then echo $"Done" || echo -e $"$CONF_DIR directory could not be created\n\n"; fi
	fi
	
	
		# Process variables if dialog exit status == save
	if [ "$EXIT" == "save" ];then
		echo "$S01" > $CONF_DIR/s01
		echo "$S0" > $CONF_DIR/s0
		echo "$S1" > $CONF_DIR/s1
		echo "$S2" > $CONF_DIR/s2
		echo "$S3" > $CONF_DIR/s3
		echo "$S4" > $CONF_DIR/s4
		echo "$S5" > $CONF_DIR/s5
		echo "$S6" > $CONF_DIR/s6
		echo "$S7" > $CONF_DIR/s7
		echo "$S8" > $CONF_DIR/s8
		echo "$S9" > $CONF_DIR/s9
		echo "$S10" > $CONF_DIR/s10
		echo "$S11" > $CONF_DIR/s11
		echo "$S12" > $CONF_DIR/s12
		echo "$S13" > $CONF_DIR/s13
		echo "$S14" > $CONF_DIR/s14
		echo "$S15" > $CONF_DIR/s15
		echo "$S16" > $CONF_DIR/s16
		echo "$S17" > $CONF_DIR/s17
		echo "$S18" > $CONF_DIR/s18
		echo "$S19" > $CONF_DIR/s19
		echo "$S20" > $CONF_DIR/s20
		echo "$S21" > $CONF_DIR/s21
		echo "$S22" > $CONF_DIR/s22
		echo "$S23" > $CONF_DIR/s23
		echo "$S24" > $CONF_DIR/s24
	else	#do nothing
		yad --button="gtk-ok:0" --window-icon=$ICON --width=250 \
		--title="xcompmgr-settings" --timeout 8 \
		--text=$"No settings were changed. Current configuration is maintained..."
		exec xcompmgr_start
		exit 0
	fi
	read_settings
	yad --button="gtk-ok:0" --window-icon=$ICON --width=250 \
	--title="xcompmgr-settings" --timeout 8 --text=$"New settings will be used..."
	exec xcompmgr_start
