#!/bin/csh -fb
# (The "-fb" might need to be changed to "-f" on some systems)
#

if ($#argv < 1) then
    echo "Usage:  extcompose output-file-name"
    exit 1
endif
set OUTFNAME=$1

chooseaccesstype:
echo ""
echo "Where is the external data that you want this mail message to reference?"
echo "    1 -- In a local file"
echo "    2 -- In an AFS file"
echo "    3 -- In an anonymous FTP directory on the Internet"
echo "    4 -- In an Internet FTP directory that requires a valid login"
echo "    5 -- Under the control of a mail server that will send the data on request"
echo ""
echo -n "Please enter a number from 1 to 5: "
set ans=$<
if (${ans:q} == 1)  then
    set accesstype=local-file
else if (${ans:q} == 2) then
    set accesstype=afs
else if (${ans:q} == 3) then
    set accesstype=anon-ftp
else if (${ans:q} == 4) then
    set accesstype=ftp
else if (${ans:q} == 5) then
    set accesstype=mail-server
else
    echo "That is NOT one of your choices."
    goto chooseaccesstype
endif
if (${accesstype:q} == "ftp" || ${accesstype:q} == "anon-ftp") then
    echo -n "Enter the full Internet domain name of the FTP site: "
    set site=$<
    echo -n "Enter the name of the directory containing the file (RETURN for top-level): "
    set directory=$<
    echo -n "Enter the name of the file itself: "
    set name = $<
    echo -n "Enter the transfer mode (type 'image' for binary data, RETURN otherwise): "
    set mode = $<
    if (${mode:q} == "") set mode=ascii
    echo "Content-type: message/external-body; access-type=${accesstype:q}; name="\"${name:q}\"\; > ${OUTFNAME:q}
    echo -n "    site="\"${site:q}\" >> ${OUTFNAME:q}
    if (${directory:q} != "") echo -n "; directory="\"${directory:q}\">> ${OUTFNAME:q}
    if (${mode:q} != "") echo -n "; mode="\"${mode:q}\">> ${OUTFNAME:q}
    echo "">> ${OUTFNAME:q}
else if (${accesstype:q} == "local-file" || ${accesstype:q} == "afs") then
fname:
    echo -n "Enter the full path name for the file: "
    set name = $<
    if (! -e ${name:q}) then
	echo "The file ${name:q} does not seem to exist."
	goto fname
    endif
    echo "Content-type: message/external-body; access-type=${accesstype:q}; name="\"${name:q}\"> ${OUTFNAME:q}
else if (${accesstype:q} == "mail-server") then
    echo -n "Enter the full email address for the mailserver: "
    set server=$<
    echo "Content-type: message/external-body; access-type=${accesstype:q}; server="\"${server:q}\"> ${OUTFNAME:q}
else
    echo accesstype ${accesstype:q} not yet implemented
    goto chooseaccesstype
endif

echo -n "Please enter the MIME content-type for the externally referenced data: "
set ctype = $<
getcenc:
echo "Is this data already encoded for email transport?"
echo "  1 -- No, it is not encoded"
echo "  2 -- Yes, it is encoded in base64"
echo "  3 -- Yes, it is encoded in quoted-printable"
echo "  4 -- Yes, it is encoded using uuencode"
set encode=$<
switch (${encode:q})
    case 1:
        set cenc=""
        breaksw
    case 2:
        set cenc="base64"
        breaksw
    case 3:
        set cenc="quoted-printable"
        breaksw
    case 4:
        set cenc="x-uue"
        breaksw
    default:
        echo "That is not one of your choices."
        goto getcenc
endsw
echo "" >> ${OUTFNAME:q}
echo "Content-type: " ${ctype:q} >> ${OUTFNAME:q}
if (${cenc:q} != "") echo "Content-transfer-encoding: " ${cenc:q} >> ${OUTFNAME:q}
echo "" >> ${OUTFNAME:q}
if (${accesstype:q} == "mail-server") then
    echo "Please enter all the data to be sent to the mailserver in the message body, "
    echo "ending with ^D or your usual end-of-data character:"
    cat >> ${OUTFNAME:q}
endif
