#!/bin/sh
#Tag 0x00000700
#**************************************************************************
#*
#* 		  Copyright (c) 1993 Silicon Graphics, Inc.
#*			All Rights Reserved
#*
#*	   THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
#*
#* The copyright notice above does not evidence any actual of intended
#* publication of such source code, and is an unpublished work by Silicon
#* Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
#* the property of Silicon Graphics, Inc. Any use, duplication or
#* disclosure not specifically authorized by Silicon Graphics is strictly
#* prohibited.
#*
#* RESTRICTED RIGHTS LEGEND:
#*
#* Use, duplication or disclosure by the Government is subject to
#* restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
#* Technical Data and Computer Software clause at DFARS 52.227-7013,
#* and/or in similar or successor clauses in the FAR, DOD or NASA FAR
#* Supplement. Unpublished - rights reserved under the Copyright Laws of
#* the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
#* Shoreline Blvd., Mountain View, CA 94039-7311
#**************************************************************************
#*
#* File: rmprinter
#*
#* $Revision: 1.10 $
#*
#* Description: Shell script for removing a printer from the
#*	System V spooling system.
#*
#*	Usage: rmprinter [-x <dest printer>] [-f] printer ...
#*
#*	Normally, if print jobs are pending in the print queue, rmprinter
#*	will not remove the printer. If the -f flag is specified, the
#*	printer will be removed regardless of pending print jobs. In
#*	this case the pending print jobs will not be printed. If the
#*	-x flag is specified, any pending print jobs will be moved to
#*	<dest printer> and then the printer will be removed.
#*
#*	More than one printer can be specified for removal at one time.
#*	If an error occurs while removing one of the printers the
#*	script will continue attempting to remove all printers specified
#*	but will exit with a status indicating an error.
#*
#**************************************************************************


# Well known directories and programs

LPUTIL_DIR=/usr/lib
LPUTIL=$LPUTIL_DIR/lputil
OUT_FILT="/usr/bin/pg -s -p more... -e"
SPOOL_DIR=/var/spool/lp
LPSTAT=/usr/bin/lpstat
TRANS_DIR=/var/spool/lp/transcript
OLDLOG_DIR=/var/spool/lp/etc/log


# Global variables

FORCE=0
DEST_PNAME=""


#
# Prints the program usage string
#
PrintUsage()
{
    echo "\nUsage: rmprinter [-x <dest printer>] [-f] printer ..."
    echo "where:"
    echo "\t-x Specifies the printer to which"
    echo "\t   pending print jobs are to be moved."
    echo "\t-f Specifies that printer should be removed"
    echo "\t   regardless of pending print jobs."
    echo " "
}


#
# Checks that we are the super-user and verifies
# that the System V spooling system is present
#
CheckPermSpooler()
{
    idstr=`id`
    cid=`expr "$idstr" : '^[ 	]*uid=.*(\(.*\))[ 	]*gid='`
    if [ "$cid" != "root" ]; then
	echo "You must be logged in as root to use this program."
	exit 1;
    fi
    if [ ! -r $SPOOL_DIR ]; then
	echo "The System V spooling system is not installed on this system."
	exit 1
    fi
}


#########################################################################
#
# Main program
#

#
# Ensure that we are root and that the main System V
# spooling system directory is present
#
CheckPermSpooler

#
# Process command line arguments
#
while getopts fx: flag
do
    case $flag in
	f)  FORCE=1
	    ;;
	x)  DEST_PNAME=$OPTARG
	    ;;
	\?) PrintUsage
	    exit 1
	    ;;
    esac
done

#
# Verify we have at least one printer to remove
#
shift `expr $OPTIND - 1`
if [ $# -eq 0 ]; then
	echo "$0: No printer(s) specified for removal"
	PrintUsage
	exit 1
fi

#
# Verify that the destination printer, if any, exists
#
if [ -n "$DEST_PNAME" -a ! -d /var/spool/lp/request/$DEST_PNAME ]; then
	echo "$0: Destination printer \"$DEST_PNAME\" does not exist"
	exit 1
fi

#
# Now go through the list of printers removing each one
#
changed=0
errflag=0
for PRINTER
do
    #
    # First verify that the specified printer exits
    #
    if [ ! -d /var/spool/lp/request/$PRINTER ]; then
	echo "$0: Printer \"$PRINTER\" does not exist"
	errflag=1
	continue
    fi

    #
    # Make sure that if a destination printer is specified,
    # that it is not the same as the printer to be removed
    #
    if [ "$DEST_PNAME" = "$PRINTER" ]; then
	echo "$0: The printer being removed cannot be the destination printer"
	errflag=1
	continue
    fi

    #
    # Now check for outstanding requests. If they are found and there
    # is no destination or forced removal, do not remove the printer.
    #
    if [ "$FORCE" = 0 ]; then
	reqlist=`lpstat -o$PRINTER 2> /dev/null`
	if [ -n "$reqlist" -a -z "$DEST_PNAME" ]; then
	    echo "$0: Printer \"$PRINTER\" not removed - print jobs pending."
	    echo "\tUse -f to force removal of \"$PRINTER\" or"
	    echo "\t-x to specify a new destination for the requests."
	    echo " "
	    echo "Print queue for \"$PRINTER\":"
	    echo " "
    	    $LPSTAT -o$PRINTER | $OUT_FILT
	    errflag=1
	    continue
	fi
    fi

    #
    # Now remove old cruft. This is old transcript and log stuff
    #
    rm -rf ${TRANS_DIR}/${PRINTER}-log* ${TRANS_DIR}/${PRINTER}.opt
    rm -rf ${OLDLOG_DIR}/${PRINTER}-log*

    #
    # Now do the real work of removing the printer
    #
    $LPUTIL remove "$PRINTER" "$DEST_PNAME"
    if [ $? -ne 0 ]; then
	echo "$0: Unable to remove printer - $LPUTIL command failed"
	errflag=1
	continue
    fi
    echo "Printer \"$PRINTER\" removed"
    changed=1
done

#
# Inform the user of what has happened
#
if [ $changed -ne 0 ]; then
    echo " "
    echo "Here is your printing environment:"
    echo " "
    $LPSTAT -t | $OUT_FILT
fi

exit $errflag
