#!/bin/csh -f
# 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 idctr=0
set MustDelete=0
set batchmode=0
set multipart = 1
set splitsize = 100000
while ($#argv > 0)
    switch ("$1")
        case "-S":
            shift
            if ($#argv == 0) then
                echo "-S requires a following argument, the SPLIT threshhold"
                exit
            endif
            set splitsize=$1
            shift
            breaksw
        case "-b":
            # Batch mode -- ALL args must be on command line
            set batchmode = 1
            shift
            breaksw
        case "-c":
            shift
            if ($#argv == 0) then
                echo "-c requires a following argument, the CC address"
                exit
            endif
            set cc=$1
            shift
            breaksw
        case "-s":
            shift
            if ($#argv == 0) then
                echo "-s requires a following argument, the SUBJECT"
                exit
            endif
            set subject="$1"
            shift
            breaksw
        case "-t":
            shift
            if ($#argv == 0) then
                echo "-t requires a following argument, the TO address"
                exit
            endif
            set to="$1"
            shift
            breaksw
        case "-e":
            shift
            if ($#argv == 0) then
                echo "-e requires a following argument, the ENCODING value"
                exit
            endif
            set encode=$1
            if ($encode == "base64") then
	    set encodingprog = "mimencode -b"
            else if ($encode == "x-uue") then
	        set encodingprog = "uuencode mail-body"
            else if ($encode == "7bit") then
	        set encodingprog = cat
           else
	        set encodingprog = "mimencode -q"
	        set encode = "quoted-printable"
           endif 
           shift
            breaksw
        case "-f":
            shift
            if ($#argv == 0) then
                echo "-f requires a following argument, the DATA FILE"
                exit
            endif
            set datafile=$1
            if (! -e  $datafile) then
                echo The file $datafile does not exist.
                exit
            endif
            shift
            breaksw
        case "-m":
            shift
            if ($#argv == 0) then
                echo "-m requires a following argument, the MIME CONTENT-TYPE"
                exit
            endif
#            set ctyperaw = $1
#            set  ctype=`echo $ctyperaw | tr \" \'`
	set ctype = $1
            shift
            breaksw
        case "-z":
            set MustDelete=1
            shift
            breaksw
        case "-n":
	if ( ! $?ctype || ! $?datafile) then
	    echo One part is incomplete -- each part needs at least -f and -m
	    exit 1
	endif
	if (! $?encode) then
	        if ("$ctype" =~ text*) then
	              set encodingprog = "mimencode -q"
		set encode = "quoted-printable"
	        else
	            set encode = base64
	            set encodingprog = "mimencode -b"
	        endif
	endif
	set ctype${multipart} = $ctype
	set datafile${multipart} = $datafile
	set encode${multipart} = $encode
	set encodingprog${multipart} = "$encodingprog"
	unset ctype
	unset datafile
	unset encode
	unset encodingprog
            @  multipart++
	shift
	breaksw
        default:
            echo UNRECOGNIZED METASEND OPTION: $1
            exit
    endsw
end
if ($batchmode == 0) then
    if (! $?to) then
        echo -n "To: "
        set to = $<
    endif
    if (! $?subject) then
        echo -n "Subject: "
        set subject = $<
    endif
    if (! $?cc) then
        echo -n "CC: "
        set cc = $<
    endif
anotherpart:
    if (! $?ctype) then
        echo -n "Content-type: "
#        set ctyperaw = $<
#        set  ctype=`echo $ctyperaw | tr \" \'`
        set ctype = $<
    endif
#    if ($ctype != $ctyperaw)  echo Double  quotes not allowed, using $ctype
    if (! $?datafile) then
        getfile:
        echo -n "Name of file containing $ctype data: "
        set datafile = $<
        if (! -e  $datafile) then
            echo The file $datafile does not exist.
            goto getfile
        endif
    endif
    if (! $?encode) then
        retry:
        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)"
        set encode=$<
        switch ("$encode")
            case 1:
                set encodingprog = cat
	  set encode = 7bit
                breaksw
            case 2:
                set encodingprog = "mimencode -b"
	  set encode = base64
                breaksw
            case 3:
                set encodingprog = "mimencode -q"
	  set encode = quoted-printable
                breaksw
            case 4:
                set encodingprog = "uuencode $datafile"
	  set encode = x-uue
                breaksw
            default:
                echo Unrecognized answer, please try again.
                goto retry
        endsw
    endif
    echo -n "Do you want to include another file too (y/n) [n] ? "
    set ans=$<
    if  ("$ans" =~ y*) then
	set ctype${multipart} = $ctype
	set datafile${multipart} = $datafile
	set encode${multipart} = $encode
	set encodingprog${multipart} = "$encodingprog"
	unset ctype
	unset datafile
	unset encode
	unset encodingprog
            @  multipart++
            goto anotherpart
     endif
else
    if (! $?to || ! $?subject || ! $?ctype || ! $?datafile) then
        echo metasend: in batch mode, -t, -s, -f, and -m are all required
        exit
    endif
    if (! -e  $datafile) then
        echo metasend:  The file $datafile does not exist
        exit
    endif
    if (! $?cc) set cc=""
#     if ($ctype != $ctyperaw)  echo Double  quotes not allowed, using $ctype
    if (! $?encode) then
        if ("$ctype" =~ text*) then
	set encodingprog = "mimencode -q"
	set encode = "quoted-printable"
        else
            set encodingprog = "mimencode -b"
            set encode = base64
        endif
    else if ($encode == "base64") then
        set encodingprog = "mimencode -b"
    else if ($encode == "x-uue") then
        set encodingprog = "uuencode mail-body"
    else if ($encode == "7bit") then
        set encodingprog = cat
    else
        set encodingprog = "mimencode -q"
        set encode = "quoted-printable"
    endif
endif

if (! $?encode) then
        if ("$ctype" =~ text*) then
	set encode = "quoted-printable"
	set encodingprog = "mimencode -q"
        else
            set encode = base64
            set encodingprog = "mimencode -b"
        endif
endif
set ctype${multipart} = $ctype
set datafile${multipart} = $datafile
set encode${multipart} = $encode
set encodingprog${multipart} = "$encodingprog"

set fname = /tmp/metasend.$$
echo "To: " "$to" > $fname
echo "Subject: " "$subject" >> $fname
echo "CC: " "$cc" >> $fname
echo "MIME-Version: 1.0" >> $fname
set hname = `hostname`
if ($multipart > 1) then
    set  boundary = ${hname}.$$.`date | tr "  " .`
    set newid=`date | tr " :" "__"`
    echo "Content-ID: <${newid}_$idctr@${hname}>"   >> $fname
    @ idctr++
    echo "Content-type: multipart/mixed;" >> $fname
    echo "	boundary="\"$boundary\" >> $fname
    echo "" >> $fname
    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
    set i=1
    set junkfile=/tmp/mmjunk.$$
    while  ($multipart >= $i)
	echo --$boundary >> $fname
              set newid=`date | tr " :" "__"`
              echo "Content-ID: <${newid}_$idctr@${hname}>"   >> $fname
	@ idctr++
	echo -n Content-type: >>$fname
	echo echo \$ctype$i \>\>  $fname > $junkfile
	source $junkfile 
	echo -n Content-Transfer-Encoding: >>$fname
	echo echo \$encode$i \>\> $fname > $junkfile
	source $junkfile 
	echo ""  >> $fname
	echo \$encodingprog$i \< \$datafile$i \>\> $fname > $junkfile
	source $junkfile 
	@ i++
    end
    rm $junkfile
    echo --${boundary}-- >> $fname
else 
    set newid=`date | tr " :" "__"`
    echo "Content-ID: <${newid}_$idctr@${hname}>"   >> $fname
    @ idctr++
    echo "Content-type: " $ctype >> $fname
    echo "Content-Transfer-Encoding: " $encode >> $fname
    echo  "" >> $fname
    $encodingprog < $datafile >> $fname
    # Ensure last line has trailing carraige return
    echo "" >> $fname
endif
if ($batchmode == 0) echo -n "Delivering mail, please wait...  "
splitmail -s $splitsize -d $fname
if (! $status) then
    if ($batchmode == 0) echo "Mail delivery apparently succeeded."
    rm $fname
else if ($MustDelete == 1) then
    echo Mail delivery failed
    rm $fname
else
    echo Mail delivery failed, draft mail is in $fname
endif

