#!/bin/bash
#***********************************************************************
# compton_start simple script to start compton
# Generate under PCLinuxOS-LXDE for LXDE-Sessions
# Author: Daniel Meiß-Wilhelm (leiche) 
# Lincence: GPL
# Web site: www.pclinuxos.com/forum
# Required: gtkdialog bash compton
#*********************************************
# Code 0 no direct rendering
# Code 1 no lxde session
#*********************************************
export TEXTDOMAIN=compton_start
export TEXTDOMAINDIR="/usr/share/locale"
#
# check if will running xcompmgr
#
GLX=`glxinfo | grep 'direct rendering' `
#
# check if xcompmgr still running
#
RUN=`ps -e | grep -w compton | cut -d ":" -f3 | cut -d " " -f2`
RUN1=`ps -e | grep -w xcompmgr | cut -d ":" -f3 | cut -d " " -f2`
#
# some variables
#
TITLE=compton-start
VERSION=0.08
ICON=/usr/share/pixmaps/xcomp.png
CONF_DIR=~/.compton
if [ -f ~/.compton/main ];then
MAIN=`cat $CONF_DIR/main`
else
MAIN="-cCGfF -o 0.38 -O 200 -I 200 -t 0.02 -l 0.02 -r 3.2 -D2 -m 0.88"
fi
#
# running checks
#
if [ "$GLX" = "direct rendering: Yes" ]; then 
	echo $"compton will successfully start!" 
	else
#------------------- INFO DIALOG ---------------------------------------
export INFO_DIALOG='
<window window_position="1" title="'$TITLE$VERSION'" icon-name="xcomp">
	<vbox>
		<hbox border-width="30">
			<pixmap>
				<input file icon="gtk-dialog-info"></input>
			</pixmap>
			<text use-markup="True">
				<label>"'$"Your video card support no direct rendering, exit script... "'"</label>
			</text>
		</hbox>
		<hseparator></hseparator>
		<hbox homogeneous="true">
			<button ok></button>
		</hbox>
	</vbox>
</window>
'
gtkdialog --program=INFO_DIALOG 
    exit 0
fi 
if [ "$RUN1" = "xcompmgr" ];then
	echo $"xcompmgr is still running...\nWill now stopped!"
#------------------- INFO DIALOG ---------------------------------------
export INFO0_DIALOG='
<window window_position="1" title="'$TITLE$VERSION'" icon-name="pclos-convert">
	<vbox>
		<hbox border-width="30">
			<pixmap>
				<input file icon="gtk-dialog-info"></input>
			</pixmap>
			<text use-markup="True">
				<label>"'$"xcompmgr is still running, it is in a conflict with compton!
<b>It will now stopped!</b> "'"</label>
			</text>
		</hbox>
		<hseparator></hseparator>
		<hbox homogeneous="true">
			<button use-stock="true" label="gtk-ok">
				<action>killall compton</action>
				<action type="exit">INFO0_DIALOG</action>
			</button>
			<button cancel></button>
		</hbox>
	</vbox>
</window>
'
gtkdialog --program=INFO0_DIALOG
fi
		
if [ "$RUN" = "compton" ];then
	echo $"compton is still running...!"
	exit 0
fi

if [ -f ~/.config/openbox/rc.xml ];then
SET1=`grep -i animateIconify ~/.config/openbox/rc.xml | cut -d ">" -f2 | cut -d "<" -f1`
	if [ "$SET1" = "yes" ];then
#------------------- INFO DIALOG ---------------------------------------
export INFO1_DIALOG='
<window window_position="1" title="'$TITLE$VERSION'" icon-name="xcomp">
	<vbox>
		<hbox border-width="30">
			<pixmap>
				<input file icon="gtk-dialog-info"></input>
			</pixmap>
			<text use-markup="True">
				<label>"'$"compton wont propper working, you have enabled 
		<b>Animate iconify and restore</b>. 
This will now <b>tick off</b>!
To enable it, try:
Open (obconf) <b>OpenBox Configuration Manager</b>
<i>Appearanc</i> -&gt; <i>Animate iconify and restore</i> <b>tick on</b> "'"</label>
			</text>
		</hbox>
		<hseparator></hseparator>
		<hbox homogeneous="true">
			<button ok></button>
		</hbox>
	</vbox>
</window>
'
gtkdialog --program=INFO1_DIALOG 		
#		obconf
		perl -pi -e "s|<animateIconify>yes</animateIconify>|<animateIconify>no</animateIconify>|g" $HOME/.config/openbox/rc.xml
	fi
fi	
if [ -f ~/.config/openbox/lxde-rc.xml ];then
SET1=`grep -i animateIconify ~/.config/openbox/lxde-rc.xml | cut -d ">" -f2 | cut -d "<" -f1`
	if [ "$SET1" = "yes" ];then
		perl -pi -e "s|<animateIconify>yes</animateIconify>|<animateIconify>no</animateIconify>|g" ~/.config/openbox/lxde-rc.xml
	fi
fi

compton $MAIN > $CONF_DIR/compton.log 2>&1 && sleep 1
if [ `ps -e | grep -w compton | cut -d ":" -f3 | cut -d " " -f2` = "compton" ];then
	echo $"compton successfully started..."
else
	echo $"something went wrong, check compton settings..."
#------------------- INFO DIALOG ---------------------------------------
export INFO2_DIALOG='
<window window_position="1" title="'$TITLE$VERSION'" icon-name="xcomp">
	<vbox>
		<vbox border-width="30">
			<pixmap>
				<input file icon="gtk-dialog-info"></input>
			</pixmap>
			<text use-markup="True">
				<label>"'$"Something went wrong, check compton settings..."'"</label>
			</text>
			<text use-markup="True">
				<label>"'$"<b>Compton quits with:</b>"'"</label>
			</text>
			<text wrap="false" xalign="0" selectable="false">
				<input>cat '$CONF_DIR/compton.log'</input>
			</text>
		</vbox>
		<hseparator></hseparator>
		<hbox homogeneous="true">
			<button ok></button>
		</hbox>
	</vbox>
</window>
'
gtkdialog --program=INFO2_DIALOG 
	exec compton_settings	
fi
exit 0

