From: moore@cs.utk.edu (Keith Moore)
Newsgroups: alt.hackers
Subject: MIME-compatible SHAR program
Date: 10 Sep 92 04:26:40 GMT
Organization: Univ. of Tenn. Computer Science, Knoxville
Distribution: world
Reply-To: moore@cs.utk.edu
NNTP-Posting-Host: wilma.cs.utk.edu
Content-type: multipart/mixed ; boundary = "MIME-shar-964"
MIME-Version: 1.0

Here is a version of SHAR that generates MIME messages.  A MIME mail
reader can understand these and extract them.  If the reader lacks a
MIME mail reader, the header can be stripped off and the result fed to
the Bourne shell like any other SHAR file.  Obviously this is pretty
rudimentary, but I wanted to see how it could be done.

(MIME really needs an application/text content-type...)

Teaching awk or sed to understand base64 and quoted-printable are
left as exercises to the reader...

#!/bin/sh
#
# this is a MIME-compatible .shar file.  If you have a MIME-smart
# mail reader, it will offer to extract files for you.  Otherwise,
# you can treat this message as any other .shar file -- strip off
# the message headers (everything before the #!/bin/sh line above)
# and feed them as input to /bin/sh -- to extract the files.
#
awk 'BEGIN {
	state=0;
}
/^--MIME-shar-964-- *$/ && (state == 2) {
	printf ("EndOfFile\n");
	printf ("exit 0\n");
	next;
}
/^--MIME-shar-964 *$/ && (state == 0 || state == 2) {
	if (state == 2) 
		printf ("EndOfFile\n");
	filename=""
	state = 1;
	next;
}
/^ *$/ && (state == 1) {
	printf ("sed -e '"'"'s/^X//'"'"' << \"EndOfFile\" > %s\n", filename);
	state = 2;
	next;
}
/^Content-type: application\/octet-stream; name=/ && (state == 1) {
	split($0,a,"\"");
	filename=a[2];
	next;
}
{ if (state == 2) { printf("X%s\n", $0); }}' << "EndOfAwkInput"
--MIME-shar-964
Content-type: application/octet-stream; name="mime-shar"

#!/bin/sh
#
# mime-shar version 1.0
# Keith Moore
# Copyright (c) 1992 Keith Moore
#
# This program generates a MIME message which contains one or more files.
# The MIME message is also a shell script; the headers can be removed and
# the result fed to a UNIX shell for extraction.
#
case $# in
	0) echo 'usage: '$0' filename...' ; exit ;;
esac
BOUNDARY=MIME-shar-$$
sed -e 's/BOUNDARY/'"$BOUNDARY"'/' << "EndOfSedInput"
Content-type: multipart/mixed ; boundary = "BOUNDARY"
MIME-Version: 1.0


#!/bin/sh
#
# this is a MIME-compatible .shar file.  If you have a MIME-smart
# mail reader, it will offer to extract files for you.  Otherwise,
# you can treat this message as any other .shar file -- strip off
# the message headers (everything before the #!/bin/sh line above)
# and feed them as input to /bin/sh -- to extract the files.
#
awk 'BEGIN {
	state=0;
}
/^--BOUNDARY-- *$/ && (state == 2) {
	printf ("EndOfFile\n");
	printf ("exit 0\n");
	next;
}
/^--BOUNDARY *$/ && (state == 0 || state == 2) {
	if (state == 2) 
		printf ("EndOfFile\n");
	filename=""
	state = 1;
	next;
}
/^ *$/ && (state == 1) {
	printf ("sed -e '"'"'s/^X//'"'"' << \"EndOfFile\" > %s\n", filename);
	state = 2;
	next;
}
/^Content-type: application\/octet-stream; name=/ && (state == 1) {
	split($0,a,"\"");
	filename=a[2];
	next;
}
{ if (state == 2) { printf("X%s\n", $0); }}' << "EndOfAwkInput"
EndOfSedInput
for file in $* ; do
	echo '--'$BOUNDARY
	echo 'Content-type: application/octet-stream; name="'$file'"'
	echo ''
	cat $file
done
echo '--'$BOUNDARY'--'
echo 'EndOfAwkInput'
echo 'exit 0'

--MIME-shar-964--
EndOfAwkInput
exit 0


--
Keith Moore / U.Tenn CS Dept / 107 Ayres Hall / Knoxville TN  37996-1301
Internet: moore@cs.utk.edu      BITNET: moore@utkvx
Newsgroups: alt.hackers