#!/bin/sh

# Script written by Charles Barcza (kbarcza@blackpanther.hu) for blackPanther OS
#  --- www. blackpanther .hu ---

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. "
NOALREADY="Althoug in conf files enabled current not run effect service! Reload now all visual effects ? If yes then press to [ OK ] button for reload"
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 succesful! " 
RELOGIN="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"
ALWAYSOFF="You are disable effects in always time? If yes then press to [ OK ] button."
ALTHOUGH="Although current not running! "
ONLYUSER="Enable only this user"
ALLSYSTEM="Enable on all system"
ENABLEACT="Effects "

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. "
		NOALREADY="Br a konfigban be van kacsolva jelenleg nem fut az effekt szolgltats. Szeretn jraindtani a vizulis effekteket? Ha igen akkor kattintson az [ OK ] gombra"    
		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! "
		RELOGIN="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."
		ALWAYSOFF="Szeretn kikapcsolni vglegesen az effekteket? Ha igen kattintson az [ OK ] gombra."
		ALTHOUGH="Habr jelenleg nem fut! "
		ONLYUSER="Csak ennel a felhasznalonal"
		ALLSYSTEM="A teljes rendszeren(szakerto)"
		ENABLEACT="Bekapcsolja:"
esac

. /etc/sysconfig/compositing-wm

PRG="/usr/bin/3detect"
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
	create_menu
	    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 $RELOGIN"
cat << EOF > /etc/sysconfig/compositing-wm
COMPOSITING_WM_START=auto
COMPOSITING_WM=compiz-fusion
EOF

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


}

userenable() {
DONEMSG="Enable Effects (Only User) $DONE "
cat > $HOME/.kde/Autostart/enableeffects << EOF
#!/bin/sh
#
# Created by blackPanther OS 3detect script

sleep 2
compositing-wm-start --replace

#END
EOF
chmod 755 $HOME/.kde/Autostart/enableeffects

}

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

for s in /home/*/.kde/Autostart/enableeffects ; do
rm -f $s
done

kwin --replace &
donemsg
exit
else
    pidchk
    #kwin --replace &
    zenity --question --text "$ALWAYSOFF"
    ret=$?
    #test "$ret" -ne 1 && continue
    test "$ret" -ne 0 && exit
    
    echo "Switch to admin.."
    $SURUN "$PRG --disable"
    exit
fi

}

pidchk() {
if (pidof kwin &>/dev/null);then
    echo "KWIN Run [ OK ]"
    MANAGER=kwin
    killall emerald -KILL 2>/dev/null
    #else
if (pidof emerald &>/dev/null);then
    echo "Emerald Run [ OK ]"
    MANAGER=emerald
    else
    if [ ! -n "$ECHK" ];then
	echo "PIDCHK NONE"
	#emerald --replace &
	ECHK=1
	sleep 2
	pidchk
	else
	echo "PIDCHK FND"
    fi
fi
fi
if [ ! -n "$MANAGER" ];then
    echo "Window Manager fail, restore default now"
    kwin --replace &
    killall emerald -KILL
    killall compiz -KILL
    ALTHOUGH=FIRST
 else 
    if [ "$MANAGER" = "emerald" ];then
	echo "Manager [OK]"
	#disabledetect
    fi
fi

}

status() {
pidchk
if [ "$COMPOSITING_WM_START" = "auto" -o "$COMPOSITING_WM_START" = "yes"  ];then
    if [ "$MANAGER" = "kwin" ];then
	#echo kwinrunn
	    FIRST=$ALTHOUGH
	    zenity --question --text "${NOALREADY}"
	    ret=$?
	    test "$ret" -ne 1 && yesno
	    test "$ret" -ne 0 && exit
	else
	    pidchk
	    FIRST=""
	    zenity --question --text "${FIRST}${ALREADY}"
	    ret=$?
	    test "$ret" -ne 1 && writeno
	    test "$ret" -ne 0 && exit
	    
    fi
fi

}

winmancheck() {
 echo "Detecting available Window Manager.."
 sleep 5
        if [ "$COMPOSITING_WM_START" = "yes" -o "$COMPOSITING_WM_START" = "auto" ];then
	    echo "Enable Visual Effects"
	    	    if (pidof emerald &>/dev/null);then
			echo "Emerald Run [ OK ]"
		      else
		        echo "Emerald Run [ FAIL ]"
			kwin --replace
			echo "Replace Compiz WM to general kwin WM"
		    fi
	
	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 [ "$COMPOSITING_WM_START" = "auto" -o "$COMPOSITING_WM_START" = "yes" ]&&[ "$glx" != "Yes" ];then
	zenity --question --text "$WARNGLX"
	ret=$?
	test "$ret" -ne 1 && writeno
	test "$ret" -ne 0 && exit
    fi
        if [ "$COMPOSITING_WM_START" != "yes" ]&&[ "$COMPOSITING_WM_START" != "auto" ];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 [ "$COMPOSITING_WM_START" = "yes" ]&&[ "$COMPOSITING_WM_START" = "auto" ]&&[ ! -f $HOME/.3detect ];then
	    #if [ "$COMPOSITING_WM_START" = "yes" -o "$COMPOSITING_WM_START" = "auto" ]&&[ ! -f $HOME/.3detect ];then
		winmancheck
		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
	if [ ! -f $HOME/.menu/emerald-theme-manager ];then
	    create_menu
	fi  
	    pidchk	
	    echo "****************************"
	    echo "* HOME lock found, exiting *"
	    echo "****************************"
	    exit
	  else
	    if [ "$glx" != "Yes" ];then
		echo "GLX Not found, exiting"
		exit
	    fi
	   #status
	   echo "Upto YesNo?..."
	   pidchk
	   winmancheck
	   yesno
	fi
	
fi
if [ "$1" = "--enable" ] && [ "$UID" = "0" ];then
    how=`zenity --list --column "Nbr" --column "$ENABLEACT" 1 "$ONLYUSER" 2 "$ALLSYSTEM"`
    echo "Comm: $how"
    test "$how" -ne 1 && writeyes || DONEMSG="ABORT ACTION"
    test ! "$how" -ne 1 && userenable || DONEMSG="ABORT ACTION"
    donemsg
    exit 0
fi
if [ "$1" = "--disable" ] && [ "$UID" = "0" ];then
    writeno
    donemsg
    exit 0
fi
if [ "$1" = "--daemon" ];then
    echo "Daemon mode.. "
 else
    status
fi
if [ "$glx" != "Yes" ];then
    echo "None GLX"
	zenity --error --text "$WARN"
    exit
else 
    yesno
fi
