#!/bin/bash
#Auteur : Mav
#Contact : mav@cairo-dock.org
#Version : 16/11/08

#Changelog
# 16/11/08 : 	Refonte du script pour la version 2
# 13/11/08 :	Mise à jour des versions de cairo, glitz et pixman (pas de mises à jour auto)
# 22/10/08 :	Ajout de intrepid dans la détection des distribs
# 23/05/08 :	Ajout de la détection de la distrib et des plugins correspondant
# 20/05/08 : 	Ajout du choix du mode compilation (--full-compile pour forcer l'autoreconf)
# 28/03/08 : 	Ajout de l'icone pour XFCE et KDE (en test)
#				Suppression de l'icone lors de la désinstall
#				Téléchargement et recompilation de tous les plugins à chaque mise à jour de cairo-dock (en une passe)
# 25/03/08 : 	ajout de l'applet Switcher pour test :)
# 22/03/08 :	ajout de l'icone Cairo-Dock SVN dans le menu Applications de Gnome (uniquement)
# 20/03/08 :	ajout des themes dans une branches séparés
# 16/02/08 : 	ajout des paquets libxxf86vm-dev et libx11-dev danss les dépendances
# 12/02/08 : 	ajout de la fonction de desinstallation de Glitz
#				modification de la fonction de vérification des erreurs lors de l'install
		

DEBUG=0 # Warning, do not forget to change !!!
DIR=$(pwd)
LOG_CAIRO_DOCK=$DIR/log.txt
LOG_CAIRO_GLITZ=$DIR/log_glitz.txt
SCRIPT_CAIRO_WITH_GLITZ=$DIR/launch_cairo_dock_with_glitz.sh
SCRIPT="cairo-dock_svn.sh"
SCRIPT_SAVE="cairo-dock_svn.sh.save"
SCRIPT_NEW="cairo-dock_svn.sh.new"
HOST="http://svn.cairo-dock.org"
LIBCAIRO="cairo-glitz-1.8.0"
GLITZ="glitz-0.5.7"
PIXMAN="pixman-0.12.0"


PLUGINS="alsaMixer Animated-icons Cairo-Penguin Clipper clock compiz-icon cpusage Dbus desklet-rendering disk-usage dock-rendering drop-indicator dustbin GMenu icon-effect illusion logout mail motion-blur musicPlayer netspeed  nVidia powermanager rame rhythmbox shortcuts showDesklets showDesktop show-mouse slider stack stacks switcher systray terminal tomboy Xgamma xmms weather wifi"
PLUGINS_GNOME="gnome-integration"
PLUGINS_GNOME_OLD="gnome-integration-old"
PLUGINS_XFCE="xfce-integration"

NEEDED="subversion libtool build-essential automake1.9 autoconf m4 autotools-dev pkg-config libcairo2-dev libgtk2.0-dev librsvg2-dev libglitz1-dev libcairo2 librsvg2-2 libglitz1 libglitz-glx1 libglitz-glx1-dev libdbus-glib-1-dev libgnomeui-dev zenity intltool gettext libvte-dev libxxf86vm-dev libx11-dev libalsa-ocaml libalsa-ocaml-dev libasound2-dev libxtst-dev libxul-dev libgnome-menu-dev libgtkglext1-dev freeglut3-dev glutg3-dev"
NEEDED_XFCE="libthunar-vfs-1-dev"
NEEDED_GNOME="libgnomevfs2-dev"

UPDATE=0
UPDATE_PLUG_INS=0
UPDATE_CAIRO_DOCK=0
ALREADY_LOGUED=0
CAIRO_GLITZ_OK=0
ERROR=0
HTTP_TYPE="http"
DESKTOP_ENTRY_NAME=cairo-dock_svn.desktop
GLITZ_PRESENT=0
FULL_COMPILE=0
DISTRIB=""
INSTALL_GLITZ_OK=1
INSTALL_CAIRO_DOCK_OK=1


NORMAL="\\033[0;39m"
BLEU="\\033[1;34m"
VERT="\\033[1;32m" 
ROUGE="\\033[1;31m"


#######################################################################
#	Functions install
#######################################################################

install_cairo_dock() {

	rm -Rf $LOG_CAIRO_DOCK > /dev/null

	echo "Installation of cairo-dock `date`" >> $LOG_CAIRO_DOCK	

	echo "" >> $LOG_CAIRO_DOCK
	echo -e "$BLEU""Installation of cairo-dock"
	echo ""

	install_cairo >> $LOG_CAIRO_DOCK 2>&1

	if [ $? -ne 0 ]; then
		INSTALL_CAIRO_DOCK_OK=1
		echo -e "$ROUGE""Error"
		check $LOG_CAIRO_DOCK "CD"	
	else
		echo -e "$VERT""Success"
	fi
	echo -e "$NORMAL"
	echo "" >> $LOG_CAIRO_DOCK

}

install_cairo() {
	cd $DIR/cairo-dock
	autoreconf -isvf && ./configure --prefix=/usr $OPTIONS && make clean all && INSTALL_CAIRO_DOCK_OK=0

	if [ $? -ne 0 ]; then
		return 1
	fi

	sudo make install
	cd ..
}


install_cairo_dock_plugins() {
	echo -e "$BLEU""Installing the plug-in $1"
	
	echo "Installation of plugin" $1 "of `date`" >> $LOG_CAIRO_DOCK
	echo "" >> $LOG_CAIRO_DOCK	
	
	echo ""

	install_plugins $1 >> $LOG_CAIRO_DOCK 2>&1

	if [ $? -ne 0 ]; then
		INSTALL_CAIRO_DOCK_OK=1
		echo -e "$ROUGE""Error"	
	else
		echo -e "$VERT""Successful"
	fi	

	echo -e "$NORMAL"
	echo "" >> $LOG_CAIRO_DOCK

}


install_plugins() {
	cd $DIR/plug-ins
	cd $1
	INSTALL_CAIRO_DOCK_OK=1
	if  [ ! -e configure ] || [ $FULL_COMPILE -eq 1 ];then
		autoreconf -isvf 
	fi
	./configure --prefix=/usr $OPTIONS && make clean all && INSTALL_CAIRO_DOCK_OK=0
	
	if [ $? -ne 0 ]; then
		return 1
	fi

	sudo make install
	cd ..
				
}


install_cairo_dock_themes() {

	echo -e "$BLEU""Installation of themes"
	if [ $ALREADY_LOGUED -ne 1 ]; then
		if [ -f $LOG_CAIRO_DOCK ]; then		
			rm -Rf $LOG_CAIRO_DOCK > /dev/null
			ALREADY_LOGUED=1
			echo "Installation of themes of `date`" >> $LOG_CAIRO_DOCK
			echo "" >> $LOG_CAIRO_DOCK	
		fi
	fi
	
	echo "Installation of themes of `date`" >> $LOG_CAIRO_DOCK
	echo "" >> $LOG_CAIRO_DOCK	
	
	echo ""
	
	install_themes >> $LOG_CAIRO_DOCK 2>&1
	
	if [ $? -ne 0 ]; then
		INSTALL_CAIRO_DOCK_OK=1
		echo -e "$ROUGE""Error"	
	else
		echo -e "$VERT""Successful"
	fi
	
	echo "" >> $LOG_CAIRO_DOCK	
	echo ""
}


install_themes() {
	cd $DIR/themes
	if  [ ! -e configure ] || [ $FULL_COMPILE -eq 1 ];then
		autoreconf -isvf 
	fi
	./configure --prefix=/usr $OPTIONS && make clean all && INSTALL_CAIRO_DOCK_OK=0
	
	if [ $? -ne 0 ]; then
		return 1
	fi

	sudo make install
	cd ..

}


install(){
	echo -e "$BLEU""Checking packages required to compile" 
	for tested in $NEEDED
	do
		check_dependancies $tested
	done

	if [ $ENV -eq 1 ];then #Gnome
		check_dependancies $NEEDED_GNOME
	elif [ $ENV -eq 3 ];then #XFCE
		check_dependancies $NEEDED_XFCE
	fi

	echo -e "$VERT""Verification OK""$NORMAL"""
	echo -e ""
	echo -e "$BLEU""Verification of the presence of Glitz"
	if [ -e $LOG_CAIRO_GLITZ ]; then
		echo -e "$VERT""Cairo has already been compiled with Glitz"
		echo -e "$NORMAL"""
		OPTIONS="--enable-glitz"
		GLITZ_PRESENT=1
	else
		echo -e "$VERT""Cairo was not compiled with Glitz"
		echo -e "$NORMAL"""
	fi
	sleep 3
	echo -e "$BLEU"
	echo "This is the first time you install the SVN Cairo-Dock"
	echo ""
	echo "Downloading data. This may take a few minutes"
	echo -e "$NORMAL"
	sleep 2

	if [ ! -d $DIR/cairo-dock ]; then
		echo -e "$BLEU""Downloading cairo-dock"
		svn checkout $HTTP_TYPE://svn.berlios.de/svnroot/repos/cairo-dock/trunk/cairo-dock
		if [ $? -ne 0 ]; then
			echo -e "$ROUGE""Unable to connect to SVN server, please check your Internet connection or try again later"
			exit
		fi
	fi

	echo -e "$NORMAL"

	if [ ! -d $DIR/plug-ins ]; then
		echo -e "$BLEU""Downloading plugins"
		svn checkout $HTTP_TYPE://svn.berlios.de/svnroot/repos/cairo-dock/trunk/plug-ins/
		if [ $? -ne 0 ]; then
			echo -e "$ROUGE""Unable to connect to SVN server, please check your Internet connection or try again later"
			exit
		fi
	fi

	echo -e "$NORMAL"
	
	if [ ! -d $DIR/themes ]; then
		echo -e "$BLEU""Downloading themes"
		svn checkout $HTTP_TYPE://svn.berlios.de/svnroot/repos/cairo-dock/trunk/themes/
		if [ $? -ne 0 ]; then
			echo -e "$ROUGE""Unable to connect to SVN server, please check your Internet connection or try again later"
			exit
		fi
	fi

	echo ""
	echo -e "$BLEU""Data downloaded. The installation will begin"
	echo "This may take several minutes and slow down your system"
	echo -e "$NORMAL"

	sleep 5
	
	install_cairo_dock

	for plugins in $PLUGINS
	do
		install_cairo_dock_plugins $plugins
	done
	
	for plugins_integration in $PLUGINS_INTEGRATION
	do
		install_cairo_dock_plugins $plugins_integration
	done

	install_cairo_dock_themes

	check $LOG_CAIRO_DOCK "CD"	
	
}


reinstall(){

	echo -e "$BLEU""Verification of the presence of Glitz"
	if [ -e $LOG_CAIRO_GLITZ ]; then
		echo -e "$VERT""Cairo has already been compiled with Glitz"
		echo -e "$NORMAL"""
		OPTIONS="--enable-glitz"
		GLITZ_PRESENT=1
	else
		echo -e "$VERT""Cairo was not compiled with Glitz"
		echo -e "$NORMAL"""
	fi

	FULL_COMPILE=1

	install_cairo_dock
	for plugins in $PLUGINS
	do
		install_cairo_dock_plugins $plugins
	done
	for plugins_integration in $PLUGINS_INTEGRATION
	do
		install_cairo_dock_plugins $plugins_integration
	done		
	install_cairo_dock_themes
	check $LOG_CAIRO_DOCK "CD"
}



#######################################################################
#	Functions uninstall
#######################################################################


uninstall() {
	echo "Uninstalling Cairo-Dock and plug-ins"	
	
	cd $DIR/cairo-dock
	sudo make uninstall > $LOG_CAIRO_DOCK 2>&1
	cd ..

	for plugins in $PLUGINS
	do
		cd $DIR/plug-ins
		cd $plugins
		sudo make uninstall >> $LOG_CAIRO_DOCK 2>&1
		cd ..
	done
	
	if [ -e /usr/share/applications/cairo-dock_svn.desktop ]; then
		sudo rm -f /usr/share/applications/cairo-dock_svn.desktop
	fi
	echo "Uninstall was performed."
	echo ""
	echo "However, your configuration file is always present."
	echo "It is in your / home / .config and called cairo-dock (note this is a hidden folder)."
	echo "You can delete it after uninstalling done"
	zenity --info --title=Cairo-Dock --text="Cairo-Dock has been uninstalled, please read the message in the terminal"	
	exit

}

uninstall_glitz() {
	echo "Removing the Cairo library with the option enabled glitz"	
	error=0
	
	if [ -d $DIR/$LIBCAIRO ]; then
		cd $DIR/$LIBCAIRO
		sudo make uninstall > $LOG_CAIRO_GLITZ 2>&1
	else
		error=1
	fi

	if [ -d $DIR/$GLITZ ]; then
		cd $DIR/$GLITZ
		sudo make uninstall >> $LOG_CAIRO_GLITZ 2>&1
	else
		error=1
	fi

	if [ -d $DIR/$PIXMAN ]; then
		cd $DIR/$PIXMAN
		sudo make uninstall >> $LOG_CAIRO_GLITZ 2>&1
	else
		error=1
	fi
	
	if [ $error -eq 0 ]; then
		echo "Uninstall was performed."
		zenity --info --title=Cairo-Dock --text="The library Cairo with option glitz was uninstalled"
	else
		echo "There was a problem during the uninstall. Please consult the file" $LOG_CAIRO_GLITZ
	fi	
}


#######################################################################
#	Functions to install Glitz
#######################################################################

install_cairo_with_glitz() {
	cd $DIR
	export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}
	export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH}
	install_glitz > $LOG_CAIRO_GLITZ 2>&1
	
}


install_glitz() {
	wget $HOST/$GLITZ.tar.gz
	wget $HOST/$PIXMAN.tar.gz
	wget $HOST/$LIBCAIRO.tar.gz
		
	cd $DIR && tar xzvf $PIXMAN.tar.gz && rm -Rf $PIXMAN.tar.gz && cd $PIXMAN	
	./configure --prefix=/usr/local && make
	sudo make install
	
	cd $DIR && tar xzvf $GLITZ.tar.gz && rm -Rf $GLITZ.tar.gz && cd $GLITZ
	./configure --prefix=/usr/local && make
	sudo make install
	
	cd $DIR && tar xzvf $LIBCAIRO.tar.gz && rm -Rf $LIBCAIRO.tar.gz && cd $LIBCAIRO
	./configure --prefix=/usr/local --enable-glitz && make && INSTALL_GLITZ_OK=0
	sudo make install

	cd $DIR
}


install_with_glitz(){
	echo -e "$BLEU""Do you want to install the library Cairo with the option of glitz enabled?"
	echo ""
	echo -e "$BLEU""This option allows for greater responsiveness to your dock using your graphics card and not your processor to perform calculations but can slow down your system or cause malfunctions"
	echo -e "$ROUGE""Warning! errors appear with ATI cards, especially with the mobile chipsets"
	echo ""
	echo -e "$ROUGE""Are you sure you want to do this? (y/n)"
	read answer_cairo_dock_glitz

	test "$answer_cairo_dock_glitz" == "y"
	if [ $? -eq 0 ]; then
		echo ""
		echo -e "$BLEU""For this, the library Cairo must have been compiled with Glitz. This requires downloading and installing 3 packages" 
		echo -e "$ROUGE""Would you do this? (y/n)"
		read answer_cairo_glitz
		test "$answer_cairo_glitz" == "y"
		if [ $? -eq 0 ]; then
			OPTIONS="--enable-glitz"
			echo -e "$NORMAL"""
			echo -e "$BLEU""Download and install the library Cairo with - glitz"
			echo -e "$ROUGE""Warning: This may take a few minutes"
			install_cairo_with_glitz
			echo -e "$VERT""Effectué"
			echo -e "$NORMAL"""
			check $LOG_CAIRO_GLITZ "GL"			
		fi	
	fi	
	
}


#######################################################################
#	Features Updates
#######################################################################


update(){
	echo -e "$BLEU""Check for updates for cairo-dock"
	echo -e "$NORMAL"
	svn update cairo-dock | egrep -i "(Actualis|rest|Upda)" && UPDATE_CAIRO_DOCK=1 || UPDATE_CAIRO_DOCK=0
	
	if [ $UPDATE_CAIRO_DOCK -eq 1 ];then
		echo -e "$VERT""An update for cairo-dock was detected"
		echo -e "$NORMAL"
		sleep 1
		install_cairo_dock
		UPDATE=1
		echo -e "$VERT""Updating and reinstalling plug-ins following the update for cairo-dock"
		echo -e "$NORMAL"""
	fi
	
	for plugins in $PLUGINS
	do
		if [ -d $DIR/plug-ins/$plugins ]; then		
				
			echo -e "$BLEU""Checking for Updates to" $plugins
			echo -e "$NORMAL"
			cd $DIR
			svn update plug-ins/$plugins | egrep -i "(Actualis|rest|Upda)" && UPDATE_PLUG_INS=1 || UPDATE_PLUG_INS=0
			if [ $UPDATE_PLUG_INS -eq 1 ];then				
				echo -e "$VERT""An updated version of the plugin $plugins was detected"
				sleep 1
				echo -e "$NORMAL"
			fi
			if [ $UPDATE_CAIRO_DOCK -eq 1 ] || [ $UPDATE_PLUG_INS -eq 1 ]; then
				install_cairo_dock_plugins $plugins
				UPDATE=1
			fi
								
		else
			echo -e "$BLEU""Downloading the new plugin" $plugins
			cd $DIR/plug-ins
			svn checkout $HTTP_TYPE://svn.berlios.de/svnroot/repos/cairo-dock/trunk/plug-ins/$plugins
			sleep 1
			echo -e "$NORMAL"
			install_cairo_dock_plugins $plugins
			UPDATE=1
		fi
	done

	cd $DIR
	for plugins_integration in $PLUGINS_INTEGRATION
	do
		if [ -d $DIR/plug-ins/$plugins_integration ]; then
			echo -e "$BLEU""Checking for updates" $plugins_integration
			echo -e "$NORMAL"
			svn update plug-ins/$plugins_integration | egrep -i "(Actualis|rest|Upda)" && UPDATE_PLUG_INS_INTEGRATION=1 || UPDATE_PLUG_INS_INTEGRATION=0
			if [ $UPDATE_PLUG_INS_INTEGRATION -eq 1 ]; then
				         echo -e "$VERT""An updated version of the plugin $plugins_integration was detected"
				         sleep 1
				         echo -e "$NORMAL"
				         install_cairo_dock_plugins $plugins_integration
				         UPDATE=1
			fi
		
		else
			echo -e "$BLEU""Downloading the new plugin" $plugins_integration
			cd $DIR/plug-ins
			svn checkout $HTTP_TYPE://svn.berlios.de/svnroot/repos/cairo-dock/trunk/plug-ins/$plugins_integration
			sleep 1
			echo -e "$NORMAL"
			install_cairo_dock_plugins $plugins_integration
			UPDATE=1
		fi
	done

	echo -e "$BLEU""Checking for theme updates"
	echo -e "$NORMAL"
	cd $DIR
	svn update themes | egrep -i "(Actualis|rest|Upda)" && UPDATE_THEMES=1 || UPDATE_THEMES=0
	if [ $UPDATE_THEMES -eq 1 ]; then
			 echo -e "$VERT""An update for themes was detected"
			 sleep 1
			 echo -e "$NORMAL"
			 install_cairo_dock_themes
			 UPDATE=1
    fi
    
    if [ $UPDATE -eq 1 ]; then	
	    check $LOG_CAIRO_DOCK "CD"
	else
		echo -e "$BLEU"
		echo "No update available"
		echo -e "$NORMAL"
		add_icon_to_gnome_menu
		zenity --info --title=Cairo-Dock --text="Click OK to close the terminal."
		exit
	fi

}


#######################################################################
#	Audit Function
#######################################################################

check() {
	echo -e "$NORMAL""Testing the integrity of the installation"
	sleep 1
	
	if [ $2 = "GL" ]; then
		if [ $INSTALL_GLITZ_OK -eq 1 ]; then
			echo -e "$ROUGE"
			echo "Errors were detected during installation."
			egrep -i "( error| Erreur)" $1
			echo "The installation of Cairo with the option enabled Glitz failed. Please see the log_glitz_NOK.txt for details"
			mv $LOG_CAIRO_GLITZ $DIR/log_glitz_NOK.txt
			exit
		else
			echo -e "$VERT"
			echo "The installation was completed successfully. You can now install Cairo-Dock"
			echo -e "$NORMAL"
			create_launch_script
			exit
		fi
	fi
	
	
	if [ $2 = "CD" ]; then
		if [ $INSTALL_CAIRO_DOCK_OK -eq 1 ]; then
			echo -e "$ROUGE"
			echo "Errors were detected during installation."
			egrep -i "( error| Erreur)" $1
			echo "lease see the log.txt file for more information or visit the forum cairo-dock to see the error in the section \"Version SVN\" "
			echo -e "$NORMAL"
			exit
		else
			echo -e "$VERT"
			echo "The installation was completed successfully."
			echo -e "$NORMAL"
			add_icon_to_gnome_menu
			zenity --info --title=Cairo-Dock --text="Click OK to close the terminal"
			exit
		fi
	fi
}


check_new_script() {
	cp $SCRIPT $SCRIPT_SAVE #pour moi :)
	echo -e "$NORMAL"""
	echo "Checking the availability of a new script"
	wget $HOST/$SCRIPT -q -O $SCRIPT_NEW	
	diff $SCRIPT $SCRIPT_NEW >/dev/null
	if [ $? -eq 1 ]; then
		echo -e "$ROUGE"		
		echo "Please restart the script, an update has been downloaded"
		echo -e "$NORMAL"
		mv $SCRIPT_NEW $SCRIPT
		sudo chmod u+x $SCRIPT
		zenity --info --title=Cairo-Dock --text="Click OK to close the terminal."
		exit
	else
		echo ""
		echo -e "$VERT""You have the latest version of the script  M@v"
	fi
	echo -e "$NORMAL"
	rm $SCRIPT_NEW

}


#######################################################################
#	Other functions
#######################################################################

create_launch_script() {
	echo "Creating a script to launch the Cairo-dock with Glitz"
	echo "export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}" > $SCRIPT_CAIRO_WITH_GLITZ
	echo "export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH}" >> $SCRIPT_CAIRO_WITH_GLITZ
	echo "cairo-dock --glitz" >> $SCRIPT_CAIRO_WITH_GLITZ
	sudo chmod u+x $SCRIPT_CAIRO_WITH_GLITZ
	echo -e "$ROUGE""Please use this script to run Cairo-dock once installed :" $SCRIPT_CAIRO_WITH_GLITZ
	echo -e "$NORMAL"
}


add_icon_to_gnome_menu() {
		
	if [ ! -e /usr/share/applications/$DESKTOP_ENTRY_NAME ]; then
		echo -e "$BLEU"
		echo "Adding icon Cairo-Dock SVN in the Applications menu"
		echo -e "$NORMAL"

		sudo cp $DIR/cairo-dock/data/icon-subdock.png /usr/share/pixmaps

		cd $DIR

		echo "[Desktop Entry]
		Encoding=UTF-8
		Name=Cairo-Dock SVN" > $DESKTOP_ENTRY_NAME
		
		if [ $GLITZ_PRESENT -eq 1 ];then
			echo "Exec=$DIR/launch_cairo_dock_with_glitz.sh" >> $DESKTOP_ENTRY_NAME
		else	
			echo "Exec=cairo-dock" >> $DESKTOP_ENTRY_NAME
		fi
		echo "Info=Cairo-Dock SVN
		Categories=Application;Utility; 
		Comment=Cairo-Dock SVN
		Comment[fr]=Cairo-Dock SVN
		Terminal=false
		Type=Application
		StartupNotify=true
		Name[fr_FR]=Cairo-Dock SVN
		Comment[fr_FR]=Cairo-Dock SVN
		Icon[fr_FR]=/usr/share/pixmaps/icon-subdock.png
		Icon=/usr/share/pixmaps/icon-subdock.png" >> $DESKTOP_ENTRY_NAME

		sudo mv $DESKTOP_ENTRY_NAME /usr/share/applications/

		echo -e "$VERT""Done"
		echo -e "$NORMAL"
	fi

}


detect_env_graph() {
	echo -e "$BLEU""Detection of the graphical environment"
	if [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then 
		ENV=1;
		if [ $DISTRIB = "INTREPID" ]; then PLUGINS_INTEGRATION=$PLUGINS_GNOME;
		elif [ $DISTRIB = "HARDY" ]; then PLUGINS_INTEGRATION=$PLUGINS_GNOME;
		elif [ $DISTRIB = "GUTSY" ]; then PLUGINS_INTEGRATION=$PLUGINS_GNOME_OLD;
		else PLUGINS_INTEGRATION=$PLUGINS_GNOME; fi
		echo -e "$VERT""Your environment is GNOME"
	elif [ x"$KDE_FULL_SESSION" = x"true" ]; then 
		ENV=2;
		echo -e "$VERT""Your environment is KDE"
	elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then 
		ENV=3;
		PLUGINS_INTEGRATION=$PLUGINS_XFCE
		echo -e "$VERT""Your environment is XFCE"
	else echo "Unable to determine your graphical environment"
	fi
	echo -e "$NORMAL"""
	return $ENV
}

detect_distrib() {
	echo -e "$NORMAL"""
	echo -e "$BLEU""Detection of distribution"
	cat /etc/lsb-release | grep "intrepid" > /dev/null && DISTRIB="INTREPID"
	cat /etc/lsb-release | grep "hardy" > /dev/null && DISTRIB="HARDY" 
	cat /etc/lsb-release | grep "gutsy" > /dev/null && DISTRIB="GUTSY"
	
	
	if [ -n $DISTRIB ]; then
		echo -e "$VERT""Your distribution is $DISTRIB"
		echo -e "$NORMAL"
	fi
}


check_dependancies() {
	dpkg -s $1 |grep installed |grep "install ok" > /dev/null	
	if [ $? -eq 1 ]; then
		echo -e "$ROUGE""The package $tested is not installed""$NORMAL"""
		sudo apt-get install $tested
	fi
}


choose_download_type(){
	echo -e ""
	echo -e "$NORMAL ""Choose your connection type to SVN"
	echo -e "\t1 --> HTTP : Connecting as usual"
	echo -e "\t2 --> HTTPS : Connecting behind a proxy (university building, company)"
	
	echo -e "\nYour choice : "
	read answer_download_type
	if [ $answer_download_type -eq 2 ]; then HTTP_TYPE="https"
	fi
}


about() {
	echo "Author : Mav"
	echo "Contact : mav@cairo-dock.fr"
	exit
}


if [ $DEBUG -ne 1 ]; then
	check_new_script
fi

LG_CMD=`echo $LANG | grep fr`
if [ -n $LG_CMD ]; then 
	LG="FR" 
fi


echo -e "$NORMAL""Script to install the SVN version of Cairo-Dock \n"
echo -e "Please select the option to install : \n"

echo -e "\t1 --> Update the SVN version installed"
echo -e "\t2 --> Install the SVN version for the first time"
echo -e "\t3 --> Install Glitz"
echo -e "\t4 --> Reinstall the current SVN version"
echo -e "\t5 --> Uninstall the SVN version"
echo -e "\t6 --> Uninstall Glitz"
echo -e "\t7 --> About"

echo -e "\nYour choice : "
read answer_menu

case $answer_menu in

	"1")
		detect_distrib
		detect_env_graph
		update
	;;
	
	"2")
		choose_download_type
		detect_distrib
		detect_env_graph
		install
	;;
	
	"3")
		choose_download_type
		detect_distrib
		detect_env_graph
		install_with_glitz
	;;
	
	"4")
		detect_distrib
		detect_env_graph
		reinstall
	;;
	
	"5")
		uninstall
	;;
	
	"6")
		uninstall_glitz
	;;
	
	"7")
		about
	;;
	
esac









