#! /bin/sh
# Delete all memories of printers and return the lp system to a pristine state
# Copyright 1987 Silicon Graphics, Inc. - All Rights Reserved

SPOOLER_BASEDIR=/var/spool/lp

#
# 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 $SPOOLER_BASEDIR ]; 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

echo " "
echo "WARNING: Running this script will remove ALL installed printers,"
echo "         delete all pending print jobs, and remove all saved settings." 
echo "         This will affect all users of this system, as well as"
echo "         all remote users of printers attached to this system."
echo "         You should use this tool ONLY if your spooler is"
echo "         irretrievably damaged, and all other measures have failed."
echo '\nAre you sure you wish to continue?  (y/n)[n] \c'
read yn
if [ \( "$yn" != "y" \) -a \( "$yn" != "Y" \) ]; then
	exit 1
fi
echo " "

# OK, now stop the scheduler
#
echo "Stopping scheduler..."
/etc/init.d/lp stop

echo "Resetting spooler special files..."
cat /dev/null > $SPOOLER_BASEDIR/pstatus
cat /dev/null > $SPOOLER_BASEDIR/qstatus
cat /dev/null > $SPOOLER_BASEDIR/outputq

# remove any default printer setting
echo "Removing default printer setting..."
cat /dev/null > $SPOOLER_BASEDIR/default

echo "Removing all installed printers and associated files..."
rm -rf $SPOOLER_BASEDIR/member/*
rm -rf $SPOOLER_BASEDIR/devices/*
rm -rf $SPOOLER_BASEDIR/request/*
rm -rf $SPOOLER_BASEDIR/interface/*
rm -rf $SPOOLER_BASEDIR/class/*
rm -rf $SPOOLER_BASEDIR/activeicons/*
rm -rf $SPOOLER_BASEDIR/settings/*

if [ -d $SPOOLER_BASEDIR/psparams ]; then

    # psparams dir only there if Impressario installed.

    rm -rf $SPOOLER_BASEDIR/psparams/*
    chown lp.sys $SPOOLER_BASEDIR/psparams
    chmod 755 $SPOOLER_BASEDIR/psparams
fi

echo "Setting ownership and permissions on spooler directories..."
chown lp.sys    \
		$SPOOLER_BASEDIR/activeicons \
		$SPOOLER_BASEDIR/class \
		$SPOOLER_BASEDIR/devices \
		$SPOOLER_BASEDIR/gui_interface \
        	$SPOOLER_BASEDIR/interface \
		$SPOOLER_BASEDIR/member \
		$SPOOLER_BASEDIR/pod \
		$SPOOLER_BASEDIR/request \
		$SPOOLER_BASEDIR/settings
chmod 755       \
		$SPOOLER_BASEDIR/activeicons \
		$SPOOLER_BASEDIR/class \
		$SPOOLER_BASEDIR/devices \
		$SPOOLER_BASEDIR/gui_interface \
        	$SPOOLER_BASEDIR/interface \
		$SPOOLER_BASEDIR/member \
		$SPOOLER_BASEDIR/pod \
		$SPOOLER_BASEDIR/request \
		$SPOOLER_BASEDIR/settings

echo "Restarting scheduler..."
/etc/init.d/lp start
echo " "
echo "The spooler system has been purged and reset."
echo "Please re-add your printers now."
