#!/bin/sh -e
# This file 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.
#
# Hacked December 1998 by Keith Moore to support URL access type per RFC 2017
# the hack requires awk, and the pathname of awk is supplied by GNU configure.
# also hacked to use "echo -n foo" or "echo foo\c" as required.
#
if [ $# -lt 1 ]
then
    echo "Usage:  $0 output-file-name" 1>&2
    exit 1
fi
OUTFNAME=$1

case `echo -n "" | wc -c | sed -e 's/ //g'` in
	0) ep="-n"; es="";;
	*) ep=""; es="\\c";;
esac

choosing=yes
while [ $choosing = yes ]
do
    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 external file referenced by a URL"
    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 "    6 -- In an AFS file"
    echo ""
    echo $ep "Please enter a number from 1 to 5: $es"
    read ans
    case "$ans" in
        1) accesstype=local-file ;;
	2) accesstype=url ;;
	3) accesstype=anon-ftp ;;
	4) accesstype=ftp ;;
	5) accesstype=mail-server ;;
	6) accesstype=afs;;
	* ) echo "That is NOT one of your choices." 1>&2; continue ;;
    esac

    case "$accesstype" in
        ftp | anon-ftp )
	    echo $ep "Enter the full Internet domain name of the FTP site: $es"
	    read site
	    echo $ep "Enter the name of the directory containing the file (RETURN for top-level): $es"
	    read directory
	    echo $ep "Enter the name of the file itself: $es"
	    read name
	    echo $ep "Enter the transfer mode (type 'image' for binary data, RETURN otherwise): $es"
	    read mode
	    if [ -n "$mode" ]
	    then mode=ascii
	    fi
	    echo "Content-type: message/external-body; access-type=$accesstype; name="\"$name\"\; > $OUTFNAME
	    echo $ep "    site=\"$site\"$es" >> $OUTFNAME
	    if [ -n "$directory" ]
	    then echo $ep "; directory=\"$directory\"$es" >> $OUTFNAME
	    fi
	    echo $ep "; mode=\"$mode\"$es" >> $OUTFNAME
	    echo "" >> $OUTFNAME
	    choosing=no
	    ;;

	url )
	    echo $ep "Enter the URL for the file: $es"
	    read url
#
# RFC 2017 requires that SPACEs, CTLs, double quotes, blackslashes
# and 8-bit characters be precent-encoded, and that the URL be split
# up into chunks of 40 or fewer characters, the chunks separated by
# white space.  the following awk script implements that protocol.
#
	    echo "$url" | /bin/nawk '
BEGIN {
        for (i = 0; i < 256; ++i) {
                enc[sprintf("%c", i)] = sprintf ("%%%02X", i);
        }
        for (i = 33; i < 128; ++i) {
                if (i != 0x22 && i != 0x5c) {
                        enc[sprintf("%c", i)] = sprintf ("%c", i);
                }
        }
        printf ("Content-Type: message/external-body; access-type=URL;\n");
	printf (" url=\""); 
} 
{ 
        c = 0;
        for (i=1; i<=length($0); ++i) {
                x=substr($0,i,1);
                if (x == "%") { 
                        if (c + 3 >= 40 ) {
                                printf (" ");
                                c = 0;
                        }
                        printf ("%s", substr($0,i,3)); 
                        c = c + 2;
                        i = i + 2;
                }
                else {
                        if (c + length (enc[x]) >= 40) {
                                printf (" ");
                                c = 0;
                        }
                        printf ("%s", enc[x]);
                        c = c + length (enc[x]);
                } 
        } 
}
END { printf ("\"\n") }' > $OUTFNAME 
	choosing=no
	;;

	local-file | afs )
	    name=
	    while [ -z "$name" ]
	    do
	        echo -n "Enter the full path name for the file: "
		read name
		if [ ! -f "$name" ]
		then
		    echo "The file $name does not seem to exist."
		    name=
		fi
	    done
	    echo "Content-type: message/external-body; access-type=$accesstype; name="\"$name\"> $OUTFNAME
	    choosing=no
	    ;;
	
	mail-server )
	    echo -n "Enter the full email address for the mailserver: "
	    read server
	    echo "Content-type: message/external-body; access-type=$accesstype; server="\"$server\"> $OUTFNAME
	    choosing=no
	    ;;
	
	* )
	    echo accesstype $accesstype not yet implemented
	    ;;
    esac
done

echo $ep "Please enter the MIME content-type for the externally referenced data: $es"
read ctype

choosing=yes
while [ $choosing = yes ]
do
    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"
    read encode
    case "$encode" in
	1 ) cenc="" choosing=no ;;
	2 ) cenc="base64" choosing=no ;;
	3 ) cenc="quoted-printable" choosing=no ;;
	4 ) cenc="x-uue" choosing=no ;;
	* ) echo "That is not one of your choices." ;;
    esac
done

echo "" >> $OUTFNAME
echo "Content-type: " $ctype >> $OUTFNAME
if [ -n "$cenc" ]
then echo "Content-transfer-encoding: " $cenc >> $OUTFNAME
fi
echo "" >> $OUTFNAME
if [ "$accesstype" = "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
fi
