#!/bin/sh

#*****************************************************************************
#                                                                            *
#            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: /u/ikhan/carme/s/Utility/obsolete/RCS/alias_print.sh,v $ $Revision: 1.1 $
#	$Author: ikhan $ $Date: 97/07/15 13:57:03 $
#
#
# Front end for printing on the SGI platforms
# Checks to see if the file is an Alias pix image file and if so converts
# it to SGI rgb format and sends it to Impressario if installed.
#
#	May/93	PS	Added support for SGI's IMPRESSARIO product for
#			new printer drivers.
#

TEMP_IMAGE=/usr/tmp/ap_image.$$

if [ -x $ALIAS_LOCATION/bin/aliasWarning ]; then
    ALIAS_WARN=$ALIAS_LOCATION/bin/aliasWarning
else
    ALIAS_WARN=aliasWarning
fi


trap '/bin/rm -f $TEMP_IMAGE' 0
trap '/bin/rm -f $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
    $ALIAS_WARN -B OK \
            -s "alias_print uses Impressario." \
            -s "Impressario not found on the current system" \
			-s "Files not submitted to a printer queue" > /dev/null
	echo "alias_print assumes the use of Impressario.  Impressario not found"
	echo "on the current system.  Files not printed"
	exit 1
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

#
# 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

	#
	# 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.
	#
	#
	# 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
		#
   			/usr/sbin/printpanel -geometry +210+460 -c $options < $TEMP_IMAGE
        fi
		/bin/rm -f $TEMP_IMAGE
	else
        /usr/sbin/printpanel -geometry +210+460 $options < $file
	fi

done

exit 0
