#!/bin/sh
#Tag 2560010

#*****************************************************************************
#                                                                            *
#            Copyright (C) 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/stand/RCS/alias_print.sh_sgi,v $ $Revision: 5.0 $
#	$Author: software $ $Date: 94/03/23 02:39:10 $
#
#
# Front end for printing on the SGI platforms
# - trys to find all printers and determines which ones are color or
#   postscript printers
# - if there is only one it prints to that printer
# - if there is more than one a selection dialog is presented to the user
#   asking which printer they wish to use
#
#	May/93	PS	Added support for SGI's IMPRESSARIO product for
#			new printer drivers.
#

COLOR_PRINTERS=/usr/tmp/ap_printers.$$
TEMP_IMAGE=/usr/tmp/ap_image.$$
LP_INTERFACE=/usr/spool/lp/interface
SELECT_PRINTER=selectprinter

trap '/bin/rm -f $COLOR_PRINTERS $TEMP_IMAGE' 0
trap '/bin/rm -f $COLOR_PRINTERS $TEMP_IMAGE ;  exit 1' 1 2 3 15

#
# Check to see if the system has Impressario loaded on it. If so, then
# we will use that to print to anonymous printers.
#
if [ -x /usr/sbin/printpanel ]; then
    IMPRESSARIO=TRUE
else
    IMPRESSARIO=FALSE
fi

#
# go through arguments and do some unsophisticated separating of options
# and filenames
#
files=
options=
for arg in $* ; do
	case $arg in
		-*) options="$options $arg" ;;
		*)  files="$files $arg" ;;
	esac
done

if [ -z "$files" ]; then
	echo "Usage: `basename $0` [<printer specific options>] pixfile ..." 1>&2
	exit 1
fi

#
# In IRIX 4.0, all available printers have a script/program in $LP_INTERFACE
#
unknowns=
for p in $LP_INTERFACE/* ; do
	printer=`/bin/basename $p`

	#
	# These variables will be set by eval'ing the results of the lputil
	# program
	#
	NAME=
	TYPE=
	HOSTNAME=
	HOSTPRINTER=

	#
	# Get information on this printer and set the above variables
	#
	eval `/usr/lib/lputil getinfo $printer`

	#
	# if the TYPE is unknown or blank and this is a remote printer, try and
	# get some better info from remote host
	#
	if [ \( "$TYPE" = "unknown" -o -z "$TYPE" \) -a \
				-n "$HOSTNAME" -a -n "$HOSTPRINTER" ] ; then
		#
		# HOSTNAME and HOSTPRINTER will get schmucked, so save them
		#
		save_HOSTNAME=$HOSTNAME
		save_HOSTPRINTER=$HOSTPRINTER
		eval `/usr/lib/lputil remoteinfo $HOSTNAME $HOSTPRINTER $USER`
		HOSTNAME=$save_HOSTNAME
		HOSTPRINTER=$save_HOSTPRINTER
	fi

	if [ -z "$TYPE" -o "$TYPE" = "unknown" ] ; then
		unknowns="$unknowns $printer"
	fi

	#
	# The only printers which can print images are Color or
	# PostScript printers
	#
	if [ "$TYPE" = "Color" -o "$TYPE" = "PostScript" -o \
         "$IMPRESSARIO" = "TRUE" ] ; then
		if [ -z "$HOSTNAME" ] ; then
			HOSTNAME=`/bin/uname -n`
		fi
		printer_name="$NAME"
		if [ -z "$printer_name" ] ; then
			if [ "$TYPE" = "Color" ] ; then
				printer_name="Color Printer"
			elif [ "$TYPE" = "PostScript" ] ; then
				printer_name="PostScript Printer"
			fi
		fi

		#
		# store the info for this candidate away into our temp file
		#
		echo ${printer}:${printer_name}:${HOSTNAME}:${HOSTPRINTER}:${TYPE}:${NAME} \
														>> $COLOR_PRINTERS
	fi
done

#
# if there were some unknown printer types let the user know about it
#
if [  "$IMPRESSARIO" = "FALSE" ]; then
    if [ -n "$unknowns" ] ; then
	    aliasWarning -B OK \
		    -s "The following printers were of unknown type and were ignored:" \
		    -s "$unknowns" > /dev/null
	    echo "The following printers were of unknown type and were ignored: $unknowns" 1>&2
    fi
fi

#
# Determine how many choices we have and act accordingly
#
if [ -f $COLOR_PRINTERS ] ; then
	choices=`/bin/cat $COLOR_PRINTERS | /bin/wc -l`
else
	choices=0
fi
selected=
if [ $choices -eq 0 ] ; then
	aliasWarning -B OK \
		-s "There are no suitable printers accessible from `uname -n`." \
		> /dev/null
	echo "There are no suitable printers accessible from `uname -n`." 1>&2
	exit 1
elif [ $choices -eq 1 ] ; then
	selected=1
else
	#
	# if more than one choice, present them to the user and let them make
	# the choice
	#
	selected=`/usr/bin/awk -F: '{ printf "%-9s on %-9s (%s)\n", $1, $3, $2 }' \
		$COLOR_PRINTERS | $SELECT_PRINTER`
fi

if [ "$selected" ] ; then
	printer=`/bin/sed -n -e "${selected}p" $COLOR_PRINTERS`

	#
	# extract the info on this printer
	#
	name=`echo $printer | /usr/bin/cut -f2 -d:`
	device=`echo $printer | /usr/bin/cut -f1 -d:`
	type=`echo $printer | /usr/bin/cut -f5 -d:`
	real_name=`echo $printer | /usr/bin/cut -f6 -d:`
	hostname=`echo $printer | /usr/bin/cut -f3 -d:`
	hostprinter=`echo $printer | /usr/bin/cut -f4 -d:`

	#
	# if we never got a definitive printer name for this printer, 
	# and its a remote printer, ask the remote host for better info
	#
	if [ -z "$real_name" -a -n "$hostname" -a -n "$hostprinter" ] ; then
		eval `/usr/lib/lputil remoteinfo $hostname $hostprinter $USER`
		if [ -n "$NAME" ] ; then
			real_name="$NAME"
		fi
	fi

	if [ -n "$real_name" ] ; then
		name=$real_name
	fi

	#
	# go through all the filenames
	#
	for file in $files ; do
		if [ ! -f "$file" ] ; then
			echo "Image file \"$file\" is non-existent - ignored" 1>&2
			continue
		fi

		#
		# if the filename contains the current working directory, chop it off
		#
		cwd=`pwd`
		relative=`expr "$file" : "$cwd/\(.*\)\$"`
		if [ "$relative" ]; then
			file=$relative
		fi

		#
		# check if the printer is one of the ones for which we provide special
		# support
		#
		if [ "$name" = "Kodak XL7700 (IEEE)" ] ; then
			(
			kodaksetup $file | \
			while read args; do
				if [ "$args" ]; then
					/usr/bin/lp $options -d$device $args
				fi
			done
			) &
		elif [ "$name" = "Hitachi VY-5000 (SCSI)" ] ; then
			(
			vy5000setup $file | \
			while read args; do
				if [ "$args" ]; then
					/usr/bin/lp $options -d$device $args
				fi
			done
			) &
		else 
			#
			# Give them a chance to cancel first.
			#
            if [ "$IMPRESSARIO" = "FALSE" ]; then
    			base_file=`basename $file`
	    		if [ -n "$real_name" ] ; then
		    		response=`aliasWarning -b Cancel -B OK \
			    		-s "Print \"$base_file\" on \"$device\" ($name)?"`
			    else
				    response=`aliasWarning -b Cancel -B OK \
				    -s "The printer model couldn't be determined for \"$device\"." \
				    -s "Do you still want to print \"$base_file\" on \"$device\"" \
				    -s "assuming a generic $type printer?"`
			    fi
            fi

			#
			# This is a printer type which is not explicitly supported by ALIAS
			# We assume it is a SGI supported printer which understands SGI
			# format image files; therefore we convert the passed image into
			# SGI format if necessary and pass it on to the LP subsystem.
			#
			if [ "$IMPRESSARIO" = "TRUE" -o "$response" = "OK" ] ; then
				#
				# check if it is an ALIAS format image:
				# - ALIAS pix files have 0x18 in 5th short and ALIAS matte files
				#   have 0x8 in 5th short
				# - if it is ALIAS format convert to SGI format and send it to
				#   printer
				# - if it isn't just pass it on to the printer and hope that it
				#   can understand it
				#
				set -- `/bin/dd bs=1 count=10 < $file 2> /dev/null | /bin/od -h`
				if [ "$6" = 0018 -o "$6" = 0008 ] ; then
					if /usr/sbin/fromalias $file $TEMP_IMAGE ; then
						#
						# use the copy option so that the temp file can be
						# safely removed afterwards
						#
                        if [ "$IMPRESSARIO" = "FALSE" ]; then
    						/usr/bin/lp -c $options -d$device $TEMP_IMAGE
                        else
    						/usr/sbin/printpanel -geometry +210+460 -c $options -d$device < $TEMP_IMAGE
                        fi
						/bin/rm -f $TEMP_IMAGE
					fi
				else
                    if [ "$IMPRESSARIO" = "FALSE" ]; then
    					/usr/bin/lp $options -d$device $file
                    else
                        /usr/sbin/printpanel -geometry +210+460 $options -d$device < $file
                    fi
				fi
			fi
		fi
	done
fi
exit 0
