:
# 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 Bob Glickstein, Z-Code Software Corp.
# Conversion Copyright (c) 1992 Z-Code Software Corp. (Z-Code)
# 
# 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 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 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.

MustDelete=0
batchmode=0
splitsize = 100000

while test ! -z "$*"
do
	case $1 in
		-S) shift
		    if test -z "$*"
		    then
			echo "-S requires a following argument, the SPLIT threshhold"
			exit 1
		    fi
		    splitsize=$1
		    shift ;;

		-b) batchmode=1
		    shift ;;

		-c) shift
		    if test -z "$*"
		    then
		    	echo "-c requires a following argument, the CC address"
			exit 1
		    fi
		    cc=$1
		    shift ;;

		-s) shift
		    if test -z "$*"
		    then
			echo "-s requires a following argument, the SUBJECT"
			exit 1
		    fi
		    subject=$1
		    shift ;;

		-t) shift
		    if test -z "$*"
		    then
			echo "-t requires a following argument, the TO address"
			exit 1
		    fi
		    to=$1
		    shift ;;

		-e) shift
		    if test -z "$*"
		    then
			echo "-e requires a following argument, the ENCODING value"
			exit 1
		    fi
		    encode=$1
		    shift ;;

		-f) shift
		    if test -z "$*"
		    then
			echo "-f requires a following argument, the DATA FILE"
			exit 1
		    fi
		    datafile=$1
		    shift ;;

		-m) shift
		    if test -z "$*"
		    then
			echo "-m requires a following argument, the MIME CONTENT-TYPE"
			exit 1
		    fi
		    ctype=$1
		    shift

		-z) MustDelete=1
		    shift ;;

		*) echo UNRECOGNIZED METASEND OPTION: $1
		   exit 1 ;;
	esac
done

if test $batchmode -eq 0
then
	if test -z "${to:-}
	then
		echo-n "To: "
		read to
	fi
	if test -z "${subject:-}"
	then
        	echo-n "Subject: "
		read subject
	fi
	if test -z "${cc:-}"
	then
		echo-n "CC: "
		read cc
	fi
	if test -z "${ctype:-}"
	then
        	echo-n "Content-type: "
		read ctype
	fi
	if test -z "${datafile:-}"
	then
		looping=1
		while test $looping -eq 1
		do
			echo-n "Name of file containing $ctype data: "
			read datafile
			if test -r "$datafile"
			then
				looping=0
			else
				echo "The file $datafile does not exist."
			fi
		done
	fi

	if test -z "${encode:-}"
	then
		looping=1
		while test $looping -eq 1
		do
			echo "Do you want to encode this data for sending through the mail?"
			echo "  1 -- No, it is already in 7 bit ASCII"
			echo "  2 -- Yes, encode in base64 (most efficient)"
			echo "  3 -- Yes, encode in quoted-printable (less efficient, more readable)"
			echo "  4 -- Yes, encode it using uuencode (not standard, being phased out)"
			read encode
			looping=0
			case "$encode" in
				1) encodingprog=cat
				   encode=7bit ;;
				2) encodingprog="mmencode -b"
				   encode=base64 ;;
				3) encodingprog="mmencode -q"
				   encode=quoted-printable ;;
				4) encodingprog="uuencode $datafile"
				   encode=x-uue
				*) echo Unrecognized answer, please try again.
				   looping=1 ;;
			esac
		done
	fi
else
	if test -z "${to:-}" \
		-o -z "${subject:-}" \
		-o -z "${ctype:-}" \
		-o -z "${datafile:-}"
	then
		echo "metasend: in batch mode, -t, -s, -f, and -m are all required"
		exit 1
	fi

	if test ! -r "$datafile"
	then
		echo "metasend: The file $datafile does not exist"
		exit 1
	fi

	if test -z "${cc:-}"
	then
		cc=''
	fi

	if test -z "${encode:-}"
	then
		case "$ctype" in
			text*) encodingprog=mmencode -q
			       encode=quoted-printable ;;
			*) encodingprog=mmencode -b
			   encode=base64 ;;
		esac
	else
		case "$encode" in
			base64) encodingprog="mmencode -b" ;;
			x-uue) encodingprog="uuencode $datafile" ;;
			*) encodingprog="mmencode -q"
			   encode=quoted-printable ;;
		esac
fi

fname=/tmp/metasend.$$
echo "To: $to" > $fname
echo "Subject: $subject" >> $fname
echo "CC: $cc" >> $fname
echo "MIME-Version: 1.0" >> $fname
echo "Content-type: $ctype" >> $fname
echo "Content-Transfer-Encoding: $encode" >> $fname
echo "" >> $fname
"$encodingprog" < "$datafile" >> "$fname"
# Ensure last line has trailing carriage return
echo "" >> "$fname"

splitmail -s $splitsize -d $fname

if test $? -eq 0
then
	rm -f $fname
elif test "$MustDelete" -eq 1
then
	echo Mail delivery failed
	rm -f $fname
else
	echo "Mail delivery failed, draft mail is in $fname"
fi
