#! /bin/bash

# Display Manager Switching Tool
# (c) 1999-2012 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, XDM, WDM or LIGHTDM."
  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 kdm
    ;;
  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="kdm"
    DISPLAYMANAGER="KDE"
    ;;
  WDM)
    DISPLAYMANAGER=WDM
    [ ! -x /usr/bin/wdm ] && error WDM wdm
    DM=wdm
    ;;
  LIGHTDM)
    DISPLAYMANAGER=LIGHTDM
    [ ! -x /usr/sbin/lightdm ] && error LIGHTDM lightdm
    DM=lightdm
    ;;
  LXDM)
    DISPLAYMANAGER=LXDM
    [ ! -x /usr/sbin/lxdm ] && error LXDM lxdm
    DM=lxdm
    ;;
  SDDM)
    DISPLAYMANAGER=SDDM
    [ ! -x /usr/sbin/sddm ] && error SDDM sddm
    DM=sddm
    ;;

  *)
    echo "The graphical display manager $1 is not supported yet."
    exit 1
    ;;
esac

# set Display Manager
rm -f /etc/systemd/system/display-manager.service
systemctl enable $DM.service

echo "Your default graphical display manager has successfully been switched."
exit 0
