#! /bin/sh

MAILER=${MAILPROG:-zmail}
ZMAIL=/usr/bin/X11/zmail

mailerror="Could not mail the file. "
unreadable="is not a readable file."
unsupported="is an unsupported mailer."
USAGE="usage: `basename $0` -hytc <files>"
unknownType="is not a mailable icon."
cc=`whoami`

if test ! -x `which $MAILER` 
then
	xconfirm -t "$mailerror $MAILER not found." > /dev/null
	exit 2
fi

while getopts \?hy:t:c: d
do
	case $d in
	y) typs=$OPTARG;;
	t) to=$OPTARG;;
	c) cc=$OPTARG;;
	h) echo $USAGE
	    exit 2;;
	\?) echo $USAGE
            exit 2
	esac
done
shift `expr $OPTIND - 1`



for f in $typs
do
	case $f in 
		AsciiTextFile) typ="text/plain"
	               ;;
	        Ascii) typ="text/plain"
			;;
		ShowcaseFile) typ="application/x-showcase"
			;;
		ShowcaseDocument) typ="application/x-showcase"
			;;
		SoundEditor) typ="audio/x-aiff"
			;;
		TIFFImage) typ="image/tiff"
			;;
		GIFImageFile) typ="image/gif"
			;;
		GIF87ImageFile) typ="image/gif"
			;;
		GIF89ImageFile) typ="image/gif"
			;;
		FITImage) typ="image/gif"
			;;
		SGIImage) typ="image/x-sgi-image"
			;;
		MovieFile) typ="video/x-sgi-movie"
			;;
		SGIMovieFile) typ="video/x-sgi-movie"
			;;
		InventorBinaryData) typ="application/x-inventor"
			;;
		InventorAsciiData) typ="application/x-inventor"
			;;
		AIFFSoundFile) typ="audio/x-aiff"
			;;
		AIFCSoundFile) typ="audio/x-aifc"
			;;
		PostScriptFile) typ="application/postscript"
			;;
		*) 
			if isSuper Directory $f > /dev/null ||
			   isSuper Executable $f > /dev/null ||
			   isSuper GenericWindowedExecutable $f > /dev/null ||
			   isSuper SpecialFile $f > /dev/null ||
			   test "$f" = "UserToken" ||
			   test "$f" = "BuiltInUserToken" ||
			   test "$f" = "NetworkUserToken" ||
			   isSuper TransferDev  $f > /dev/null
		  	 then
			   xconfirm -t "Cannot send \"$1\"." > /dev/null
			   exit 2
			else
		  	   typ="application/octet-stream"
			fi
			;;
	esac
	if test ! -r $1 
	then
	  xconfirm -t "$mailerror $1 $unreadable" > /dev/null
	  exit 2
	fi
	BNAME=`basename $1`
	case $MAILER in 
		zmail)
#	 	 zmail -attach ${typ}:$1 ${to} < /dev/null
#	        echo "Attached is \"$1\" from ${cc}." | 
	        ${ZMAIL} -attach ${typ}:$1 -subject "RECEIVED: ${BNAME}" ${to} < /dev/null
#               echo "Sent \"$1\" to ${to}." | 
		${ZMAIL} -subject "SENT: ${BNAME}" ${cc} < /dev/null
	  	;;
		*)
		  xconfirm -t "$mailerror $MAILER $unsupported" > /dev/null
		  ;;
	esac
	shift
done
exit 0

