#!/usr/athena/bin/perl
opendir (ML, "/mit/mkgray/Mail/inbox") || die "No mailbox...";
open (SCI, "/mit/mkgray/.scan.tmp") || die "No .scan.tmp";

while (<SCI>) {
	($blank, $mesgnum, $month, $day, @rest) = split (/[ \/]+/, $_);
	$when{$mesgnum} = (31 * $month) + $day;
	print "Message $mesgnum at day $month/$day = $when{$mesgnum}\n";
	rename ($mesgnum, "$mesgnum.tmp") || print "Failed while ";
	print "Renaming $mesgnum\n";
	sub byvalue { $a <=> $b; }
	sub byval { $when{$a} <=> $when{$b}; }
	@keys = keys %when;
	@sorted = sort byval @keys;
} 
	foreach (@sorted) {
		$x++;
		$oldnum = $_;
		print "Day: $when{$oldnum} :: $oldnum should become $x\n";
		rename ("$oldnum.tmp", $x) || print "Maybe not...\n";
}
#	rename ($mesgnum, $mesgnum.".tmp");
#	print "Renaming $mesgnum\n";



#foreach (@sorted) {
#	$oldnum = $;
#	$x++;
#	rename ($oldnum.".tmp", $x);
#	print "Reassigning $oldnum to $x\n";
#}
