#! /bin/sh
# $Header: /u3/acs/jdpeek/.bin/RCS/replf,v 1.3 90/10/13 08:31:54 jdpeek book $
###	replf - refile current message into folder, then reply to it
###	Usage: replf +folder [switches for repl]
##
##	DO YOU USE MH FOLDERS A LOT?  YOU PROBABLY inc MAIL INTO YOUR inbox,
##	THEN YOU replY TO SOME MESSAGES AND refile THE ORIGINALS INTO SOME
##	OTHER FOLDER.  THE PROBLEM IS THAT IF YOU USE AN Fcc: LINE TO PUT
##	A COPY OF THE REPLY IN THE DESTINATION FOLDER, IT IS STORED BEFORE
##	THE ORIGINAL MESSAGE.  ALSO, IT'S A PAIN TO refile THE ORIGINAL,
##	THEN TYPE THE SAME NAME ON THE Fcc LINE.  SO, IT'D BE CONVENIENT
##	TO DO ALL THAT IN ONE STEP.  replf LETS YOU DO THAT.
##
##	replf USES refile TO MOVE THE *CURRENT* MESSAGE INTO THE FOLDER YOU
##	NAME.  (IT ONLY WORKS FOR THE CURRENT MESSAGE!)  THEN, IT STARTS
##	repl WITH THE -fcc +folder SWITCH, SO THAT AN Fcc: OF YOUR MESSAGE
##	WILL GO INTO THAT FOLDER, TOO.
##
##	WATCH OUT: THIS VERSION OF replf HAS ONE PROBLEM:  YOU CAN'T USE
##		What now? push 
##	TO SEND THE DRAFT.  THAT'S BECAUSE THE TEMPORARY FILES replf MAKES
##	ARE REMOVED BEFORE THE MESSAGE IS SENT.  USE send INSTEAD.
#
#	Original from Marshall T. Rose and John L. Romine in
#	MH6.6 distribution.  Revised by Jerry Peek, 2/25/90.

context=/tmp/ctx$$	# COPY OF MH context FILE
mh=/usr/local/mh	# WHERE MH COMMANDS LIVE
profile=/tmp/prf$$	# COPY OF .mh_profile FILE
stat=1	# DEFAULT EXIT STATUS; RESET TO 0 BEFORE NORMAL exit

/bin/rm -f $profile $context
trap '/bin/rm -f $profile $context; exit $stat' 0 1 2 15

# PARSE COMMAND LINE:
folder=   switches=
for arg
do
	case "$arg" in
	+*|@*) # IT'S A FOLDER
		case "$folder" in
		"")	folder="$arg" ;;
		*)	echo "`basename $0`: '$arg'?  Only one folder at a time." 1>&2
			exit
			;;
		esac
		;;
	*)	switches="$switches $arg" ;;
	esac
done

case "$folder" in
"")	echo "usage: `basename $0` +folder [switches for repl]" 1>&2
	exit
	;;
esac

# MAKE TEMPORARY context AND .mh_profile FILES.
# THEN, RESET $MH AND $MHCONTEXT UNTIL END OF THIS SCRIPT:
/bin/cp ${MHCONTEXT-`$mh/mhpath +`/context} $context || exit
# READ NEXT TWO LINES, PLUS A COPY OF USER'S STANDARD
# .mh_profile, INTO NEW $profile:
/bin/cat - ${MH-$HOME/.mh_profile} << \END > $profile || exit
MH-Sequences:
Previous-Sequence: pseq
END

MH=$profile MHCONTEXT=$context
export MH MHCONTEXT

# REFILE MESSAGE INTO $folder.
# NEW MESSAGE NUMBER IS IN THE pseq OF $folder.
$mh/refile $folder || exit
$mh/repl $folder pseq -fcc $folder $switches
stat=$?		# EXIT WITH STATUS OF repl
exit
