#!/bin/sh
#############################################
#
# RICOH UNIX Printing Solution
# Uninstallation Program
# COPYRIGHT (C) 1999-2011 RICOH COMPANY,LTD.
# All rights reserved
# Version 31.0.0
#
#############################################
#############################################
##
## 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, 10
  3. HP-UX 10.x, 11.x, 11iv2, 11iv3
  4. IBM AIX V4.3, 5L V5.1, V5.2, V5.3, V6.1, V7.1
  5. RedHat Linux 6.x, 7.x, 8.x, 9, Enterprise V4, V5, V6
  6. SCO OpenServer 5.0.6, 5.0.7, 6.0
  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)

        PRINTCAP=/etc/printers.conf
	if grep "^$PRINTER_NAME[|:]\{0,1\}[\\]\{0,1\}$" $PRINTCAP > /dev/null
        then
	        /usr/lib/lpadmin -x $PRINTER_NAME
	###################################
	#
	# Remove default setting 10/10/2003
	#
	###################################
		SPOOLDIR=/var/spool/lpd/$PRINTER_NAME
		if test -d $SPOOLDIR
		then
	      		 /usr/bin/rm -r $SPOOLDIR
		fi

		error_code=`expr $error_code \| $?`
        else
                $myecho "\"$PRINTER_NAME\" not found"
                error_code=1       
        fi
#	/usr/lib/lpadmin -x rp_$PRINTER_NAME
#	error_code=`expr $error_code \| $?`
	;;

HPUX)

	if [ -f "/etc/lp/interface/$PRINTER_NAME" ]
	then
		/usr/bin/cancel $PRINTER_NAME -e
		/usr/lib/lpadmin -x$PRINTER_NAME
	###################################
	#
	# Remove default setting 10/10/2003
	#
	###################################
	SPOOLDIR=/var/spool/lpd/$PRINTER_NAME
	if test -d $SPOOLDIR
	then
	       /usr/bin/rm -r $SPOOLDIR
	fi

		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

	###################################
	#
	# Remove default setting 10/10/2003
	#
	###################################
	SPOOLDIR=/var/spool/lpd/$PRINTER_NAME
	if test -d $SPOOLDIR
	then
		rm -r $SPOOLDIR
	fi

	error_code=`expr $error_code \| $?`
#	if [ "$DEVICE_OPTION" = Y ]; then
	/usr/sbin/piomisc_base rmpq_generic "$PRINTER_NAME:$DEV_NAME"
#	else
#	/usr/lib/lpd/pio/etc/piomisc_ext rmpq_other '$PRINTER_NAME:@$HOSTNAME' 'remote'
#	fi
	error_code=`expr $error_code \| $?`

	;;

LINUX)
	if [ -e /etc/printcap.local ]
	then
		PRINTCAP=/etc/printcap.local
	else
		PRINTCAP=/etc/printcap
	fi
#######################################
#
# If it is RedHat Enterprise version
# then set PRINTCAP=/etc/printcap
# update 06/04/2004
#
########################################
	if uname -r | grep EL > /dev/null
	then
		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
######################################
#
# Update at 05/12/2004 remove Lunix filter
#
######################################
		rm -f /usr/share/linux_filters/$PRINTER_NAME.filter
	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

    SPOOLDIR=/var/spool/lpd/rp_$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
