#!/bin/sh
##############################################################################
#                                                                            #
#            Copyright (C) 1985, 1986, 1987, 1990, Alias Research, Inc.      #
#                                                                            #
#    These coded instructions,  statements and  computer programs contain    #
#    unpublished information proprietary to Alias Research, Inc.  and are    #
#    protected by the Canadian and US Federal copyright law. They may not    #
#    be disclosed to third parties  or copied  or duplicated, in whole or    #
#    in part,  without the prior written consent of Alias Research,  Inc.    #
#                                                                            #
# Unpublished-rights reserved under the Copyright Laws of the United States. #
#                                                                            #
##############################################################################
# 	$Source: /v/alias6.0/src/Utility/etc/RCS/install_vy5000.sh_sgi,v $ $Revision: 6.0 $
# 	$Author: software $ $Date: 94/03/23 13:29:34 $
 
#
#      filename: install_vy5000.sh
#      purpose : execute this script file as root to initially install
#                the lp support for the Hitachi VY-5000 printer
#
#      SUPER-IMPORTANT USAGE NOTE: when you are logged in as root,
#                                  it is quite possible that your
#                                  current directory is *NOT* in your
#                                  search path.  Thus, to execute this
#                                  program as root, type ./install_vy5000
#
#      NOTE #1: since there was problems giving the line printer spooler
#               the actual SCSI device, we now tell the lp subsystem that
#               the device is /dev/null and insert the proper device into
#               the vy5000_face interface script.
#
#      NOTE #2: this script can also be used to remove or update the
#				printer spooler device
#

#
# if there is a clash with the name of hitachi, the hitachiname
# variable can be changed to something more unique. This could happen if the
# customer already has another printer named hitachi
#
hitachiname=hitachi

tempinterface=/tmp/hitachiinst.$$

cleanup()
{
	/bin/rm -f $tempinterface

	#
	# make sure scheduler is running again
	#
	/usr/lib/lpsched > /dev/null 2>&1
}

goodbye()
{
	cleanup
	exit $1
}

remove()
{
	echo removing $1 ... \\c
	if /usr/lib/lpadmin -x$1 ; then
		echo done
	else
		echo Could not remove printer named $1
		goodbye 1
	fi
}

trap "cleanup; exit 1" 1 2 3 15 

/bin/cat <<- EOF

Whenever you are asked questions in this program, the default action is
contained in square brackets. The default action is carried out if you
just hit the return key in response to the question.

NOTE: This program is only used to install the line printer drivers for
the Hitachi VY-5000 Sublimate Dye Thermal Printer with SCSI interface

EOF

echo Do you wish to continue with this program? [yes] \\c
read answer
if [ "$answer" -a "$answer" != "yes" ]; then
	exit 0
fi

#
# start and stop lp scheduler
#
echo Trying to stop the line printer scheduler ...
/usr/lib/lpsched	# this doesn't hurt if it already running
sleep 2				# wait for scheduler to get started
/usr/lib/lpshut 
if [ $? -ne 0 ]; then
	echo "couldn't stop the line printer scheduler - are you logged in as root?"
	goodbye 1
fi

#
# check if lp already thinks there is a hitachi device here
# - since the bourne shell executes loops in subshells any exit within the
#   while loop will only exit out of the loop :-( therefore I check the
#   status at the end of the loop and exit if non-zero
#
/usr/bin/lpstat -v | sed -ne "/${hitachiname}/p" | while read line; do
	#
	# check if lp device named $hitachiname is already there
	#
	dev=`/bin/expr "$line" : "device for ${hitachiname}: \(.*\)"`
	if [ "$dev" ]; then
		if [ "$dev" = "/dev/null" ]; then
			/bin/cat <<- EOF

			The Alias Hitachi VY-5000 spooler already appears to be installed.
			If you are updating the printer spooler you should remove it.

			EOF
		else
			/bin/cat <<- EOF

			There is already a printer named "$hitachiname" that uses ${dev}.
			Removing this printer may cause something else to break.
			If you are in doubt please answer no and ask the system
			administrator about this or call Alias Assist.

			EOF
		fi

		echo "Do you want to remove it? [no] \c"
		read answer < /dev/tty
		if [ "$answer" = "yes" ]; then
			remove $hitachiname
		else
			goodbye 1
		fi
	fi
done
if [ $? -ne 0 ]; then
	exit 1
fi

#
# ask which SCSI address is being used by the printer
#
if [ -x /dev/sc1d1l0 ]; then
	/bin/cat <<- EOF

You have to enter which SCSI bus the Hitachi VY-5000 is attached to. If
the workstation and printer are connected properly and the workstation
has been rebooted after they were connected, the "hinv" command should
indicate which SCSI bus the printer is attached to.  The default is
SCSI bus '0'.

EOF

	echo Enter the SCSI bus number which is being used by the VY-5000? [0] \\c
	read bus
	if [ "$bus" ]; then
		if [ `/bin/expr "$bus" : '[0-9][0-9]*'` = 0 ]; then
			echo Numbers are the only valid response
			goodbye 1
		fi
	else
		bus = 0
	fi
else
	bus = 0
fi

/bin/cat <<- EOF

You have to enter the SCSI unit number for the Hitachi VY-5000. It is
set to SCSI unit number 2 when shipped from the factory. If the
workstation and printer are connected properly and the workstation has
been rebooted after they were connected, the "hinv" command should
indicate which SCSI unit number the printer is.

EOF

echo Enter the SCSI unit number which will be used by the VY-5000? [exit] \\c
read port
if [ "$port" ]; then
	if /bin/expr $port : '[0-9][0-9]*' > /dev/null 2>&1; then
		if [ -c /dev/scsi/sc0d${port}l0 ]; then
			hitachidevice=/dev/scsi/sc0d${port}l0
			if /bin/chmod 666 $hitachidevice; then
				:
			else
				echo could not modify $hitachidevice device file \
					 - are you logged in as root?
				goodbye 1
			fi
		else
			echo There is no SCSI device number $port
			goodbye 1
		fi
	else
		echo Numbers are the only valid response
		goodbye 1
	fi
else
	goodbye 1
fi

#
# we have to alter interface script to do the following:
# - setup proper SCSI device
# - setup proper path to actual hitachi driver program
# - setup ALIAS_LOCATION so that security works
#
interface=vy5000_face
driver=vy5000_driver
if [ "$ALIAS_LOCATION" ]; then
	if [ -x "${ALIAS_LOCATION}/etc/$driver" ]; then
		#
		# make copy of interface script and use editor to change it
		#
		if /bin/cp $interface $tempinterface; then
			/bin/ed $tempinterface <<- EOF >/dev/null
			g;^DEV=.*;s;;DEV=$hitachidevice;
			g;^PRINTLIB=.*;s;;PRINTLIB=${ALIAS_LOCATION}/etc;
			g;^FILTER=.*;s;;FILTER=$driver;
			g;^ALIAS_LOCATION=.*;s;;ALIAS_LOCATION=$ALIAS_LOCATION;
			w
			q
			EOF
			if [ $? -ne 0 ]; then
				/bin/cat <<- EOF

				Something went wrong when editing copy of $interface
				It is very important that $interface is not changed
				from the version installed by loadtape.

				EOF
				goodbye 1
			fi
		else
			/bin/cat <<- EOF

			Couldn't copy the "$interface" interface script
			Are you in the directory which contains this installation script?

			EOF
			goodbye 1
		fi
	else
		/bin/cat <<- EOF

		Couldn't find the "$driver" driver program
		Are you in the directory which contains this installation script?

		EOF
		goodbye 1
	fi
else
	echo The ALIAS_LOCATION environment variable has to be set
	goodbye 1
fi

echo Installing spooled printer ...
if /usr/lib/lpadmin -p$hitachiname -i$tempinterface -v/dev/null; then
	/usr/bin/enable $hitachiname
	/usr/lib/accept $hitachiname
	echo done
else
	echo "Error occured when installing printer driver"
	goodbye 1
fi

goodbye 0
