#!/bin/sh

AUTOLOADGL="You use fglrx driver. If correct all function? If yes then press OK autoload always or cancel to exit"
AUTOLOADGLTEST="This security mechanisme if used LibGL preload. If PC NO freeze then prress OK for autoload or cancel to exit"
DETECTMSG="Direct Rendering Detected. Now available more visual effects. Do you want enable it?"
ROOTPASS="For this action nedeed ROOT access, Please enter root password now and press OK"
ALREADY="Already Enabled More Visual Effects. Do you want to turn off this effects? Press [ OK ] button to Disable, or [ Cancel ] to exit. "
DISABLE="Visual effect ENABLED, press OK to disable detect or Cancel to close dialog"
SKIP="Disable 3Detect function now? Press [ OK ] to disable detect or [ Cancel ] to close dialog."
WARNGLX="3D/GLX support not available! Without don't working Visual Effects. Disable now autolad?"
WARN="3D/GLX support not available! Without don't working Visual Effects. For use, please set VGA card or change device driver."
DONE="action succeful! Please relogin in system to apply"
WARNFGLRX="Warning! \nfglrx driver set in xorg. Preload libGL for this? "
FINALIZE="Now enabled Visual effects, If you want enabled always ON then press OK button or Cancel to exit"

case $LANGUAGE in
	hu* )	
		AUTOLOADGL="Most az fgrlx meghajtr hasznlja. Ha rendben van minden kattintson az OK-ra az automatikus betltshez s az ablak kikapcsolshoz vagy a Mgsemre a kilpshez."
		AUTOLOADGLTEST="Ez egy biztonsgi mechanizus ha hasznlta a LibGL preloadot.Ha a PC nem fagyott le s szeretn ha automatikusan betltdne kattintson az OK-ra vagy a Mgsemre a kilpshez"
		DETECTMSG="Direct Rendring tmogats. gy most elthetv vlt tbb vzulis effekt. Be akarja kapcsolni ket?"
		ROOTPASS="Ehhez a mvelehthez ROOT jogokra van szksge. Krem adja meg a root jelszt s kattintson az OK-ra"
		ALREADY="Mr be vannak kapcsolva a vzulis effektek. Ki akarja ezeket most kapcsolni? Kattintson az [ OK ] gombra a kikapcsolshoz, vagy a [ Mgsem ] -re a kilpshez. "
		DISABLE="Vzulis effektek BEKAPCSOLVA, kattintson az OK-ra a detektls kikapcsolshoz vagy a Mgsem-re a kilpshez"
		SKIP="Kikapcsolja a 3Detect funkcit? Kattintson az [ OK ]-ra az automatikus detektls kikapcsolshoz vagy a [ Mgsem ]-re a kilpshez"
		WARNGLX="3D/GLX tmogats nem elrhet! Enlkl nem mkdnek a vzulis effektek. Kikapcsolja az automatikus betltst?"
		WARN="3D/GLX tmogats nem elrhet! Enlkl nem mkdnek a vzulis effektek. Ha hasznlni akarja a 3D-s effekteket, be kell lltani a grafikus krtyt, esetleg meghajt kell cserlnie."
		DONE="mvelet sikeresen befejezdtt! Krem jelentkezzen be jra a rendszerbe a hasznlathoz."
		WARNFGLRX="Figyelmeztets! \nfglrx driver van belltva az xorg-ban. Hasznljam a libGL preload funkcit? Sajnos az j fglrx meghajtkkal mskpp nem mkdnek az effektek. Persze meg lehet prblni enlkl is ha a gp lefagyst okozn!" 	    
		FINALIZE="A vizulis effektek be vannak kapcsolva, Szeretn, hogy ezek llandan betltdjenek? Akkor kattintson az OK gombra vagy a Mgsemre a kilpshez."
esac

PRG="/usr/bin/3detect"
STATUS=`cat /etc/sysconfig/compositing-wm | grep START | awk -F= '{ print $2}'`
SURUN=`which gksu 2>/dev/null || which gsu 2>/dev/null || which kdesu 2>/dev/null || echo "Error1"`

if [ "$SURUN" = Error1 ]
then 
    echo "[Warning! The SU manager not found. Please check your installation]"
    exit 
fi


yesno() {
    zenity --question --text "$DETECTMSG"
    ret=$?
    if [ "$ret" -ne 1 ];then
	fglrxcheck
	compositing-wm-start --replace
	    zenity --question --text "$FINALIZE"
	    ret=$?
	    if [ "$ret" -ne 1 ];then
		writeyes
		rm -f $HOME/.3det*
	    fi
	 
    fi
    zenity --question --text "$SKIP"
    ret=$?
    echo "SKIP: $ret"
    test "$ret" -ne 1 && touch $HOME/.3detect 
    test "$ret" -ne 0 && rm -f $HOME/.3det* 
    exit
}

yes() {
    zenity --entry --hide-text --text "$ROOTPASS"
}

writeyes() {
if [ "$UID" = "0" ];then
echo "Enabled"
DONEMSG="Enable Effects $DONE"
cat << EOF > /etc/sysconfig/compositing-wm
COMPOSITING_WM_START=yes
COMPOSITING_WM=compiz-fusion
EOF

donemsg
exit
else 
    echo "Switch to admin.."
    $SURUN "$PRG --enable"
    exit
fi


}
writeno() {
if [ "$UID" = "0" ];then
echo "Disabled"
DONEMSG="Disable Effects $DONE"
cat << EOF > /etc/sysconfig/compositing-wm
COMPOSITING_WM_START=no
COMPOSITING_WM=compiz-fusion
EOF
donemsg
exit
else 
    echo "Switch to admin.."
    $SURUN "$PRG --disable"
    exit
fi

}

status() {
if [ "$STATUS" = "yes" ];then
    zenity --question --text "$ALREADY"
    ret=$?
    test "$ret" -ne 1 && writeno
    test "$ret" -ne 0 && exit
    
fi

}

fglrxcheck() {
		if [ -n "$fglrx" ];then
		    echo "!FGLRX Warn!"
		    if [ -f $HOME/.3detect ]&&[ -f $HOME/.3detpretest ];then
			echo "Found .3detect not found 3detpretest"
			zenity --question --text "$AUTOLOADGLTEST"
			ret=$?
			test "$ret" -ne 1 && touch $HOME/.3detpreload
			if [ -f $HOME/.3detpreload ];then
			    rm -r $HOME/.3detpretest 
			    fglrxpreload
			fi
			    #echo "Create PreloadLock? $ret"
			exit
		     else 
		        if [ -f $HOME/.3detpreload ];then
				echo "Autoload libGL..."
		    		fglrxpreload
			    exit
			 else
		    	    if [ -f $HOME/.3detpreoff ];then
				echo "libGL PreLoad OFF"
				exit
			    fi
			    
			fi				
		    fi
		    if (pidof compiz &>/dev/null);then
			echo "Already running compiz"
		      else
			echo "Compiz none in ps "
    			zenity --question --text "$WARNFGLRX"
    			ret=$?
			echo "WARN $ret"
			test "$ret" -ne 0 && disabledetect
			echo "Step.."			
			test "$ret" -ne 1 && fglrxpreload #&& exit
			echo "Autolad?"
			zenity --question --text "$AUTOLOADGL"
			ret=$?
			test "$ret" -ne 1 && touch $HOME/.3detpretest #&& fglrxpreload
			echo "Continue.."
		        #echo "Upto FGLRX Load.."
		    fi
		fi

}

fglrxpreload() {
    echo "!! Preload fglrx libGL !! Freeze PC?"
	export LD_LIBRARY_PATH=/usr/lib/ati:$LD_LIBRARY_PATH
	LIBGL_ALWAYS_INDIRECT=1
	LD_PRELOAD=/usr/lib/ati/libGL.so.1.2 compositing-wm-start --replace
	ret=$?
	test "$ret" -ne 1 && sleep 2 && kwin --replace &    

}

disabledetect() {
    zenity --question --text "$DISABLE"
    ret=$?
    test "$ret" -ne 1 && touch $HOME/.3detect .3detpreoff && exit
    test "$ret" -ne 0 && exit
}

create_menu() {
    if [ ! -f $HOME/.menu/emerald-theme-manager ];then
	echo "Create Emerald Menu"
        mkdir -p $HOME/.menu
	echo "?package(emerald): command=\"emerald-theme-manager\" needs=\"X11\" icon=\"emerald-theme-manager-icon.png\" section=\"System\" title=\"Emerald WM Themes\" longtiltle=\"Theme Manager for Emerald\"" > $HOME/.menu/emerald-theme-manager
	update-menus -u
    fi
}

donemsg() {
    zenity --info --text "$DONEMSG"
}

clear
glx=`glxinfo | grep ^direct.rendering | awk -F: '{ print $2 }' | sed -e 's| ||' 2>/dev/null`
fglrx=`cat /etc/X11/xorg.conf | grep 'Driver "fglrx"'`

if [ "$1" = "--daemon" ];then
    if [ "$STATUS" = "yes" ]&&[ "$glx" != "Yes" ];then
	zenity --question --text "$WARNGLX"
	ret=$?
	test "$ret" -ne 1 && writeno
	test "$ret" -ne 0 && exit
    fi
        if [ "$STATUS" != "yes" ];then
		if [ -f $HOME/.menu/emerald-theme-manager ];then
		    echo "Delete Emerald Menu"
		    rm -f $HOME/.menu/emerald-theme-manager
		    update-menus
		    rm -f $HOME/.3detpreload
		    rm -f $HOME/.3detpreoff
		fi
	    else    
		if [ ! -n "$fglrx" ]&&[ -f $HOME/.3detpreload ];then
		    echo "Change X driver! AutoDisable libGL preload extension now!"
		    rm -f $HOME/.3detpreload
		fi
		
		fglrxcheck

	    if [ "$STATUS" = "yes" ]&&[ ! -f $HOME/.3detect ];then
		echo "Upto createmenu"
		create_menu
		disabledetect
		#exit
		#create_menu
	    fi    
	fi
	
	if [ -f $HOME/.3detect ];then
	    if [ "$glx" != "Yes" ];then
		rm -f $HOME/.3detect
		rm -f $HOME/.3detpreload
		rm -f $HOME/.3detpreoff
	    fi
	    echo "****************************"
	    echo "* HOME lock found, exiting *"
	    echo "****************************"
	    exit
	  else
	    if [ "$glx" != "Yes" ];then
		echo "GLX Not found, exiting"
		exit
	    fi
	   echo "Upto YesNo?..."
	   
	   
	   yesno
	fi
	
fi
if [ "$1" = "--enable" ] && [ "$UID" = "0" ];then
    writeyes
    donemsg
    exit 0
fi
if [ "$1" = "--disable" ] && [ "$UID" = "0" ];then
    writeno
    donemsg
    exit 0
fi

status

if [ "$glx" != "Yes" ];then
    echo "None GLX"
	zenity --error --text "$WARN"
    exit
else 
    yesno
fi
