--- perfect_maildir.pl	Sun Mar  9 11:08:26 2003
+++ bin/perfect_maildir.pl	Tue Nov 25 19:26:09 2003
@@ -17,6 +17,8 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
+use Date::Manip qw(UnixDate ParseDateString);
+
 # Get the hostname
 
 my $hostname = `hostname`;
@@ -60,6 +62,7 @@
   $flags .= $1 if $line =~ /^Status: ([A-Z]+)/;
   $flags .= $1 if $line =~ /^X-Status: ([A-Z]+)/;
   $subject = $1 if $line =~ /^Subject: (.*)$/;
+  $delivered = $1 if $line =~ /^Delivery-date: (.*)$/;
 }
 
 $num++;
@@ -67,7 +70,7 @@
 # open output file
 my $file;
 if ($flags =~ /O/) {
-  $file = "$maildir/cur/$time.$num.$hostname";
+  $file = sprintf( "%s%05d%s", "$maildir/cur/$time.", $num, ".$hostname" );
   my $extra = '';
   $extra .= 'F' if $flags =~ /F/; # flagged
   $extra .= 'R' if $flags =~ /A/; # replied
@@ -75,7 +78,7 @@
   $extra .= 'T' if $flags =~ /D/; # trashed
   $file .= ":2,$extra" if $extra;
 } else {
-  $file = "$maildir/new/$time.$num.$hostname";
+  $file = sprintf( "%s%05d%s", "$maildir/new/$time.", $num, ".$hostname" );
 }
 
 # filter out the "DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA" message or the message doesn't exists
@@ -96,6 +99,10 @@
   print FILE $line;
 }
 close(FILE);
+
+$delivered = sprintf UnixDate(ParseDateString( $delivered ),"%s\n");
+utime( $delivered, $delivered, $file );
+
 goto repeat unless eof(STDIN);
 
 my $elapsed = time - $time;
