#! /bin/bash

# Display Manager Switching Tool
# (c) 1999-2008 Red Hat, Inc.

if [ $UID != 0 ]; then
  echo "You must be root to use this utility"
  exit 1
fi

if [ -z "$1" ]; then
  echo "Please specify one of either GDM, KDM or XDM."
  exit 1
fi

error() {
  echo "ERROR: $1 is not installed on your machine! to install $1, please type"
  echo "yum install $2"
  exit 1
}

DATADIR="/usr/share/system-switch-displaymanager/"

DISPLAYMANAGER=`echo $1 |tr a-z A-Z`
case $DISPLAYMANAGER in
  KDM|KDE)
    DM=kdm
    DISPLAYMANAGER=KDE
    [ ! -x /usr/bin/$DM ] && error KDM kdebase-workspace
    ;;
  GDM|GNOME)
    DM=gdm
    DISPLAYMANAGER=GNOME
    [ ! -x /usr/sbin/gdm ] && error GDM gdm
    ;;
  XDM)
    DM=xdm
    DISPLAYMANAGER=XDM
    [ ! -x /usr/bin/xdm ] && error XDM xorg-x11-xdm
    ;;
  SYSTEM)
    DM=""
    DISPLAYMANAGER=""
    ;;
  WDM)
    DISPLAYMANAGER=WDM
    [ ! -x /usr/bin/wdm ] && error WDM wdm
    DM=wdm
    ;;
  *)
    echo "The graphical display manager $1 is not supported yet."
    exit 1
    ;;
esac

# set Display Manager
if [ -f /etc/sysconfig/desktop ] ; then
  /bin/cp -f /etc/sysconfig/desktop /etc/sysconfig/desktop.save
  /bin/cat /etc/sysconfig/desktop.save | grep -vi DISPLAYMANAGER > /etc/sysconfig/desktop
else
  > /etc/sysconfig/desktop
fi

if [ -n "$DM" ] ; then
  echo "DISPLAYMANAGER=$DISPLAYMANAGER" >> /etc/sysconfig/desktop
fi
echo "Your default graphical display manager has successfully been switched."

exit 0
