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

onintr cleanup

set hname = `hostname`
set idctr=0
set MustDelete=0
set batchmode=0
set multipart = 1
if ($?SPLITSIZE) then
    @ splitsize = ${SPLITSIZE}
else
    @ splitsize = 100000
endif
set outputfile = ""
set multipartsubtype = "mixed"
set cdescrip = "An object packed by metasend"
set isentity=0
set newid=`date | tr " :" "__"`
set cid="<${newid:q}_${idctr:q}@${hname:q}>"
@ idctr++

if (! $?METAMAIL_TMPDIR) then
    set METAMAIL_TMPDIR=/tmp
endif
if ( ! -e ${METAMAIL_TMPDIR:q} ) then
    echo "${METAMAIL_TMPDIR} does not exist"
    exit -1
else if ( ! -d ${METAMAIL_TMPDIR:q} ) then
    echo "${METAMAIL_TMPDIR} is not a directory"
    exit -1
endif
set TEMPDIR = "${METAMAIL_TMPDIR}/metasend.$$"
if ( -e ${TEMPDIR:q} ) then
    echo "${TEMPDIR} already exists"
    exit -1
endif
mkdir -m 0700 ${TEMPDIR:q} || exit 1

while ($#argv > 0)
    switch (${1:q})
        case "-S":
            shift
            if ($#argv == 0) then
                echo "-S requires a following argument, the SPLIT threshhold"
                exit -1
            endif
            @ splitsize = ${1:q}
            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 -1
            endif
            set cc=${1:q}
            shift
            breaksw
        case "-i":
            shift
            if ($#argv == 0) then
                echo "-i requires a following argument, the Content-ID value"
                exit -1
            endif
            set cid=${1:q}
            shift
            breaksw
        case "-I":
            shift
            if ($#argv == 0) then
                echo "-I requires a following argument, the multipart Content-ID value"
                exit -1
            endif
            set multicid=${1:q}
            shift
            breaksw
        case "-s":
            shift
            if ($#argv == 0) then
                echo "-s requires a following argument, the SUBJECT"
                exit -1
            endif
            set subject=${1:q}
            shift
            breaksw
        case "-t":
            shift
            if ($#argv == 0) then
                echo "-t requires a following argument, the TO address"
                exit -1
            endif
            set to=${1:q}
            shift
            breaksw
        case "-F":
            shift
            if ($#argv == 0) then
                echo "-F requires a following argument, the FROM address"
                exit -1
            endif
            set from=${1:q}
            shift
            breaksw
        case "-P":
            shift
            if ($#argv == 0) then
                echo "-P requires a following argument, the preamble file name"
                exit -1
            endif
            set preamblefile=${1:q}
            shift
            breaksw
        case "-e":
            shift
            if ($#argv == 0) then
                echo "-e requires a following argument, the ENCODING value"
                exit -1
            endif
            set encode=${1:q}
            if (${encode:q} == "base64") then
		set encodingprog = "mimencode -b"
            else if (${encode:q} == "x-uue") then
	        set encodingprog = "uuencode mail-body"
            else if (${encode:q} == "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 -1
            endif
            set datafile=${1:q}
            if (! -e  ${datafile}) then
                echo "The file ${datafile} does not exist."
                exit -1
            endif
            shift
            breaksw
        case "-m":
            shift
            if ($#argv == 0) then
                echo "-m requires a following argument, the MIME CONTENT-TYPE"
                exit -1
            endif
#            set ctyperaw = ${1:q}
#            set  ctype=`echo ${ctyperaw:q} | tr \" \'`
	set ctype = ${1:q}
	if (`echo ${ctype:q} | grep /` == "") then
	    echo "Invalid content-type specified"
	    exit -1
	endif
            shift
            breaksw
        case "-o":
            shift
            if ($#argv == 0) then
                echo "-o requires a following argument, the output file name"
                exit -1
            endif
            set outputfile=${1:q}
            shift
            breaksw
        case "-/":
            shift
            if ($#argv == 0) then
                echo "-/ requires a following argument, the multipart subtype"
                exit -1
            endif
            set multipartsubtype=${1:q}
            shift
            breaksw
        case "-D":
            shift
            if ($#argv == 0) then
                echo "-D requires a following argument, the Content-Description value"
                exit -1
            endif
            set cdescrip=${1:q}
            shift
            breaksw
        case "-E":
            set isentity=1
            shift
            breaksw
        case "-z":
            set MustDelete=1
            shift
            breaksw
        case "-n":
            if (${isentity:q}) then
              if (! $?encode) then 
                set encode=7bit
                set encodingprog=cat
              endif
              if (! $?ctype) then
                  set ctype=bogus
                  # Never used
               endif
	if ( ! $?datafile) then
	    echo One part is incomplete -- each part needs at least -f and -E
	    exit -1
	endif
            else 
	if ( ! $?ctype || ! $?datafile) then
	    echo One part is incomplete -- each part needs at least -f and -m
	    exit -1
	endif
            endif
	if (! $?encode) then
	        if (${ctype:q} =~ text*) then
	              set encodingprog = "mimencode -q"
		set encode = "quoted-printable"
	        else
	            set encode = base64
	            set encodingprog = "mimencode -b"
	        endif
	endif
	set ctype${multipart} = ${ctype:q}
	set datafile${multipart} = ${datafile:q}
	set encode${multipart} = ${encode:q}
	set cdescrip${multipart} = ${cdescrip:q}
	set cid${multipart} = ${cid:q}
	set isentity${multipart} = ${isentity:q}
	set encodingprog${multipart} = ${encodingprog:q}
	unset ctype
	unset datafile
	unset encode
	unset encodingprog
	set newid=`date | tr " :" "__"`
	set cid="<${newid:q}_${idctr:q}@${hname:q}>"
	@ idctr++
	set cdescrip = "An object packed by metasend"
	set isentity=0
            @  multipart++
	shift
	breaksw
        default:
            echo "UNRECOGNIZED METASEND OPTION: ${1}"
            exit -1
    endsw
end
if (${batchmode:q} == 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 (`echo ${ctype:q} | grep /` == "") then
        echo "Invalid content-type specified"
        unset ctype
        goto anotherpart
    endif
#    if (${ctype:q} != ${ctyperaw:q})  "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:q})
            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:q} =~ y*) then
	set ctype${multipart} = ${ctype:q}
	set datafile${multipart} = ${datafile:q}
	set encode${multipart} = ${encode:q}
	set encodingprog${multipart} = ${encodingprog:q}
	set cdescrip${multipart} = ${cdescrip:q}
	set cid${multipart} = ${cid:q}
	set isentity${multipart} = ${isentity:q}
	set cdescrip = "An object packed by metasend"
	set isentity=0
	unset ctype
	unset datafile
	unset encode
	unset encodingprog
	set newid=`date | tr " :" "__"`
	set cid="<${newid:q}_${idctr}:q@${hname:q}>"
	@ idctr++
            @  multipart++
            goto anotherpart
     endif
else
    if (${outputfile:q} == "") then
      if (! $?to || ! $?subject || ! $?ctype || ! $?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
      endif
    else
      if (! $?datafile) then
        echo metasend:  in batch mode, -f is required.
      endif
      if (${isentity:q}) then
        if (! $?ctype) then
          set ctype=bogus
          # will not be used anyway really
        endif
      endif
      if (! $?ctype  ) then
        echo metasend: in batch mode, with output directed to a file, -E or -m is required
        exit -1
      endif
    endif    
    if (! -e  ${datafile}) then
        echo "metasend: The file ${datafile} does not exist"
        exit -1
    endif
#    if (! $?cc) set cc=""
#     if (${ctype:q} != ${ctyperaw:q})  echo "Double  quotes not allowed, using ${ctype:q}"
    if (! $?encode) then
        if (${ctype:q} =~ text*) then
	set encodingprog = "mimencode -q"
	set encode = "quoted-printable"
        else
            set encodingprog = "mimencode -b"
            set encode = base64
        endif
    else if (${encode:q} == "base64") then
        set encodingprog = "mimencode -b"
    else if (${encode:q} == "x-uue") then
        set encodingprog = "uuencode mail-body"
    else if (${encode:q} == "7bit") then
        set encodingprog = cat
    else
        set encodingprog = "mimencode -q"
        set encode = "quoted-printable"
    endif
endif

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

set fname = ${TEMPDIR:q}/metasend
echo "MIME-Version: 1.0" > ${fname:q}
if ($?to) then
    echo "To: ${to:q}" >> ${fname:q}
endif
if ($?subject) then
    echo "Subject: ${subject}" >> ${fname:q}
endif
if ($?cc) then
    echo "CC: ${cc:q}" >> ${fname:q}
endif
if ($?from) then
    echo "From: ${from:q}" >> ${fname:q}
endif
if (${multipart:q} > 1) then
    set  boundary = ${hname:q}.$$.`date | tr "  " .`
    set newid=`date | tr " :" "__"`
    if ($?multicid) then
        echo "Content-ID: ${multicid:q}"   >> ${fname:q}
    else
        echo "Content-ID: <${newid:q}_${idctr:q}@${hname:q}>"   >> ${fname:q}
        @ idctr++
    endif
    echo "Content-type: multipart/${multipartsubtype:q};" >> ${fname:q}
    echo "	boundary="\"${boundary:q}\" >> ${fname:q}
    echo "" >> ${fname:q}
    if ($?preamblefile) then
        cat ${preamblefile} >> ${fname:q}
    else
        echo "This is  a multimedia message in MIME  format.  If you are reading this" >> ${fname:q}
        echo "prefix, your mail reader does  not understand MIME.  You may wish" >> ${fname:q}
        echo "to look into upgrading to a newer version of  your mail reader." >> ${fname:q}
    endif
    set i=1
    while  (${multipart:q} >= ${i:q})
	echo "" >> ${fname:q}
	echo "--${boundary}" >> ${fname:q}
	eval set isentity=\${isentity${i}:q}
	if (! ${isentity:q}) then
	    echo -n "Content-ID: "   >> ${fname:q}
	    eval echo \${cid${i}:q} >> ${fname:q}
	    echo -n "Content-type: ">>${fname:q}
	    eval echo \${ctype${i}:q} >> ${fname:q}
	    eval echo "Content-Description:" \${cdescrip${i}:q} >> ${fname:q}
	    eval echo "Content-Transfer-Encoding:" \${encode${i}} >> ${fname:q}
	    echo ""  >> ${fname:q}
	endif
	eval \${encodingprog${i}} \${datafile${i}} >> ${fname:q}
	@ i++
    end
    echo "" >> ${fname:q}
    echo --${boundary}-- >> ${fname:q}
    echo "" >> ${fname:q}
else 
    if (! ${isentity:q}) then
        echo "Content-ID: ${cid}"   >> ${fname:q}
        echo "Content-type: ${ctype}" >> ${fname:q}
        echo "Content-Description: ${cdescrip}" >> ${fname:q}
        echo "Content-Transfer-Encoding: ${encode}" >> ${fname:q}
    endif
    echo  "" >> ${fname:q}
    ${encodingprog} ${datafile} >> ${fname:q}
    # Ensure last line has trailing carriage return
    echo "" >> ${fname:q}
endif
if (${outputfile:q} == "") then
    if (${batchmode:q} == 0) echo -n "Delivering mail, please wait...  "
    splitmail -s ${splitsize} -d ${fname:q}
    if (! ${status}) then
        if (${batchmode:q} == 0) echo "Mail delivery apparently succeeded."
    else if (${MustDelete:q} == 1) then
        echo "Mail delivery failed"
        goto cleanup
    else
        echo "Mail delivery failed, draft mail is in ${fname}"
        exit -1
    endif
else
    mv ${fname:q} ${outputfile}
    if (${status:q}) goto cleanup
endif

rm -rf ${TEMPDIR:q}
exit 0

cleanup:
    rm -rf ${TEMPDIR:q}
    exit -1
