#!/bin/sh
# 
# MAKE SURE A PRINTCAP FILE CALLED printcap.lcs is in the directory
# from which this script is run
#
# script to install a generic LCS printcap file and
# create the necessary spool and related directories
# for BSD type printing
#
# SHOULD NOT BE RUN ON PRINT SERVERS - machines that
# spool network printers or machines that have a printer directly
# attached
#
# it saves a copy of the old printcap file to
# /etc/printcap.old.$$
#
# basic steps:
# 1) disable printing
# 2) save old printcap
# 3) copy in new printcap
# 4) create spool directories for each printer
# 5) set permissions for new printers
# 6) restart printing
# 7) tell user about new printers
#
# If this script is run on a machine with an existing printcap,
# any custom modifications will be overwritten.
# It is mainly intended as an aid to setup new machines

ID=`whoami`
OS=`uname -r | cut -c1-5`
NODE=`uname -n`
#
# SET THESE FOR YOUR SYSTEM
#
SPOOLDIR=/usr/spool
PRINTCAP=/etc/printcap
LPC=/usr/etc/lpc
LPSTAT=/bin/lpstat
CP=/bin/cp
CHOWN=/etc/chown
CHMOD=/bin/chmod
RM=/bin/rm
#
# DON'T GO HERE
#
if [ $ID != root ]; then
   echo "You must be root to properly execute $0"
   echo "Goobye...."
   exit 1
fi

if [ $OS != 4.1.1 -a $OS != 4.1.2 -a $OS != 4.1.3 -a $OS != 4.1.3_U1 -a $OS != 4.1.4 ];  then
    echo "This script should only be used on SunOS4.1.{1,2,3,3_U1,4}."
    echo "Goodbye..."
    exit 1
fi
	
echo "This script will replace the current printcap file on"
echo "$NODE. IT SHOULD NOT BE RUN ON LAB PRINT SERVERS"
echo ""
echo "Make sure a file called printcap.lcs exists in the directory from which"
echo "this script is being run. You can get printcap.lcs from"
echo "ftp://ftp.lcs.mit.edu/LCS/printcap.lcs"
echo ""
echo -n "Do you wish to continue? (y|n) "
read answer

if [ "$answer" != "y" ];then
	echo "Goodbye..."
	exit 1
fi

echo "Diabling current printers and saving old printcap..."
$LPC disable all  > /dev/null
$LPC down all > /dev/null
$CP $PRINTCAP $PRINTCAP.$$

#
# now create the new spool directories. 
#

echo "Creating spool directories and status files."

for PRINTER in amie celery charmin director gi ham kuzu oval paperhaus pastrami pulp salami scoot spam turkey wild colorpaper colortrans  bear lj4 alt4 le-monde phaserpaper phasertrans phaser540
do
	if [ -d $SPOOLDIR/$PRINTER ];then
		echo "$SPOOLDIR/$PRINTER already exists. Not creating directory"
		/bin/touch $SPOOLDIR/$PRINTER/status
		$CHMOD 775 $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status
		$CHMOD g+s $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status
		$CHOWN root.daemon $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status 
	else
		echo "Creating $SPOOLDIR/$PRINTER"
		/bin/mkdir $SPOOLDIR/$PRINTER
		/bin/touch $SPOOLDIR/$PRINTER/status
                $CHMOD 775 $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status
                $CHMOD g+s $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status
                $CHOWN root.daemon $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status 
	fi
done

for PRINTER in shu-12pn shu-12pv shu-trans shu-tv shu set-paper set-6pv set-fpn set-12pn set-12pv set-trans set-tv set mut mut-paper mut-trans shu-paper shu-6pv shu-fpn  
do
        if [ -d $SPOOLDIR/$PRINTER ];then
                echo "$SPOOLDIR/$PRINTER already exists. Not creating directory"
                /bin/touch $SPOOLDIR/$PRINTER/status
                /bin/touch $SPOOLDIR/$PRINTER/log
                $CHMOD 775 $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status
                $CHMOD 775 $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/log
                $CHMOD g+s $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status
                $CHMOD g+s $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/log
                $CHOWN root.daemon $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status 
                $CHOWN root.daemon $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/log 
        else
		echo "Creating $SPOOLDIR/$PRINTER"
                /bin/mkdir $SPOOLDIR/$PRINTER
                /bin/touch $SPOOLDIR/$PRINTER/status
                /bin/touch $SPOOLDIR/$PRINTER/log
                $CHMOD 775 $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status
                $CHMOD 775 $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/log
                $CHMOD g+s $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status
                $CHMOD g+s $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/log
                $CHOWN root.daemon $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/status
                $CHOWN root.daemon $SPOOLDIR/$PRINTER $SPOOLDIR/$PRINTER/log
        fi
done

#
# remove the old and create the new printcap
#
$RM $PRINTCAP
$CP ./printcap.lcs $PRINTCAP
echo "Enabling new printers..."
$LPC enable all > /dev/null
$LPC start all > /dev/null

echo "The following printers should now be available:"
$LPSTAT -a | /bin/grep ":" | /bin/cut -f1 -d:
