#!/usr/local/bin/perl
#
#NAME
#  Prev - produce the previous email message
#
#SYNOPSIS
#  Prev [+folder] [index]
#
#REQUIRES
#
	$LNAM = $ENV{'LOGNAME'} || $ENV{'USER'};
	$HOME = $ENV{'HOME'} || "/home/$LNAM";
	@INC = ($HOME, "$HOME/mh","$HOME/sh",@INC);
	require 'jcmh.pm';
#
#DESCRIPTION
#  This lists the previous message from an mh-style directory.
#
#OPTIONS
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>

$| = 1;

for $a (@ARGV) {
	print "$0: Arg \"$a\"\n" if $V>2;
	if ($a =~ /^\+(\w+)$/) {
		$FLDR = $1;
		print "Folder \"$FLDR\" from command line.\n" if $V>2;
	} elsif ($a =~ /^\d+$/) {
		$ndx = $a;
	} else {
		print STDERR "$P: Unrecognized arg \"$a\" ignored.\n";
	}
}
print "Folder \"$FLDR\"\n" if $V>1;

unless ($ndx) {
	$ndx = &getindex($MDIR,$FLDR,1);
	print "Current index in $FDIR is $ndx.\n" if $V>1;
}
$hdrs = "^(Subj|From|To|Repl|Date|Cc|X-)";

file:
for ($ndx--; $ndx > 0; $ndx--) {
	unless (open(MSG,"$FDIR/$ndx")) {
		printf "$FDIR/%d doesn't exist.\n",$ndx if $V>2;
		next file;
	}
	print "$FDIR/$ndx\n" if $V>0;
	&setindex($MDIR,$FLDR,$ndx);
	$from = $subj = $date = '';
line:
	while ($l = <MSG>) {
		$l =~ s/[\s\r]+$//;	# Trim away white stuff
		if ($l eq '') {		# Null line ends headers
			print "\n";
			last line;
		} elsif ($l =~ /$hdrs/i) {
			print "$l\n";
		}
	}
	while ($l = <MSG>) {print $l}
	exit 0;
}
print STDERR "No previous message found.\n" if $V>1;

exit 1;
