#!/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: /tmp_mnt/worf/root/v/alias5.1/src/Utility/kodak/RCS/install_kodak.sh_sgi,v $ $Revision: 5.0 $
# 	$Author: software $ $Date: 94/03/23 02:33:20 $
 
#
#      filename: install_kodak.sh
#      purpose : execute this script file as root to initially install
#                the lp support for the Kodak XL7700 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_kodak
#
#      NOTE #1: since there was problems giving the line printer spooler
#               the actual IEEE device, we now tell the lp subsystem that
#               the device is /dev/null and insert the proper device into
#               the kodakface 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 kodak, the kodakname
# variable can be changed to something more unique. This could happen if the
# customer already has another printer named kodak
#
kodakname=kodak

tempinterface=/tmp/kodakinst.$$

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 Kodak XL7700 Digital Continuous Tone Color Printer

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 kodak 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 "/${kodakname}/p" | while read line; do
	#
	# check if lp device named $kodakname is already there
	#
	dev=`/bin/expr "$line" : "device for ${kodakname}: \(.*\)"`
	if [ "$dev" ]; then
		if [ "$dev" = "/dev/null" ]; then
			/bin/cat <<- EOF

			The Alias Kodak XL7700 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 "$kodakname" 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 $kodakname
		else
			goodbye 1
		fi
	fi
done
if [ $? -ne 0 ]; then
	exit 1
fi

#
# ask which IEEE address is being used by the printer
#
/bin/cat <<- EOF

You have to enter the IEEE device number for the Kodak XL7700. It is
device 1 by default. The IEEE device selector dial is on the back of
the printer if you want to verify or change it to something else. Make
sure you turn the printer off and on if you change the IEEE device
number.

EOF

echo Enter the IEEE device number which will be used by the XL7700? [exit] \\c
read port
if [ "$port" ]; then
	if /bin/expr $port : '[0-9][0-9]*' > /dev/null 2>&1; then
		if [ -c /dev/dev$port ]; then
			kodakdevice=/dev/dev$port
			if /bin/chmod 666 $kodakdevice; then
				:
			else
				echo could not modify $kodakdevice device file \
					 - are you logged in as root?
				goodbye 1
			fi
		else
			echo There is no IEEE 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 IEEE device
# - setup proper path to actual kodak driver program
# - setup ALIAS_LOCATION so that security works
#
interface=kodakface
driver=kodakdriver
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=$kodakdevice;
			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$kodakname -i$tempinterface -v/dev/null; then
	/usr/bin/enable $kodakname
	/usr/lib/accept $kodakname
	echo done
else
	echo "Error occured when installing printer driver"
	goodbye 1
fi

goodbye 0
