#!/bin/bash
# script for removing kdm and restoring xdm as the runlevel 5 Display Manager
# (for RedHat 5.x Linux)
INITTAB="/etc/inittab"
XSETUP="/etc/X11/xdm/Xsetup_0"


echo 'Restoring xdm as the default X Display Manager'
echo '(reverses changes made to /etc/inittab, /etc/X11/xdm/Xsetup_0 by "kdm_on")'

if ! [ -f $INITTAB ] ; then
    echo "error: $INITTAB does not exist: exiting"
    exit
fi 

if ! [ -f $XSETUP ] ; then
    echo "error: $XSETUP does not exist: exiting"
    exit
fi

if ! cp -pf $INITTAB $INITTAB.tmp ; then 
     echo 'error: "kdm_off" must be run as root: exiting' 
     exit
fi
cp -pf $XSETUP $XSETUP.tmp

grep kdm $INITTAB  > $INITTAB.tmp
if ! [ -s $INITTAB.tmp ] ; then
     echo "kdm_off: no reference to kdm found in $INITTAB: exiting"
     exit
fi

# using $XSETUP.tmp as a temporary file while processing $XINITTAB
sed  -e "/kdm/d" $INITTAB > $XSETUP.tmp
sed -e "s/^\#x:5:respawn:/x:5:respawn:/g" $XSETUP.tmp > $INITTAB.tmp

grep xdm $INITTAB.tmp  | grep x:5:respawn: > $XSETUP.tmp
if ! [ -s $XSETUP.tmp ] ; then
    echo "error: original call to xdm in $INITTAB was not commented out"
    echo "you will have to edit $INITTAB manually; exiting"
    exit
fi

# modify Xsetup_0
sed  -e "/kdmdesktop/d" $XSETUP > $XSETUP.tmp

echo "saving old  $XSETUP as $XSETUP.kdesave"
mv -f $XSETUP  $XSETUP.kdesave
mv -f $XSETUP.tmp  $XSETUP

echo "saving old  $INITTAB as $INITTAB.kdesave"
mv -f $INITTAB $INITTAB.kdesave
mv -f $INITTAB.tmp $INITTAB

echo "xdm is now restored as the runlevel-5 X Display Manager"
echo 'To restart runlevel 5 using xdm, type "telinit 3 ; telinit 5" '















