#!/bin/csh -fb
# (The "-fb" might need to be changed to "-f" on some systems)
#
# 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.
#

# Set a sensible value for the temporary directory, if its not
# already set.  If TMPDIR is set previously, then we will
# assume it is adequately protected.
if (! $?METAMAIL_TMPDIR) then
    if ($?TMPDIR) then
        set METAMAIL_TMPDIR="$TMPDIR"
    else
        set METAMAIL_TMPDIR=~/metamail_tmp
    endif
endif

# Set a sensible umask value
umask 077

# Make sure that the temporary directory is available
if (! -d "$METAMAIL_TMPDIR") then

    if (! -e "$METAMAIL_TMPDIR") then
        mkdir "$METAMAIL_TMPDIR"
    else
        echo "$METAMAIL_TMPDIR exists, but is not a directory"
        exit 2
    endif

    if ( $status != 0 || ! -d "$METAMAIL_TMPDIR" ) then
        echo "Error creating $METAMAIL_TMPDIR"
        exit 2
    endif

endif

if (! $?X_VIEWER) then
  set X_VIEWER="xloadimage -view -quiet -geometry +1+1"
# set X_VIEWER="xv -geometry +1+1"
endif
if ("$1" == "-viewer" && $#argv > 1) then
    set X_VIEWER = "$2"
    shift
    shift
endif

if (! $?MM_NOTTTY) then
    set MM_NOTTTY=0
endif
if (! $?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, and have your DISPLAY variable set, you will then
    echo be able to see the picture properly.
    echo ""
    if ($MM_NOTTTY) then
        set 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] ? "
        set ANS=$<
        if ("$ANS" =~ n* ||  "$ANS" =~ N* ) exit 0
        while ( 1 == 1)
            echo -n "File name:"
            set fname=$<
            if (! -f $fname) then
                break
            else
                echo "$fname already exists. Try again"
            endif
        end
    endif
    cp "$1" $fname
    if ($status == 0) echo Wrote file $fname
    exit 0
endif

if ($#argv == 0) then
    set SQUIRREL=${METAMAIL_TMPDIR}/sp.$$
    cat > $SQUIRREL
else
    set SQUIRREL="$*"
endif

echo NOTE:  TO MAKE THE PICTURE WINDOW GO AWAY, JUST TYPE 'q' IN IT.
if ($#argv == 0) then
    echo "==> TO SAVE THIS IMAGE, COPY THE FILE $SQUIRREL BEFORE EXITING"
    echo ""
    $X_VIEWER $SQUIRREL
    rm $SQUIRREL
else

foreach i ($*)
    echo "==> TO SAVE THIS IMAGE, COPY THE FILE $i BEFORE EXITING"
    echo ""
    set dir=$i:h
    if ($dir != $i) then
	# Make the ln sure to work	
	cd $dir
    endif
    ln $i $$.PRESS-q-TO-EXIT 
    if (! $status) then 
        sh -c "$X_VIEWER $$.PRESS-q-TO-EXIT"
        rm $$.PRESS-q-TO-EXIT 
    else
        sh -c "$X_VIEWER $i"
    endif
end
endif
