#!/bin/sh

case `id -u` in
0)
	;;
*)
	echo "gnome-xgl-switch must be run as root" 1>&2
	exit 1
	;;
esac

case $1 in

-e|--enable|--enable-xgl)
	case $2 in
	
	-a|--auto)
		xgl_opts="`/usr/bin/gnome-xgl-info | grep accel | cut -d " " -f 2-`"
    		echo $xgl_opts > /etc/sysconfig/xgl/xgl_options
		;;
		
	-s|--set)
		echo "$3" > /etc/sysconfig/xgl/xgl_options
		;;
	*)
		echo "--auto or --set [xgl options] command requiered !!!"
		exit 1
		;;
	esac
    printf "%s" "`cat /etc/sysconfig/xgl/custom.conf.xgl.begin`" > /etc/X11/gdm/custom.conf
    printf "%s\n" " `cat /etc/sysconfig/xgl/xgl_options`" >> /etc/X11/gdm/custom.conf
    printf "%s\n" "`cat /etc/sysconfig/xgl/custom.conf.xgl.end`" >> /etc/X11/gdm/custom.conf
    echo "enable" > /etc/sysconfig/xgl/status
    ;;
   
-d|--disable|--disable-xgl)
		cp -f /etc/sysconfig/xgl/custom.conf.default /etc/X11/gdm/custom.conf
    	echo "disable" > /etc/sysconfig/xgl/status
    	;;
*)
    	echo "Usage: gnome-xgl-switch --enable-xgl --auto" 1>&2
    	echo "       gnome-xgl-switch --enable-xgl --set [xgl options]" 1>&2
    	echo "       gnome-xgl-switch --disable-xgl" 1>&2
    	exit 1
    	;;
esac

read -p "Do you want to apply the modifications now? (y/n) : " -n 1 ret
if [ $ret = "y" ] || [ $ret = "Y" ]; then
	printf "\n\n  "
	read -p "Attention !!! save you current documents before press any key to this terminal, GDM must be restarted for applying this settings... " -n 1
	/usr/sbin/gdm-restart || exit 1
else
	printf "\n\nThe new configuration will be applied to the next starting of gnome!\n\n"
fi

exit 0
