#!/bin/sh

#################################################################
#								#
# You may want to set your E-mail return PATH,  For example:	#
# EMAIL="enrico_rastelli@juggling.org"				#
EMAIL=""							#
#								#
# You probably won't need to change this, but you might:	#
MAIL_PROGRAM="mail"						#
#								#
# Install this file, starting with the "#!/bin/sh" line,	#
# with the name "jis" in some directory in your PATH.		#
# Be sure to make it executable, using "chmod +x jis".		#
# You may have to do a "rehash" command before using this	#
# command for the first time.					#
#								#
# The uudecode program is required when retrieving binary data.	#
# Use of the -unpack option to automate unpacking files uses	#
# the perl command.  You must have this available, or you will	#
# have to unpack returned files manually.			#
#								#
#################################################################
#								#
# Usage:							#
#								#
# To retrieve the top level JIS index:				#
#	jis							#
#								#
# To retrieve the Help instructions:				#
#	jis -help						#
#								#
# To retrieve the index of all available files:			#
#	jis -index						#
#								#
# To retrieve files from the JIS:				#
#	jis file-1 file-2 file-3 ...				#
#								#
# To unpack files in returned mail from the JIS:		#
#	jis -unpack mail-message				#
#	cat mail-message | jis -unpack				#
#								#
# Examples:							#
#	jis meetings/no_amer.html				#
#	jis help/numbers/5-balls/				#
#	jis pics/Pics/popovitch.gif pics/Pics/markb86a.jpg	#
#	jis animations/mm.mpg programs/ibm-pc/jp36.zip		#
#	jis rec.juggling/'2tdem1$su9@hijinks.hal.COM'		#
#								#
#################################################################

SUBJECT="FILE-SERVICE request"
MAIL_SERVER="file-service@juggling.org"

if [ $# -eq 0 ]; then
	set index.html
fi

if [ " $1 " = " -unpack " ]; then
	shift
	cat $* | perl -e '
		while (<STDIN>) {
			if (/^begin /) {
				($begin, $mode, $file) = split;
				if ($file =~ m|/$|) {
					$file .= "index.html";
				}
				system("mkdir -p `dirname $file`");
				if ($mode eq "file") {
					print STDERR "file $file\n";
					unlink("$file");
					open(FILE, ">$file");
					while (<STDIN>) {
						if (/^end\n$/) {
							last;
						}
						s/^ ( *end)$/$1/;
						print FILE;
					}
					close(FILE);
				} else {
					print STDERR "uudecode $file\n";
					open(FILE, "|uudecode");
					print FILE;
					while (<STDIN>) {
						print FILE;
						if (/^end\n$/) {
							last;
						}
					}
					close(FILE);
				}
			}
		}
	'
	exit 0
fi

(
	echo "# request via $0 command"
	if [ -n "$EMAIL" ]; then
		echo "PATH $EMAIL"
	fi
	for arg do
		case "$arg" in
		-help)	echo HELP				;;
		-index)	echo INDEX				;;
		-*)	echo 1>&2 "bad flag: $arg"; exit 1	;;
		*)	echo "SEND $arg"			;;
		esac
	done
) | $MAIL_PROGRAM -s "$SUBJECT" "$MAIL_SERVER"
exit 0
