# Global stuff for JC's perl clone of the mh package.

use User::pwent qw(:FIELDS);

$ENV{'PATH'} = '/u/guests/jc/mh:/u/guests/jc/p/cgi:/bin:/usr/bin:/usr/local/bin:/usr/eecs/bin';
($P = $0) =~ s".*/"";
$Varg = $ENV{"V_$P"} || $ENV{'V_MH'} || $ENV{'V'} || 1;
if ($Varg =~ /(\d+)/) {
	$V = int($1);
} else {
	$V = 1 unless defined $V;
}

%M2m = ('Jan' => '01', 'Feb' => '02', 'Mar' => '03',
		'Apr' => '04', 'May' => '05', 'Jun' => '06', 
		'Jul' => '07', 'Aug' => '08', 'Sep' => '09',
		'Oct' => '10', 'Nov' => '11', 'Dec' => '12');
$outcols = 80;			# Default to punch-card width ;-)
if (-t STDIN) {
	@stty = `/bin/stty -a`;
	for $l (@stty) {
		if ($l =~ / (\d+) col/) {
			$outcols = $1;
			print "Window has $outcols columns.\n" if $V>2;
		}
	}
}

&usrinfo();		# Make sure we have our user information

$FLDR = 'inbox';	# Default mail folder
$LNAM = $ENV{'LOGNAME'} || $ENV{'USER'} || 'nobody';
$HOME = $ENV{'HOME'}    || "/home/$LNAM";
$MDIR = $ENV{'MAILDIR'} || "$ENV{'HOME'}/Mail";
$FDIR = $MDIR . '/' . &getfolder($MDIR,$fldr);

print "$P: FDIR=\"$FDIR\"\n" if $V>2;

sub getfolder {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Find the name of the current folder for a mail directory.  The 2nd  arg  is #
# the default to use if the folder can't be found, usually 'inbox'.           #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($fdir,$fldr) = @_;
	$fdir =  $MDIR  unless $fdir;
	$fldr = 'inbox' unless $fldr;
	if (open(SEQ,"$fdir/.folder")) {
		while ($l = <SEQ>) {
			if ($l =~ /^\s*(\w+)[\s\r]*$/) {
				if (-d "$fdir/$1") {
					$fldr = $1;
						print "$0: Folder $fldr from $fdir/$1\n" if $V>2;
					last;
				} else {
					printf STDERR "$0: \"$1 is not a folder (in $fdir/$1)\n";
				}
			}
		}
		close SEQ;
	}
	return $fldr;
}

sub setfolder {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Set the current folder.  
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($mdir,$fldr) = @_;
	local(*SEQ);
	$fldr = 'inbox' unless $fldr;
	unless (open(SEQ,">$mdir/.folder")) {
		print STDERR "$0: Can't write $mdir/.folder ($!)\n";
		return 0;
	}
	print SEQ "$fldr\n";
	close SEQ;
	$FLDR = $fldr;
	return $FLDR;
}

sub getindex {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Get the index of the current message in a folder.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($mdir,$fldr,$mndx) = @_;
	$mdir = $MDIR unless $mdir;
	$fldr = $FLDR unless $fldr;
	unless (open(SEQ,"$mdir/$fldr/.mh_sequences")) {
		print STDERR "$P:getindex: Can't read $mdir/$fldr/.mh_sequences ($!)\n" if $V>2;
		return($mndx || 1);
	}
	while ($l = <SEQ>) {
		if ($l =~ /^cur:\s*(\d+)[\s\r]*$/) {
			$mndx = $1;
			last;
		}
	}
	close SEQ;
	return($mndx || 1);
}

sub maxindex {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Find the highest index in an email folder.  This is  done  by  listing  the #
# folder's files whose names are integers, and returning the highest name.    #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($mdir,$fldr) = @_;
	local(*DIR,$dir,$f,@files,$imax);
	$dir = "$mdir/$fldr";
	unless (opendir(DIR,$dir)) {
		print STDERR "$P:maxindex: Can't read dir \"%s\" ($!)\n";
		return 0;
	}
	@files = readdir(DIR);
	closedir(DIR);
	$imax = 1;
	for $f (@files) {
		if ($f =~ /^(\d+)[\s\r]*$/) {
			$imax = $1 if $imax < $1;
		}
	}
	print "Highest index in folder $fldr is $imax.\n" if $V>2;
	return $imax;
}

sub msgdesc {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Output a message description.  This is used by the List and Show commands to
# tell the user about mail messages.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($ndx,$flg,$date,$from,$subj) = @_;
	local($yy,$mm,$dd,$mmm);
	if (($d,$mmm,$c,$yy) = ($date =~ /^\w*,\s*(\d+)\s+(\w\w\w),* (\d*)(\d\d) /)) {
		$date = sprintf "%02d%02d%02d",$4,$M2m{$2},$1,
	} elsif (($mmm,$d,$c,$yy) = ($date =~ /^\w*,\s*(\w\w\w) (\d+)\s+(\d*)(\d\d) /)) {
		$date = sprintf "%02d%02d%02d",$4,$M2m{$1},$2,
	} elsif (($d,$mmm,$c,$yy) = ($date =~ /^(\d+)\s+(\w\w\w) (\d*)(\d\d) /)) {
		$date = sprintf "%02d%02d%02d",$4,$M2m{$2},$1,
	} elsif (($mmm,$d,$c,$yy) = ($date =~ /^(\w\w\w) (\d+)\s+(\d*)(\d\d) /)) {
		$date = sprintf "%02d%02d%02d",$yy,$M2m{$mmm},$d,
# "Fri May 16 09:28:00 2003"
	} elsif (($mmm,$d,$c,$yy) = ($date =~ /^\w* (\w\w\w) (\d+)\s+\d+:\d+:\d+ (\d*)(\d\d)/)) {
		$date = sprintf "%02d%02d%02d",$yy,$M2m{$mmm},$d,
	} else {
		printf STDERR "$P: Can't parse date \"$date\" in message $ndx.\n";
		$date = '000000';
	}
	$date = substr $date,0,6;
	$from = substr $from,0,20;
	$subj = substr $subj,0,($outcols - 20);
	printf "%3d%s%6s %20s %s\n",$ndx,$flg,$date,$from,$subj;
}

sub setindex {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Set the index for a folder.  It is kept in a file ".mh_sequences".  We also #
# add a line giving the path, to prevent confusion when moving things around. #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($mdir,$fldr,$ndx) = @_;
	local($gotcur,$gotpth,$l,@lines,*SEQ,$pth);
	$ndx = 1 unless $ndx > 0;
	$pth = "$mdir/$fldr/.mh_sequences";
	if (open(SEQ,$pth)) {
		@lines = <SEQ>;
		for $l (@lines) {
			if ($l =~ /^cur:/) {$l = "cur: $ndx\n"; $gotcur++}
			if ($l =~ /^pth:/) {$l = "pth: $pth\n"; $gotpth++}
		}
		close SEQ;
		unless ($gotcur) {push @lines, "cur: $ndx\n"}
		unless ($gotpth) {push @lines, "pth: $pth\n"}
	} else {
		print STDERR "$P:setindex: Can't read $pth ($!)\n";
#		return($ndx || 1);
		push @lines, "cur: $ndx\n";
		push @lines, "pth: $pth\n";
	}
	unless (open(SEQ,">$pth")) {
		print STDERR "$0: Can't write $pth ($!)\n";
		return 0;
	}
	print SEQ @lines;
	close SEQ;
	print "Current message in \"$fldr\" is $ndx\n" if $V>2;
	return($ndx || 1);
}

sub usrinfo { my $F = 'usrinfo';
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Figure out assorted user information. This is mostly useful when we've been #
# called  from  cron  or  a  web  server,  and  don't  have  the  usual shell #
# environment all set up for us.                                              #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	my($euid,$x);
	($cwd = `/bin/pwd`) =~ s/[\r\s]+$//;
	print "$F:     cwd = \"$cwd\"\n" if $V>2;

	$euid = $>;		# Our effective user ID number
	$ruid = $<;		# Our real user ID number
	if ($V > 2) {
		print "$F:    euid = \"$euid\"\n";
		print "$F:    ruid = \"$ruid\"\n";
	}
	getpwuid($euid);
	if ($V > 2) {
		print "$F:    name = \"$pw_name\"\n";
		print "$F:  passwd = \"$pw_passwd\"\n";
		print "$F:     uid = \"$pw_uid\"\n";
		print "$F:     gid = \"$pw_gid\"\n";
		#rint "$F:   quota = \"$pw_quota\"\n";
		#rint "$F: comment = \"$pw_comment\"\n";
		#rint "$F:    gcos = \"$pw_gcos\"\n";
		print "$F: homedir = \"$pw_dir\"\n";
		#rint "$F:   shell = \"$pw_shell\"\n";
		#rint "$F:  expire = \"$pw_expire\"\n";
	}
	$ENV{'USER'}    = $pw_name;
	$ENV{'LOGNAME'} = $pw_name;
	$ENV{'HOME'}    = $pw_dir;
	$ENV{'SHELL'}   = $pw_shell;
	$MAIL = "/var/mail/$pw_name";

	print "$F:ENV{MAIL}= \"$MAIL\"\n" if $V>3;
	if (defined $MAIL && -e $MAIL) {
		print "$F:ENV{MAIL}= \"$MAIL\" exists.\n" if $V>5;
		if (-f $MAIL) {
			print "$F:ENV{MAIL}= \"$MAIL\" exists and is a file.\n" if $V>2;
		} else {
			print "$F:ENV{MAIL}= \"$MAIL\" exists and is not a file.\n" if $V>0;
			$MAIL = '';		# Wipe it out and try again.
		}
	} else {
		print "$F:ENV{MAIL}= \"$MAIL\" does not exist.\n" if $V>1;
		$MAIL = $ENV{'MAIL'} = '';
	}
	unless (-f $MAIL) {
		print "$F:ENV{MAIL}= \"$MAIL\"\n" if $V>1 && $MAIL;
		print "$F: Look for mailbox file ...\n" if $V>1;
		   if (-f ($x = "/var/mail/$pw_name"))       {$MAIL = $x}
		elsif (-f ($x = "/usr/mail/$pw_name"))       {$MAIL = $x}
		elsif (-f ($x = "/var/spool/mail/$pw_name")) {$MAIL = $x}
		elsif (-f ($x = "/usr/spool/mail/$pw_name")) {$MAIL = $x}
		else  {
			print STDERR "$F: Can't find MAIL file for user '$pw_name'.\n" if $V>0;
			print        "$F: Can't find MAIL file for user '$pw_name'.\n" if $V>0;
		}
		print "$F:ENV{MAIL}= \"$MAIL\"\n" if $V>1;
		$ENV{'MAIL'} = $MAIL;
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
1;
