#!/bin/sh
#
# $Id: mit-switch-printer,v 1.4 2004/09/27 15:20:24 jdreed Exp jdreed $
#
# To switch between CUPS and MIT's LPRng
# since redhat-switch-printer requires a package named LPRng
# and redhat now hates LPRng
# 

case `id` in
"uid=0("*)
  ;;
*)
  echo "You are not root.  This script must be run as root."
  exit 1
  ;;
esac


CUPS_INSTALLED=`/bin/rpm -q cups`
MIT_LPRNG_INSTALLED=`/bin/rpm -q mit-lprng`
MIT_LPRNG="/usr/bin/lpr.LPRng"
CUPS="/usr/bin/lpr.cups"

PRINT_ALTERNATIVE="/etc/alternatives/print"




/sbin/chkconfig cups-lpd off > /dev/null 2>&1
if [ -h $PRINT_ALTERNATIVE ]; then
    PRINT_SUBSYS=`readlink $PRINT_ALTERNATIVE`;
    if [ "$PRINT_SUBSYS" == "$MIT_LPRNG" ]; then
	echo "You are currently using MIT LPRng"
	if [ -z "$CUPS_INSTALLED" ]; then
	    echo "You do not have CUPS installed.  Cannot switch."
	    exit 1
	fi
	echo "Switching to CUPS"
	/usr/sbin/alternatives --set print /usr/bin/lpr.cups
	/sbin/service lpd stop 2>&1 > /dev/null
	/usr/sbin/printconf-backend --force-rebuild
	/sbin/service cups start 2>&1 > /dev/null
	echo "Done"
    elif [ "$PRINT_SUBSYS" == "$CUPS" ]; then
	echo "You are currently using CUPS"
	if [ -z "$MIT_LPRNG_INSTALLED" ]; then
	    echo "You do not have MIT LPRng installed.  Cannot switch."
	    exit 1
	fi
	echo "Switching to MIT LPRng"
	/bin/rm -f /etc/printcap
	/usr/sbin/alternatives --set print /usr/bin/lpr.LPRng
	/sbin/service cups stop 2>&1 > /dev/null
	/usr/sbin/printconf-backend --force-rebuild
	# Even if we have no printers define, users should still be able to
	# use LPRng
	if [ ! -e /etc/printcap ]; then
	    touch /etc/printcap
	fi
	/sbin/service lpd start 2>&1 > /dev/null
	echo "Done"
    else
	echo "You are using an unknown printing system:"
	echo $PRINT_SUBSYS
    fi
else
    echo "You are not using /etc/alternatives"
fi
