#! /bin/sh
# $Header: /u3/acs/jdpeek/.bin/RCS.Z/vmsmail2mh,v 1.2 90/03/22 06:53:59 jdpeek Exp $
###	vmsmail2mh - split VMS mail messages from EXTRACT/ALL into MH folder
###	Usage: vmsmail2mh [+folder] [...vmsmsg-files]
##
##	AN EASY WAY TO TRANSFER VMS MAIL MESSAGES TO UNIX IS BY EXTRACTING
##	ALL THE MESSAGES FROM A VMS MAIL FOLDER INTO A FILE... USE:
##		MAIL> EXTRACT/ALL
##		_file: FILENAME
##	THEN, YOU CAN ftp THEM TO THE SYSTEM RUNNING MH.
##
##	WHEN YOU GET THEM TO THE MH SYSTEM, vmsmail2mh WILL READ THROUGH
##	THE FILE (OR STANDARD INPUT, IF YOU DON'T GIVE A FILENAME):
##		- THE MESSAGES ARE SEPARATED BY FORMFEEDS (CONTROL-L).
##		  vmsmail2mh WILL CUT THEM INTO SEPARATE MH MESSAGES
##		  IN THE CURRENT FOLDER (OR +folder IF YOU GIVE ONE).
##		- THE FORMAT OF THE VMS MAIL MESSAGE IS DIFFERENT.
##			* Subj: LINES ARE CONVERTED TO Subject: LINES.
##			* From: LINES ARE SPLIT INTO From: AND Date: LINES.
##		  ONLY THE FIRST Subj: AND From: IN EACH MESSAGE IS CONVERTED.
##
##	BUG: VMS ADDRESSES LIKE:
##		SMTP%"user@host"
##		HOST::USER
##	AREN'T CONVERTED.  IN FACT, *NO* ADDRESSES ARE CONVERTED.

#	NOTE: PROGRAM HASN'T BEEN TESTED THOROUGHLY.
# 	CHECK THE MESSAGES!
#	Placed in the public domain.  Use at your own risk.
#		--Jerry Peek, 22 March 1990

dfltprot=600	# DEFAULT MESSAGE PROTECTION (IF NONE IN MH PROFILE)
folopts="-fast -nolist -nototal -nopack -norecurse"
mh=/usr/local/mh
mhprofile=/u3/acs/jdpeek/.bin/mhprofile	# READS MH PROFILE
scanopts="-noclear -noheader -noreverse"

case "$1" in
[@+]?*)
	# IF $1 DOESN'T EXIST, folder WILL CREATE IT (SIGH).
	# THAT'S BECAUSE stdout IS REDIRECTED AWAY FROM TERMINAL.
	if $mh/folder $folopts "$1" >/dev/null
	then
		# GET PATHNAME OF FOLDER, LAST MESSAGE NUMBER:
		folpath="`$mh/mhpath`" || exit
		firstmsg="`$mh/scan -format '%(msg)' last`" || exit
	else
		echo "`basename $0`: no folder.  quitting." 1>&2
		exit 1
	fi
	;;
*)	echo "Usage: `basename $0` +folder|@folder [file]
	('$1' doesn't start with + or @.)" 1>&2
	exit 1
	;;
esac

if [ -n "$2" -a \( ! -r "$2" \) ]
then
	echo "`basename $0`: quitting: can't read VMS file '$2'." 1>&2
	exit 1
fi

# GET PROTECTION MODE FROM MH PROFILE (IF NONE, USE $dfltprot):
msgprot="`$mhprofile -b msg-protect`" || msgprot=$dfltprot

# MAKE SHELL ARCHIVE FILE ON awk'S STANDARD OUTPUT.
# PIPE IT INTO sh TO CREATE THE MESSAGE FILES...
awk "BEGIN {
	folpath=\"$folpath\" # STORE AS STRING, WITH QUOTES
	msgprot=$msgprot     # STORE AS NUMBER (NO QUOTES)
	msgnum=$firstmsg"'   # CHANGE FROM DOUBLE- TO SINGLE-QUOTES
	inmsg = 0
}
# PROCESS MESSAGES.  EACH MESSAGE STARTS WITH CONTROL-L.
# HOPE THERE ARE NO LINES WITH JUST A CONTROL-L IN MESSAGES!
{
	# MESSAGE STARTS WITH CTRL-L ON A LINE BY ITSELF.
	# PRINT SHELL COMMANDS AND RESET FLAGS:
	if ($0 ~ /^\014$/ && inmsg == 1) {
		printf "END-OF-%s/%d\n", folpath, msgnum
		# SET PROTECTION (INEFFICIENT; SHOULD DO ALL MSGS. AT ONCE)
		printf "chmod %d %s/%d\n\n", msgprot, folpath, msgnum
		inmsg = 0
	}
	# AT START OF MESSAGE, PRINT START OF ARCHIVE AND SET FLAGS:
	if (inmsg == 0) {
		inmsg = 1
		msgnum += 1
		printf "/bin/cat > %s/%d << \\END-OF-%s/%d\n", \
			folpath, msgnum, folpath, msgnum
		didsubj = 0
		didfrom = 0
		next
	}
	# TURN Subj: INTO Subject: AND SET FLAG TO MAKE SURE WE DO NOT
	# TRASH ANY OTHER Subj: LINES (LIKE IN FORWARDED MESSAGES).
	# STRIP DATE (14-MAR-1990 15:31:12.02) OFF END OF THE From: LINE
	# AND MOVE IT TO LINE OF ITS OWN, THEN SET FLAG LIKE ABOVE:
	if ($1 ~ /^Subj:/ && didsubj == 0) {
		$1 = "Subject:"
		didsubj = 1
		print
	}
	else if ($1 ~ /^From:/ && didfrom == 0) {
		for (i = 1; i <= NF - 3; i++)
			printf "%s ", $i
		printf "%s\n", $(NF - 2)
		printf "Date: %s %s\n", $(NF - 1), $NF
		didfrom = 1
	}
	else
		print
}' $2 |		# IF NO $2, WILL READ STANDARD INPUT...
/bin/sh -e	# EXIT ON ANY ERROR

exit # RETURN STATUS FROM PIPE ABOVE
