#!/usr/bin/perl
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  Inc - include new email messages
#  MHunpack - include new email messages
#
#SYNOPSIS
#  Inc [+folder] [file]
#
#REQUIRES
#
	$home = '/u/guests/jc';
	$ENV{'PATH'} = '$home/mh:$home/sh:$home/p/cgi:/bin:/usr/bin:/usr/local/bin:/usr/eecs/bin';
	push @INC, '.',$home,"$home/mh","$home/sh";		# We need jcmh.pm in this list
	require 'jcmh.pm';	# Init and subs for mh package
#
#	$LNAM = $ENV{'LOGNAME'} || $ENV{'USER'} || 'nobody';
#	$HOME = $ENV{'HOME'} || "/home/$LNAM";
#	$MAIL = $ENV{'MAIL'} || "/var/mail/$LNAM";
#
#DESCRIPTION
#  This splits apart a file of email messages into single messages and  files
#  them in the $HOME/Mail/inbox "folder".
#
#  This is a perl re-implementation of the mh "inc" command.  This  was  done
#  partly  so  that  it  may  be  used from a web page via CGI.  The mh "inc"
#  command turns out to have some problems when run from a  web  server,  and
#  there seems to be no way to diagnose the problems successfully.
#
#OPTIONS
#
#  -D  don't delete the mail file
#  +D  delete the mail file after successful unpacking (default).
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$exitstat = 0;
$delmail = 1;	# Whether to delete mail from $MAIL file
$msgs = 0;		# How many messages we find in one file
$msgcnt = 0;	# How many messages we find total
$Refile = 1;	# Should we sort messages into folders?

if ($V>3) {
	print "Environment:\n";
	for $n (sort keys %ENV) {
		$v = $ENV{$n};
		if ($n eq 'HTTP_ACCEPT') {
			$v =~ s",", "g;
		} elsif ($n eq 'PATH') {
			$v =~ s":" "g;
		} else {
		}
		print "$n = \"$v\"\n";
	}
}

for $a (@ARGV) {
	print "$0: Arg \"$a\"\n" if $V>2;
	if (($flg,$arg) = ($a =~ /^([-+])(.*)$/)) {
		while (($a,$x) = ($arg =~ /^(.)(.*)/)) {
			$a = uc($a);
			if ($a eq 'D') {
				$delmail = ($flg eq '+') ? 1 : 0;
			} else {
				print STDERR "$P: Unknown arg '$a' ignored.\n" if $V>0;
			}
			$arg = $x;
		}
	} elsif ($a =~ /^\+(\w+)$/) {
		&setfolder($MDIR,$1);
		print "Folder \"$FLDR\" from command line.\n" if $V>2;
	} elsif ($a =~ /^\d+$/) {
		$ndx = $a;
	} else {
		print "$P: File \"$a\"\n" if $V>1;
		push @files, $a;
	}
}
print "Folder \"$FLDR\"\n" if $V>1;

unless (@files ) {
	print "$P: MAIL=\"$MAIL\"\n" if $V>2;
	if (-f $MAIL) {
		push @files, $MAIL;
	} else {
		print STDERR "$P: Can't find mailbox.\n";
		print STDERR "$P: No file \"$MAIL\"\n";
		exit 1;
	}
}

$ndx = &maxindex($MDIR,$FLDR,1);
print "$P: FDIR=\"$FDIR\"\n" if $V>2;
print "$P: Highest index in $FDIR is $ndx.\n" if $V>2;

file:
for $mbox (@files) {
	unless (open(MBOX,$mbox)) {
		printf "$mbox doesn't exist.\n" if $V>2;
		next file;
	}
	$msgs = 0;
	print "$mbox\n" if $V>1;
	$lastblank = 1;			# Pretend initial blank line
	$date = $from = $subj = '';
line:
	while ($l = <MBOX>) {	# Scan the mail
		$l =~ s/[\s\r]+$//;	# Trim white space in any format
		if ($l eq '') {		# Blank lines are special
			$lastblank = 1;	# Note the blank line
			print MSG "\n";
			&msgdesc($ndx,': ',$date,$from,$subj) unless $list;
			$list = 1;
		} else {			# Non-blank line
			if (($l =~ /^From /) && $lastblank) {	# From after blank line
				while (-f "$MDIR/$FLDR/$ndx") {
					print "$P: $MDIR/$FLDR/$ndx exists.\n" if $V>1;
					++$ndx;
				}
				close MSG;	# Finish up the previous message
				$mfile = "$MDIR/$FLDR/$ndx";
				unless (open(MSG,">$mfile")) {
					print "$P: Can't write $mfile ($!)\n";
					exit 1;
				}
				++$msgcnt;
				if (++$msgs == 1) {
					&setindex($MDIR,$FLDR,$ndx);
				}
				$from = $subj = $list = '';
			}
			unless ($list) {	# Looking for list info
				if ($l =~ /^From:\s*(.*)$/i) {
					$from = $1;
					print "From: $from\n" if $V>1;
				} elsif ($l =~ /^Date:\s*(.*)$/i) {
					$date = $1;
					print "Date: $date\n" if $V>1;
				} elsif ($l =~ /^Subject:\s*(.*)$/i) {
					$subj = $1;
					print "Subj: $subj\n" if $V>1;
				} elsif (~$from && ($l =~ /^Sender:\s*(.*)$/i)) {
					$from = $1;
					print "Sender: $from\n" if $V>1;
				} else {
					print "#Hdr: $l\n" if $V>1;
				}
			}
			$lastblank = 0;
			print MSG "$l\n";
		}
	}
	if ($delmail && $msgs>0) {		# Empty mailbox unless debugging
		print "$P: Truncate \"$mbox\"\n" if $V>0;
		truncate($mbox,0);
	}
	print "$P: $msgs new messages in \"$mbox\"\n" if $V>2;
	print "$P: Done with \"$mbox\"\n" if $V>2;
}
close MBOX;
print "$P: $msgcnt new messages.\n" if $V>0;

if ($Refile) {		# Should we grovel through the messages
	system "Refile <$MDIR/.patterns &";	# and sort them into folders?
	print "$P: Refile process started in background.\n" if $V>2;
}

exit $exitstat;
