#!/bin/sh
#
# by Mohammed Arafa marafa@in-egypt.net
# version 0.3
# 10032006
#
# Description: this script will enable the desktop manager of your choice
#
# todo: 
#	the security feature - i hope u all have your firewalls up and configured correctly (tcp/udp 177)
#	run only as root
#
# Tested with the following distros
#	1. RHEL 4 (should work with fc3)
#	2. FC4 (kde 3.5.1 compatible)
#
# Changelog:
# 09032006 - 0.1 initial script xdm, kdm, gdm
# 09032006 - 0.2 added backup procedure and restore.sh
# 10032006 - 0.3 added kde 3.5.1 (fc4) section
# 11032006 - 0.4 added force option
# 11032006 - 0.5 added colours ;)
# 15032006 - 0.6 checks for type of dm installed and adds it to menu
# 		 checks if backup previously done
# 		 change chmod to 755
#		 added quit to menu
#		 added restoreX.sh to menu (only if backup was made previously)
#		 only root can run this script

#Variables
RED='\e[1;31m'
BLUE='\e[1;34m'
CYAN='\e[1;36m'
NC='\e[0m'

pause(){
echo " Press enter to continue ..."
read enter
}

menu(){
echo ""
echo " Please select one of the following:"
if [ -e /usr/bin/gdm ] 
then echo "  G - gdm - Gnome"	 
fi
if [ -e /usr/bin/kdm ] 
then echo "  K - kdm - KDE"	 
fi
if [ -e /usr/X11R6/bin/xdm ]
then echo "  X - xdm - Xorg"	 
fi
echo ""
echo "  T - Test X"
echo "  F - Force Implementation"
if [ -e ~/x.bak/restoreX.sh ]
then echo "  R - Restore Backups "
fi
echo ""
echo "  Q - Quit"
echo -n ""

read -s dm
echo ""

case $dm in 
g|G)
	doGdm
	export manager=doGdm
# 	Xaccess
;;

K|k)
	doKdm
	export manager=doKdm
# 	Xaccess
;;

X|x)
	doXdm
	export manager=doXdm
# 	Xaccess
;;

T|t)
	testX
;;

f|F)
	echo "\n${RED} Warning: This will force XWindows to restart."
	echo " 		You will lose all unsaved documents$NC "
	echo " Press Ctrl-C to cancel."
	pause
	pkill -9 X
	$manager
;;

q|Q)
	exit 0
;;

r|R)
	~/x.bak/restoreX.sh
;;

*)
	echo "\n${RED} ERROR!$NC " 
;;
esac

echo ""

}

doGdm(){
echo " Enabling gdm"
sed -i '/xdmcp/,/false/{s/false/true/;}' /etc/X11/gdm/gdm.conf #lines above from m4n on #sed on irc.freenode.org - thx :)
sed -i 's/#Port=177/Port=177/g' /etc/X11/gdm/gdm.conf
echo " Starting gdm"
gdm
pause
}

doKdm(){
echo " Enabling kdm"
sed -i '/Xdmcp/,/false/{s/false/true/;}' /etc/kde/kdm/kdmrc
if [ -e /etc/kde/kdm/kdm ]
then sed -i 's/#Port=177/Port=177/g' /etc/kde/kdm/kdmrc #rhel 4
else sed -i  's/^# \*/\*/' /usr/share/config/kdm/Xaccess #fc 4
fi
echo " Starting kdm"
kdm
pause
}

doXdm(){
echo " Enabling xdm"
sed -i '/DisplayManager.requestPort:/,/0/s/0/177/g' /etc/X11/xdm/xdm-config 
sed -i 's/^# \*/\*/' /etc/X11/xdm/Xaccess #thanks to gnubien in #sed on irc.freenode.org
xdm
pause
}

Xaccess(){
echo " Disabling Security"
# echo " Please select security policy"
# echo " 1 - None"
# echo " 2 - Selected hosts"
# echo -n ""
# read security
# case $security in 
# 1)
# 	echo " To do"
# ;;
# 
# 2)
# 	echo " To do"
# ;;
# 
# *)
# 	echo "ERROR!"
# 	exit 666
# ;;
# esac
}

testX(){ #credits for commands go to : SteveSwitzer from http://wiki.ltsp.org/twiki/bin/view/Ltsp/XDMCP
echo "Running netstat test"
echo "_______________________"
netstat -apn | grep ":177 "
echo $?

echo "Checking for dm process"
echo "_______________________"
ps -e | grep " .dm"
echo $?

echo "Checking for firewall"
echo "_______________________"
iptables -L
echo $?
}

doBackup(){
echo "Backing up ..."
if ! [ -d ~/x.bak ]
then  mkdir -p ~/x.bak
fi
cd ~/x.bak
if ! [ -e Xaccess.xdm ]
then cp /etc/X11/xdm/Xaccess Xaccess.xdm
else echo " Xaccess.xdm exists!"
fi 
if ! [ -e xdm-config ]
then cp /etc/X11/xdm/xdm-config .
else echo " xdm-config exists!"
fi
if ! [ -e kdmrc ]
then cp /etc/kde/kdm/kdmrc .
else echo " kdmrc exists!"
fi
if ! [ -e gdm.conf ]
then cp /etc/X11/gdm/gdm.conf .
else echo " gdm.conf exists!"
fi
if ! [ -e Xaccess.kdm ]
then cp /usr/share/config/kdm/Xaccess Xaccess.kdm
else echo " FXaccess.kdm  exists!"
fi

cat >>restoreX.sh<<EOF
cp Xaccess.xdm /etc/X11/xdm/Xaccess
cp xdm-config /etc/X11/xdm/xdm-config
cp kdmrc etc/kde/kdm/kdmrc
cp gdm.conf /etc/X11/gdm/gdm.conf
cp Xaccess.kdm usr/share/config/kdm/Xaccess
EOF
chmod +755 restoreX.sh
}

####


echo ""
if [ "$UID" -ne "0" ]; then # checks to see if you are root
	echo ""
	echo "*******************************************************"
	echo ""
	echo "	`basename $0` can only be run as root!"
	echo ""
	echo "*******************************************************"
	echo ""
	exit 1
else
	clear
	doBackup
	menu  #begin scripts
fi