#! /bin/sh
#Tag 0x00000700
#**************************************************************************
#*
#* 		  Copyright (c) 1992 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: mkBSDpr
#*
#* $Revision: 1.11 $
#*
#* Description: A script to install a System V printer that forwards
#*	print jobs to a BSD printer. The BSD printer must already be
#*	installed on the system before this script is executed.
#*
#*	Usage: mkBSDpr lp_printer_name bsd_printer_name
#*
#*	Where lp_printer_name is the local name you want to assign the
#*	system V printer and bsd_printer_name is the lcoal name of the
#*	BSD printer to which jobs should be forwarded.
#*
#**************************************************************************

usage() {
	echo "usage: mkBSDpr [-v] [-l] lp_printer_name bsd_printer_name"
	echo "-h			Display the usage message"
	echo "-v			Keep a verbose activity log"
	echo "-l			Make a unique log file for the printer"
	echo "lp_printer_name		System V local printer name"
	echo "bsd_printer_name 		BSD local printer name"
	}

if [ ! -r /var/spool/lp/model/bsdface ]; then
	echo ""
	echo "ERROR: Before using this unsupported gift program,"
	echo "       you must manually copy the model file 'bsdface'"
	echo "       from the Impressario gifts directory."
	echo ""
        echo " Please be aware that this is an unsupported gift,"
	echo " provided for your convenience, but not tested or supported"
	echo " by Silicon Graphics."
	echo ""
	echo " Now that you've heard the disclaimer, type this to continue:"
	echo ""
	echo " su"
	echo " cp /usr/impressario/gifts/models/bsdface /var/spool/lp/model"
	echo " mkbsdpr $*"
	echo ""
	exit 1;
fi

cid=`/usr/bin/id | /usr/bin/nawk '{print substr($1,index($1,"("))}'`;
if [ "$cid" != "(root)" ]; then
        echo "You must be logged in as root to use this program."
        exit 1;
fi
if [ ! -r /var/spool/lp ]; then
        echo "The System V spooling system is not installed on this system."
        exit 1
fi


# Check to see if lpr is installed on this machine. Printcap is the heart
# of BSD spooler, check if it exists.
if [ ! -f /etc/printcap ]
then
	echo "The BSD spooling system is not installed on this machine."
	exit 1
fi

while getopts vl flag
do
	case $flag in
		v)
			# Turn on verbose flag in printer driver.
			# This turns the verbose flag on by default in the
			# printer driver.  The verbose flag may be controlled
			# in each print job by use of the -overb/-onoverb
			# options to the lp command.
			VFLAG="VERBOSE=1"
			;;
		l)
			# Use a separate log file.
			SPECIALLOG=1
			;;
		\? | h)
			usage
			exit 1
			;;
	esac
done

shift `expr $OPTIND - 1`

if [ $# != 2 ] ; then
	echo "$0: Insufficient number of arguments"
	usage
	exit 1
fi

LP_PRINTER=$1
HOSTNAME=`hostname`
BSD_PRINTER=$2
BSD_DUMMY_DEV=/var/spool/lp/bsd_printer
BSD_INTERFACE=bsdface

# The standard place to have sd (spool dir) is /var/spool/<printer_name>.

# Test whether another SVR3 printer is installed under that name:
if [ -s /var/spool/lp/member/$LP_PRINTER ]; then
	echo "ERROR: An 'lp' printer called '$LP_PRINTER' is already installed."
	echo "       Please choose another name and try again."
	echo "       (Type 'lpstat -a' for a list of installed printers.)"
	exit 1;
fi

# Let's make sure that printer is configured in BSD spooling system.
MatchingPrinters=`/usr/bin/grep $BSD_PRINTER /etc/printcap`
if [ -z "$MatchingPrinters" ]; then
        echo "ERROR: No printer named '$BSD_PRINTER' appear in /etc/printcap."
	echo "       You must first install and verify the BSD lpr printer"
	echo "       before you can install an SVR3 lp printer queue."
        exit 1;
fi

# Make a unique log file for this printer in /var/spool/lp/etc/log
# if requested with the -l option.  The default is to use the communal
# log file /var/spool/lp/log.

if [ "$SPECIALLOG" != "" ]
then
	PDIR=/var/spool/lp/etc/log
	if test ! -d $PDIR ; then
		mkdir $PDIR
	fi

	LOGFILE=${PDIR}/${LP_PRINTER}-log
	rm -rf ${LOGFILE}*

	cp /dev/null ${LOGFILE}
	chown lp $PDIR ${LOGFILE}
	chgrp bin $PDIR ${LOGFILE}
	chmod 664 ${LOGFILE}
	chmod 775 $PDIR
	LFLAG="LOGFILE=$LOGFILE"
	echo "Log file for printer \"$PRINTER\" will be $LOGFILE"
fi

# Let us create a dummy device file so that lobpod will not think it
# is a printer configured on the remote system.
#
if [ ! -r $BSD_DUMMY_DEV ]
then
	dev_info=`/bin/ls -l /dev/null`
	major_dev=`echo $dev_info|/usr/bin/awk \
				'{print substr($5,$5,index($5,",")-1)}'`
	minor_dev=`echo $dev_info|/usr/bin/awk '{print $6}'`
	/etc/mknod $BSD_DUMMY_DEV c $major_dev $minor_dev
	chmod 664 $BSD_DUMMY_DEV
	chown lp.sys $BSD_DUMMY_DEV
fi

if /usr/lib/lputil add $BSD_DUMMY_DEV $BSD_INTERFACE "$LP_PRINTER" \
					HOSTNAME=$HOSTNAME \
					HOSTPRINTER=$BSD_PRINTER \
					$VFLAG \
					$LFLAG
then
	# report what we have done
	echo ""
	echo "Printer '$LP_PRINTER' installed."
	echo ""
	echo "This is your current printing environment:"
	lpstat -t | more
	exit 0
else
	echo "ERROR: Failed to add new printer."
	exit 1
fi
