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

hname=`hostname`
idctr=0
MustDelete=0
batchmode=0
multipart=1
if test ! -z "${SPLITSIZE:-}"
then
	splitsize=$SPLITSIZE
else
	splitsize=100000
fi

ouputfile=""
multipartsubtype="mixed"
cdescrip="An object packed by metasend"
isentity=0
newid=`date | tr " :" "__"`
cid="<${newid}_$idctr@${hname}>"
idctr=`expr $idctr + 1`

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

		-i) shift
		    if test -z "$*"
		    then
			echo "-i requires a following argument, the Content-ID value"
			exit 1
		    fi
		    cid=$1
		    shift ;;

		-I) shift
		    if test -z "$*"
		    then
			echo "-I requires a following argument, the multipart Content-ID value"
			exit 1
		    fi
		    multicid=$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 ;;

		-F) shift
		    if test -z "$*"
		    then
			echo "-F requires a following argument, the FROM address"
			exit 1
		    fi
		    from=$1
		    shift ;;

		-P) shift
		    if test -z "$*"
		    then
			echo "-P requires a following argument, the preamble file name"
			exit 1
		    fi
		    preamblefile=$1
		    shift ;;

		-e) shift
		    if test -z "$*"
		    then
			echo "-e requires a following argument, the ENCODING value"
			exit 1
		    fi
		    encode=$1
		    case "$encode" in
			"base64")	encodingprog="mimencode -b";;
			"x-uue")	encodingprog="uuencode \$datafile";;
			"7bit")		encodingprog=cat;;
			*)		encodingprog="mimencode -q"
					encode=quoted-printable ;;
		    esac
		    shift ;;

		-f) shift
		    if test -z "$*"
		    then
			echo "-f requires a following argument, the DATA FILE"
			exit 1
		    fi
		    datafile=$1
		    if test ! -r $datafile
		    then
			echo The file $datafile does not exist
			exit 1
		    fi
		    shift ;;

		-m) shift
		    if test -z "$*"
		    then
			echo "-m requires a following argument, the MIME CONTENT-TYPE"
			exit 1
		    fi
		    ctype=$1
		    if test "`echo $ctype | grep /`" = ""
		    then
			echo Invalid content-type specified
			exit 1
		    fi
		    shift ;;

		-o) shift
		    if test -z "$*"
		    then
			echo "-o requires a following argument, the output file name"
			exit 1
		    fi
		    outputfile=$1
		    shift ;;

		-/) shift
		    if test -z "$*"
		    then
			echo "-/ requires a following argument, the multipart subtype"
			exit 1
		    fi
		    multipartsubtype=$1
		    shift ;;

		-D) shift
		    if test -z "$*"
		    then
			echo "-D requires a following argument, the Content-Description value"
			exit 1
		    fi
		    cdescrip=$1
		    shift ;;

		-E) isentity=1
		    shift ;;

		-z) MustDelete=1
		    shift ;;

		-n) if test $isentity -ne 0
		    then
			if test -z "${encode:-}"
			then
				encode=7bit
				encodingprog=cat
			fi
			if test -z "${ctype:-}"
			then
				ctype=bogus
				# Never used
			fi
			if test -z "${datafile:-}"
			then
				echo One part is incomplete -- each part needs at least -f and -E
				exit 1
			fi
		    else
			if test -z "${ctype:-}" || test -z "${datafile:-}"
			then
				echo One part is incomplete -- each part needs at least -f and -m
				exit 1
			fi
		    fi
		    if test -z "${encode:-}"
		    then
		    	case "$ctype" in
				text*)	encodingprog="mimencode -q"
					encode=quoted-printable ;;
				*)	encodingprog="mimencode -b"
					encode=base64 ;;
			esac
		    fi
		    eval ctype${multipart}=\"$ctype\"
		    eval datafile${multipart}=$datafile
		    eval encode${multipart}=$encode
		    eval cdescrip${multipart}=\"$cdescrip\"
		    eval cid${multipart}=\"$cid\"
		    eval isentity${multipart}=$isentity
		    eval encodingprog${multipart}=\"\$encodingprog\"

		    ctype=""
		    datafile=""
		    encode=""
		    encodingprog=""
		    newid=`date | tr " :" "__"`
		    cid="<${newid}_$idctr@${hname}>"
		    idctr=`expr $idctr + 1`
		    cdescrip="An object packed by metasend"
		    isentity=0
		    multipart=`expr $multipart + 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

	anotherpart=1
	while test $anotherpart -eq 1
	do
	    looping=1
	    while test $looping -ne 0
	    do
	        if test -z "${ctype:-}"
	        then
        		echo -n "Content-type: "
			read ctype
		fi
		if test "`echo $ctype | grep /`" = ""
		then
			echo Invalid content-type specified
			ctype=""
		else
			looping=0
		fi
	    done
	    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="mimencode -b"
				   encode=base64 ;;
				3) encodingprog="mimencode -q"
				   encode=quoted-printable ;;
				4) encodingprog="uuencode $datafile"
				   encode=x-uue ;;
				*) echo Unrecognized answer, please try again.
				   looping=1 ;;
			esac
		done
	    fi

	    echo -n "Do you want to include another file too (y/n) [n] ? "
	    read ans
	    case $ans in
		[Yy]*)	
		    eval ctype${multipart}=\"$ctype\"
		    eval datafile${multipart}=$datafile
		    eval encode${multipart}=$encode
		    eval encodingprog${multipart}=\"\$encodingprog\"
		    eval cdescrip${multipart}=\"$cdescrip\"
		    eval cid${multipart}=\"$cid\"
		    eval isentity${multipart}=$isentity
		    cdescrip="An object packed by metasend"
		    isentity=0
		    ctype=""
		    datafile=""
		    encode=""
		    encodingprog=""
		    newid=`date | tr " :" "__"`
		    cid="<${newid}_$idctr@${hname}>"
		    idctr=`expr $idctr + 1`
		    multipart=`expr $multipart + 1`
		    ;;
		*)
		    anotherpart=0;;
	    esac
	done

else
	if test -z "${outputfile:-}"
	then
	    if test -z "${to:-}" \
		-o -z "${subject:-}" \
		-o -z "${ctype:-}" \
		-o -z "${datafile:-}"
	    then
		echo "metasend: in batch mode, if output is not directed to a file,  -t, -s, -f, and -m are all required"
		exit 1
	    fi
	else
	    if test -z "${datafile:-}"
	    then
		echo "metasend: in batch mode, -f is required."
		exit 1
	    fi
	    if test $isentity -ne 0
	    then
	        if test -z "${ctype:-}"
	        then
			ctype=bogus
			#  will not be used anyway
		fi
	    fi
	    if test -z "${ctype:-}"
	    then
		echo "metasend: in batch mode, with output directed to a file, -E or -m is required."
		exit 1
	    fi
	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="mimencode -q"
			       encode=quoted-printable ;;
			*) encodingprog="mimencode -b"
			   encode=base64 ;;
		esac
	else
		case "$encode" in
			base64) encodingprog="mimencode -b" ;;
			x-uue)  encodingprog="uuencode $datafile" ;;
			7bit)   encodingprog=cat ;;
			*) encodingprog="mimencode -q"
			   encode=quoted-printable ;;
		esac
	fi
fi

if test -z "${encode:-}"
then
	case "$ctype" in
		text*) encodingprog="mimencode -q"
		       encode=quoted-printable ;;
		*) encodingprog="mimencode -b"
		   encode=base64 ;;
	esac
fi

eval ctype${multipart}=\"$ctype\"
eval datafile${multipart}=$datafile
eval encode${multipart}=$encode
eval cdescrip${multipart}=\"$cdescrip\"
eval cid${multipart}=\"$cid\"
eval isentity${multipart}=$isentity
eval encodingprog${multipart}=\"\$encodingprog\"

fname=$METAMAIL_TMPDIR/metasend.$$
echo "MIME-Version: 1.0" > $fname

if test ! -z "${to:-}"
then echo "To: $to" >> $fname
fi
if test ! -z "${subject:-}"
then echo "Subject: $subject" >> $fname
fi
if test ! -z "${cc:-}"
then echo "CC: $cc" >> $fname
fi
if test ! -z "${from:-}"
then echo "From: $from" >> $fname
fi

if test $multipart -gt 1
then
	boundary=${hname}.$$.`date | tr "  " .`
	newid=`date | tr " :" "__"`
	if test ! -z "${multicid:-}"
	then
		echo "Content-ID: $multicid" >> $fname
	else
		echo "Content-ID: ${newid}_$idctr@${hname}" >> $fname
		idctr=`expr $idctr + 1`
	fi
	echo "Content-type: multipart/$multipartsubtype;" >> $fname
	echo "	boundary="\"$boundary\" >> $fname
	echo "" >> $fname
	if test ! -z "${preamblefile:-}"
	then
		cat $preamblefile >> $fname
	else
		echo "This is  a multimedia message in MIME  format.  If you are reading this" >> $fname
		echo "prefix, your mail reader does  not understand MIME.  You may wish" >> $fname
		echo "to look into upgrading to a newer version of  your mail reader." >> $fname
	fi

	i=1
	while test $multipart -ge $i
	do
		echo "" >> $fname
		echo --$boundary >> $fname
		eval isentity=\$isentity$i

		if test $isentity -eq 0
		then
			eval echo "Content-ID: \$cid$i" >> $fname
			eval echo "Content-type: \$ctype$i" >> $fname
			eval echo "Content-Description: \$cdescrip$i" >> $fname
			eval echo "Content-Transfer-Encoding: \$encode$i" >> $fname
			echo "" >> $fname
		fi
		eval \$encodingprog$i \< \$datafile$i \>\> $fname
		i=`expr $i + 1`
	done
	echo "" >> $fname
	echo --${boundary}-- >> $fname
	echo "" >> $fname
else
	if test $isentity -eq 0
	then
		echo "Content-ID: $cid" >> $fname
		echo "Content-type: $ctype" >> $fname
		echo "Content-Description: $cdesrip" >> $fname
		echo "Content-Transfer-Encoding: $encode" >> $fname
	fi
	echo "" >> $fname
	$encodingprog < $datafile >> $fname
	# Ensure last line has trailing carriage return
	echo "" >> $fname
fi

if test -z "${outputfile:-}"
then
	if test $batchmode = 0
	then
		echo -n "Delivering mail, please wait...  "
	fi
	splitmail -s $splitsize -d $fname

	if test $? -eq 0
	then
		if test $batchmode = 0
		then
			echo "Mail delivery apparently succeeded."
		fi
		rm -f $fname
	elif test "$MustDelete" -eq 1
	then
		echo Mail delivery failed
		rm -f $fname
		exit -1
	else
		echo "Mail delivery failed, draft mail is in $fname"
		exit -1
	fi
else
	mv $fname $outputfile
	if test $? -ne 0
	then
		exit -1
	fi
fi
exit 0


