#!/bin/sh
# Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
# 
# Permission to use, copy, modify, and distribute this material 
# for any purpose and without fee is hereby granted, provided 
# that the above copyright notice and this permission notice 
# appear in all copies, and that the name of Bellcore not be 
# used in advertising or publicity pertaining to this 
# material without the specific, prior written permission 
# of an authorized representative of Bellcore.  BELLCORE 
# MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
# OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
# WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.

# Conversion from C shell to Bourne shell by Z-Code Software Corp.
# Conversion Copyright (c) 1992 Z-Code Software Corp.
# Permission to use, copy, modify, and distribute this material
# for any purpose and without fee is hereby granted, provided
# that the above copyright notice and this permission notice
# appear in all copies, and that the name of Z-Code Software not
# be used in advertising or publicity pertaining to this
# material without the specific, prior written permission
# of an authorized representative of Z-Code.  Z-CODE SOFTWARE
# MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
# OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS",
# WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.

# Brought into line with metamail 2.7 beta release Csh version
#    Dave Shield	February 1994

if [ -z "$METAMAIL_TMPDIR" ]
then
	METAMAIL_TMPDIR=/tmp
fi

if test -z "${X_VIEWER:-}"
then
	X_VIEWER="display"
# X_VIEWER="xv"
fi

if test "$1" = "-viewer" -a ! -z "$2"
then
	X_VIEWER="$2"
	shift
	shift
fi

if test -z "${MM_NOTTY:-}"
then
	MM_NOTTY=0
fi
if test -z "${DISPLAY:-}"
then
	echo ""
	echo This message contains a picture, which can currently only be
	echo viewed when running X11.  If you read this message while running
	echo X11, you will be able to see the picture properly.
	if test $MM_NOTTY -eq 1
	then
		fname=$METAMAIL_TMPDIR/sp.$$
		echo "The picture is being automatically stored in the file $fname"
		echo "If you do not want this file, please delete it."
	else
		echo -n "Do you want to write the picture out to a file [y] ? "
		read ANS
		case "$ANS" in
		    [Nn]*)	exit 0;;
		    *)	looping=1
			while test $looping -eq 1
			do
				echo -n "File name: "
				read fname
				if test -f $fname
				then
					echo "$fname already exists. Try again"
				else
					looping=0
				fi
			done ;;
		esac
	fi
	cp $1 $fname
	if test $? -eq 0
	then
		echo Wrote file $fname
	fi
	exit 0
fi

if test -z "$1"
then
	SQUIRREL=${METAMAIL_TMPDIR}/sp.$$
	cat > $SQUIRREL
else
	SQUIRREL="$*"
fi


echo NOTE: TO MAKE THE PICTURE WINDOW GO AWAY, JUST TYPE 'q' IN IT.
if test -z "$1"
then
	echo "==> TO SAVE THIS IMAGE, COPY THE FILE $SQUIRREL BEFORE EXITING"
	echo ""
	$X_VIEWER $SQUIRREL
	rm $SQUIRREL
else
	for i in $SQUIRREL
	do
		echo "==> TO SAVE THIS IMAGE, COPY THE FILE $i BEFORE EXITING"
		echo ""
		dir=`dirname $i`
		base=`basename $i`
		if test ! "$base" = "$i"
		then
			cd $dir
		fi
		if ln $i $$.PRESS-q-TO-EXIT  > /dev/null 2>&1
		then
		    $X_VIEWER $$.PRESS-q-TO-EXIT
		    rm $$.PRESS-q-TO-EXIT 
		else
		    $X_VIEWER "$i"
		fi
	done
fi
