#!/bin/sh
#############################################
##
## Ricoh UNIX Printing Solution
## Uninstallation Program
##
## Version 6.1  12/22/2003
## Copyright 1999-2003
## Ricoh Engineering
##
#############################################
#############################################
##
## For debugging, uncomment below: set -x
##
#############################################
#set -x

#############################################
##
## Create myecho command based on UNIX version
## For BSD UNIX myecho="echo -e",
## For SYS V UNIX myecho="echo"
##
#############################################

myecho="echo"
test X`echo -e` = X && myecho="echo -e"

#############################################
##
## Create myecho command based on UNIX version
## For BSD UNIX myecho="echo -e",
## For SYS V UNIX myecho="echo"
##
#############################################

myecho="echo"
test X`echo -e` = X && myecho="echo -e"

#############################################
##
## Check if user is logged in as root
##
#############################################

umask 022

if test "`id|sed 's/(.*$//'`" != "uid=0"
then
     $myecho "You must have root privileges to run this script. Exiting ...."
     exit 1
fi

#############################################
##
## Message to user
##
#############################################

$myecho "\nIf you want to terminate execution of this program,\
	\npress CTRL+C or Delete key at any time."

#############################################
##
## Initialize variables
##
#############################################

PRINTERS=
PRINTER_NAME=
Version=
OS_VERSION=
OS_NAME=
OS_TYPE=
exit_code=0

#############################################
##
## Get Printer to be removed
##
#############################################

if [ -n "$1" ]
then
	PRINTERS=$*
else
	$myecho "Please enter the printer to be removed: \c"
	read PRINTERS
fi

if [ -z "$PRINTERS" ]
then
	$myecho "No printer name specified. Exiting"
	exit 1
fi

#############################################
##
## Detect / select OS Version
##
#############################################

OS_NAME=`uname -s`

case $OS_NAME in
SunOS)
	OS_VERSION=`uname -r`
	OS_TYPE="SYSV"
	OS_VERSION="Solaris"
	Version=`uname -sr`
	;;
HP-UX)
	OS_VERSION="HPUX"
	OS_TYPE="SYSV"
	Version="HP-UX `uname -r`"
	;;
AIX)
	OS_VERSION="AIX43"
	OS_TYPE="SYSV"
	Version="IBM AIX `uname -v`.`uname -r`"
	;;
Linux)
	OS_VERSION="LINUX"
	OS_TYPE="BSD"
	Version="LINUX"
	;;
SCO_SV)
	OS_VERSION="SCO"
	OS_TYPE="BSD"
	Version="SCO OpenServer `uname -v`"
	;;
FreeBSD)
	OS_VERSION="FreeBSD"
	OS_TYPE="BSD"
	Version="FreeBSD `uname -r`"
	;;
*)
	;;
esac

#if [ -n "$Version" ];
#then
#
#	$myecho "\nYour system appears to be running $Version.\nIs this correct ? [y/n] \c"
#	read OS_CONFIRM
#
#fi

if [ "$OS_CONFIRM" = N -o "$OS_CONFIRM" = n -o -z "$Version" ];
then

cat << SelectVersion

Please select your UNIX version.

  1. Sun Solaris 2.4 or 2.5
  2. Sun Solaris 2.6, 7, 8, 9
  3. HP-UX 10.x or 11.x
  4. IBM AIX 4.3
  5. RedHat Linux 7.x
  6. SCO OpenServer 5.06 or Sco OpenServer 5.x
  7. FreeBSD
  8. Other

SelectVersion

$myecho "Select <1-8>: \c"
read Version

case $Version in

1) OS_VERSION="Solaris2.5" ;;
2) OS_VERSION="Solaris2.6" ;;
3) OS_VERSION="HPUX" ;;
4) OS_VERSION="AIX43" ;;
5) OS_VERSION="LINUX" ;;
6) OS_VERSION="SCO" ;;
7) OS_VERSION="FreeBSD" ;;
*) $myecho "Unsupported UNIX version. Exiting ...."
	exit 1 ;;
esac
fi

#############################################
##
## Delete printer entry
##
#############################################

$myecho "\nDeleting printer queue, please stand by..."

case $OS_VERSION in
HPUX)
	/usr/lib/lpshut
	;;
esac

for PRINTER_NAME in $PRINTERS
do

error_code=0

case $OS_VERSION in

Solaris)
	if grep -w "$PRINTER_NAME:" /etc/printers.conf > /dev/null
	then
	    /usr/lib/lpadmin -x $PRINTER_NAME
	    error_code=`expr $error_code \| $?`
	else
	    $myecho "\"$PRINTER_NAME\" not found"
	    error_code=1
	fi

	if grep -w "rp_$PRINTER_NAME:" /etc/printers.conf > /dev/null
	then
	    /usr/lib/lpadmin -x rp_$PRINTER_NAME
	    error_code=`expr $error_code \| $?`
	fi
	;;

HPUX)

	if [ -f "/etc/lp/interface/$PRINTER_NAME" ]
	then
		/usr/bin/cancel $PRINTER_NAME -e
		/usr/lib/lpadmin -x$PRINTER_NAME
		error_code=`expr $error_code \| $?`
	else
		$myecho "\"$PRINTER_NAME\" not found"
		error_code=1
	fi
	if [ -f "/etc/lp/interface/rp_$PRINTER_NAME" ]
	then
		/usr/bin/cancel rp_$PRINTER_NAME -e
		/usr/lib/lpadmin -xrp_$PRINTER_NAME
		error_code=`expr $error_code \| $?`
	fi
	;;

AIX43)
	DEV_NAME=${PRINTER_NAME}.dev
	RMT_PRINTER_NAME=${PRINTER_NAME}.rmt
	RMT_DEV_NAME=${DEV_NAME}.rmt

	if grep -w "$RMT_PRINTER_NAME:" /etc/qconfig > /dev/null
	then
	    /usr/sbin/piomisc_base rmpq_generic "$RMT_PRINTER_NAME:$RMT_DEV_NAME"
	    error_code=`expr $error_code \| $?`
	fi

	if grep -w "$PRINTER_NAME:" /etc/qconfig > /dev/null
	then
	    /usr/sbin/piomisc_base rmpq_generic "$PRINTER_NAME:$DEV_NAME"
	    error_code=`expr $error_code \| $?`
	else
	    $myecho "\"$PRINTER_NAME\" not found"
	    error_code=1
	fi
	;;

LINUX)
#	if [ -e /etc/printcap.local ]
#	then
#		PRINTCAP=/etc/printcap.local
#	else
		PRINTCAP=/etc/printcap
#	fi

	if test ! -f $PRINTCAP
	then
		$myecho 'Could not find "printcap" file'
		$myecho "please enter the full path of printcap file name: \c"
		read $PRINTCAP
	fi

	if ! grep "^$PRINTER_NAME[|:]\{0,1\}[\\]\{0,1\}$" $PRINTCAP > /dev/null
	then
		$myecho "\"$PRINTER_NAME\" not found"
		error_code=1
		continue
	fi

	cp -f $PRINTCAP $PRINTCAP.old
	awk -vPRINTER=`grep "^$PRINTER_NAME[|:]\{0,1\}[\\]\{0,1\}$" $PRINTCAP.old` '
		BEGIN {i=0;j=0}
		 index($1,PRINTER) == 1 {i=1;next}
		 /^[ 	]*:/ {j=1}
		 { if(i==0) print $0;
		 else if(j==0) {print $0;i=0;}
		 j=0 }' $PRINTCAP.old > $PRINTCAP
	if [ $? -ne 0 ]
	then
		mv $PRINTCAP.old $PRINTCAP
		error_code=1
		$myecho "Error deleting \"$PRINTER_NAME\" entry."
		continue
	fi

	SPOOLDIR=/var/spool/lpd/$PRINTER_NAME

	if test -d $SPOOLDIR
	then
		rm -rf $SPOOLDIR
	fi

	;;

SCO)

	PRINTCAP=/etc/printcap

	if test ! -f $PRINTCAP
	then
		$myecho 'Could not find "printcap" file'
		$myecho "please enter the full path of printcap file name: \c"
		read $PRINTCAP
	fi

	if grep "^$PRINTER_NAME[|:]\{0,1\}[\\]\{0,1\}$" $PRINTCAP > /dev/null
	then
		QNAME=$PRINTER_NAME
	else
		if grep "^rp_$PRINTER_NAME[|:]\{0,1\}[\\]\{0,1\}$" $PRINTCAP > /dev/null
		then
			QNAME=rp_$PRINTER_NAME
		else
			$myecho "\"$PRINTER_NAME\" not found"
			error_code=1
			continue
		fi
	fi

	cp -f $PRINTCAP $PRINTCAP.old
	awk -vPRINTER=`grep "^$QNAME[|:]\{0,1\}[\\]\{0,1\}$" $PRINTCAP.old` '
		BEGIN {i=0;j=0}
		 index($1,PRINTER) == 1 {i=1;next}
		 /^[ 	]*:/ {j=1}
		 { if(i==0) print $0;
		 else if(j==0) {print $0;i=0;}
		 j=0 }' $PRINTCAP.old > $PRINTCAP
	if [ $? -ne 0 ]
	then
		mv $PRINTCAP.old $PRINTCAP
		error_code=1
		$myecho "Error deleting \"$PRINTER_NAME\" entry."
		continue
	fi

	SPOOLDIR=/var/spool/lpd/$PRINTER_NAME

	if test -d $SPOOLDIR
	then
		rm -rf $SPOOLDIR
	fi

	if [ "$QNAME" != "$PRINTER_NAME" ]
	then
		/usr/bin/cancel $PRINTER_NAME
		/usr/bin/disable $PRINTER_NAME
		/usr/lib/lpadmin -x$PRINTER_NAME
		error_code=`expr $error_code \| $?`
	fi

	;;

FreeBSD)

	PRINTCAP=/etc/printcap

	if test ! -f $PRINTCAP
	then
		$myecho 'Could not find "printcap" file'
		$myecho "please enter the full path of printcap file name: \c"
		read $PRINTCAP
	fi

	if ! grep "^$PRINTER_NAME[|:]\{0,1\}[\\]\{0,1\}$" $PRINTCAP > /dev/null
	then
		$myecho "\"$PRINTER_NAME\" not found"
		error_code=1
		continue
	fi

	cp -f $PRINTCAP $PRINTCAP.old
	awk -vPRINTER=`grep "^$PRINTER_NAME[|:]\{0,1\}[\\]\{0,1\}$" $PRINTCAP.old` '
		BEGIN {i=0;j=0}
		 index($1,PRINTER) == 1 {i=1;next}
		 /^[ 	]*:/ {j=1}
		 { if(i==0) print $0;
		 else if(j==0) {print $0;i=0;}
		 j=0 }' $PRINTCAP.old > $PRINTCAP
	if [ $? -ne 0 ]
	then
		mv $PRINTCAP.old $PRINTCAP
		error_code=1
		$myecho "Error deleting \"$PRINTER_NAME\" entry."
		continue
	fi

	SPOOLDIR=/var/spool/lpd/$PRINTER_NAME

	if test -d $SPOOLDIR
	then
		rm -rf $SPOOLDIR
	fi

#	rm $LNXFPI
	;;

*)
	$myecho "Error deleting a printer entry. Exiting ...."
	exit 1
	;;
esac

#############################################
##
## Check if the printer uninstallation was processed successfuly
##
#############################################

if test $error_code = 0; then
	$myecho "Printer \"$PRINTER_NAME\" removed successfully."
else
	$myecho "Error deleting \"$PRINTER_NAME\" entry."
	exit_code=1
fi

done

case $OS_VERSION in
HPUX)

	/usr/lib/lpsched
	;;
LINUX)
	if [ -x "/etc/rc.d/lpd" ]
	then
		/etc/rc.d/lpd restart
	elif [ -x "/etc/rc.d/init.d/lpd" ]
	then
		/etc/rc.d/init.d/lpd restart
	else
		$myecho "\nPlease make sure to restart lpd daemon, otherwise it will not print."
	fi
	;;
FreeBSD)
	killall lpd
	if grep 'lpd_enable="YES"' /etc/rc.conf > /dev/null
	then
		/usr/sbin/lpd
	else
		/usr/local/sbin/lpd
	fi
	;;
esac

exit $exit_code
# end of uninstallation script
