#!/bin/sh
#Tag 0x00000f00
#
# SGI lp installation script for CAP 6.0
# 6/7/94 rks  (smith@cbrsgi.med.harvard.edu)
#
# change the following to set the defaults
tmpdir=/usr/tmp
capdir=/usr/local/cap
etcdir=/etc
interface=CAPinterface
#
#
if [ ! -w / ]; then
	echo "You must be root to use this script"
	exit
fi
echo
echo
echo "CAP 6.0 lp printer installation script for SGI IRIX 4.x"
echo 
echo "Be sure that you look at the README file before you install any printers!"
echo
echo "Do you want to continue? (y)"
while true
do
	read ans
	if [ -z "$ans" ]; then
	  ans="yes"
	fi
	
	case $ans in
		y* | Y*) result=1; break;;
		n* | N*) result=0; break;;
		*) echo "You must answer yes or no or a carriage return for yes." ;;
	esac
done
if [ $result -eq 0 ]; then
  exit
fi

# get the interface file
if [ -r "$1" ]; then
	interface=$1
fi
while true
do
	if [ -s "$interface" ]; then
		break
	else
		echo "What is the interface file?"
		read interface
	fi
done

# make sure the script can find the CAP executables
while true
do
	if [ -x "$capdir/atlooklws" ]; then
		break
	else
		echo "Where are the CAP executables?"
		read capdir
	fi
done

# get the default zone name
echo "Checking default zone ..."
defzone=`$capdir/atlooklws | grep 'Looking for' | awk -F@ '{print $2}' |awk '{print $1}'`

# main loop
while true
do
	while true
	do
		zone=$defzone
	
		# check printers with atlooklws
		while true
		do
			echo "Checking printers in zone $zone ..."
			echo 
			$capdir/atlooklws $zone | grep ' - ' | awk -F: '{print $1}' > $tmpdir/printerlist$$
			cat $tmpdir/printerlist$$
			printnums=`cat $tmpdir/printerlist$$ | awk '{print $1}'`
			echo
			echo "Which printer do you want to install?"
			echo "(Enter the number of the printer, or z to change zones)"
			while true
			do
				read ans
				case "$ans" in
					Z | z)
						# change zones
						$capdir/getzones | nl > $tmpdir/getzones$$
						cat $tmpdir/getzones$$
						zonenums=`cat $tmpdir/getzones$$ | awk '{print $1}'`
						echo
						while true
						do
							echo "Which zone number do you want to change to ?"
							read znum
							for z in $zonenums
							do
								if [ "$z" = "$znum" ]; then
									zone=`cat $tmpdir/getzones$$ | cut -f2-100 |sed -n "${z}p"`
									break 3
								fi
							done
						done;;
					*)
						# get apple printer name
						for num in $printnums
						do
							if [ "$ans" = "$num" ]; then
								printer=`cat $tmpdir/printerlist$$ | cut -c7-100 | sed -n "${num}p"`
								echo "$printer"
								break 3 
							fi
						done;;
				esac
				echo "(Enter the number of the printer, or z to change zones)"
			done
		done

		# get UNIX printer name
		defname=LaserWriter
		n=2 
		while true
		do
			if [ -f /usr/spool/lp/interface/$defname ]; then
				defname=LaserWriter$n
				n=`expr $n + 1`
			else
				break
			fi
		done
		echo "What do you want to use for the UNIX printer name? ($defname)"
		while true
		do
			read uname
			if [ -z "$uname" ]; then
				uname=$defname
				break
			fi
			if [ -f /usr/spool/lp/interface/"$uname" ]; then
				echo "That name is being used already. Select another name."
			else
				break
			fi
		done
		
		# get the location of the cap.printers file
		while true
		do
			echo "Where does the cap.printers file go? ($etcdir)"
			read ans
				if [ -z "$ans" ]; then
					break
				fi
				if [ -d "$ans" ]; then
					etcdir="$ans"
					break
				fi
		done

		# ask whether to modify the WorkSpace
		wsmod=0
		if [ ! -f /usr/lib/filetype/local/CAPprint.ftr ]; then 
			echo "Do you wish to modify your WorkSpace filetype rules so that you can"
			echo "print to CAP printers directly from the WorkSpace? (y)" 
			while true
			do
				read ans
				if [ -z "$ans" ]; then
	  				ans="yes"
				fi

				case $ans in
					y* | Y*) result=1; break;;
					n* | N*) result=0; break;;
					*) echo "You must answer yes or no or a carriage return for yes." ;;
				esac
			done
			if  [ $result = 1 ]; then
				wsmod=1
			fi
		fi

		# confirm the settings
		echo
		echo
		echo "Interface: $interface"
		echo "AppleTalk Zone: $zone"
		echo "Apple printer name: $printer"
		echo "UNIX printer name: $uname"
		echo "cap.printers location: $etcdir"
		if [ ! -f /usr/lib/filetype/local/CAPprint.ftr ]; then 
			if [ $wsmod = 1 ]; then
				echo "Modify filetype rules: yes"
			else
				echo "Modify filetype rules: no"
			fi
		fi
		echo
		echo
		echo
		echo "Is this correct? (y)"	
		while true
		do
			read ans
			if [ -z "$ans" ]; then
	  			ans="yes"
			fi
			case $ans in
				y* | Y*) result=1; break;;
				n* | N*) result=0; break;;
				*) echo "You must answer yes or no or a carriage return for yes." ;;
			esac
		done
		if [ $result = 1 ]; then
			break
		else
			echo "Let's try again..."
		fi
	done
	
	# allow the user to bail out
	echo "Do you want to install the interface now? (y)"
	while true
	do
		read ans
		if [ -z "$ans" ]; then
	  		ans="yes"
		fi
		case $ans in
			y* | Y*) result=1; break;;
			n* | N*) result=0; break;;
			*) echo "You must answer yes or no or a carriage return for yes." ;;
		esac
	done
	if [ $result = 0 ]; then
		echo "Goodbye..."
		echo
		rm -rf $tmpdir/printerlist$$
		rm -rf $tmpdir/getzones$$
		exit
	fi
	
	# Install everything
	
	# make/modify the cap.printers file
	echo "${uname}=${printer}:LaserWriter@${zone}" > $tmpdir/capentry$$
	touch $tmpdir/cpold$$
	if [ -f $etcdir/cap.printers ]; then 
		cp $etcdir/cap.printers $etcdir/cap.printers.old
		sed "/^${uname}=/d" $etcdir/cap.printers > $tmpdir/cpold$$
	fi
	cat $tmpdir/cpold$$ $tmpdir/capentry$$ > $etcdir/cap.printers
	
	# install the interface
	/usr/lib/lpshut
	/usr/lib/lpadmin -p"$uname" -v/dev/null -i"$interface"
	/usr/lib/lpsched
	/usr/lib/accept "$uname"
	enable "$uname"
	
	# modify the filetype rules and the printtest script
	if [ $wsmod = 1 ]; then
 		mv /usr/lib/vadmin/printtest /usr/lib/vadmin/printtest.old
		cp ./printtest /usr/lib/vadmin
		cp ./CAPprint.ftr /usr/lib/filetype/local
		dir=`pwd`
		cd /usr/lib/filetype
		make
		cd $dir
	fi
	
	# delete the temp files
	rm -rf $tmpdir/printerlist$$
	rm -rf $tmpdir/getzones$$
	rm -rf $tmpdir/cpold$$
	rm -rf $tmpdir/capentry$$
	echo
	echo "Installation complete"
	echo
	echo "Do you want to install another printer? (n)"
	
	# install another printer or exit
	while true
	do
		read ans
		if [ -z "$ans" ]; then
	  		ans="no"
		fi
		case $ans in
			y* | Y*) result=1; break;;
			n* | N*) result=0; break;;
			*) echo "You must answer yes or no or a carriage return for no." ;;
		esac
	done
	if [ $result = 0 ]; then
		echo "Goodbye..."
		echo
		exit
	fi
done
 
