--- orig/perfect_maildir.pl	2003-03-09 05:08:26.000000000 -0500
+++ perfect_maildir.pl	2004-02-26 17:42:41.000000000 -0500
@@ -1,7 +1,7 @@
-#!/usr/bin/perl
+#!/usr/local/bin/perl

-# "Simple but Perfect" mbox to Maildir converter v0.2
-# Copyright (C) 2001-2003  Philip Mak <pmak@aaanime.net>
+# "Simple but Perfect" mbox to Maildir converter v0.3
+# Copyright (C) 2001-2004  Philip Mak <pmak@aaanime.net>
  #
  # This program is free software; you can redistribute it and/or
  # modify it under the terms of the GNU General Public License
@@ -17,6 +17,9 @@
  # along with this program; if not, write to the Free Software
  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
02111-1307, USA.

+use DateTime::Format::Mail;
+my $date_parser = DateTime::Format::Mail->new( loose => 1 );
+
  # Get the hostname

  my $hostname = `hostname`;
@@ -31,6 +34,18 @@
  }

  # check for writable maildir
+if (!-d "$maildir") {
+    mkdir $maildir;
+}
+if (!-d "$maildir/cur") {
+    mkdir "$maildir/cur";
+}
+if (!-d "$maildir/new") {
+    mkdir "$maildir/new";
+}
+if (!-d "$maildir/tmp") {
+    mkdir "$maildir/tmp";
+}
  unless (-w "$maildir/cur") {
    print STDERR "Cannot write to $maildir/cur\n";
    exit 1;
@@ -60,6 +75,7 @@
    $flags .= $1 if $line =~ /^Status: ([A-Z]+)/;
    $flags .= $1 if $line =~ /^X-Status: ([A-Z]+)/;
    $subject = $1 if $line =~ /^Subject: (.*)$/;
+  $date = $1 if $line =~ /^Date: (.*)$/;
  }

  $num++;
@@ -96,6 +112,14 @@
    print FILE $line;
  }
  close(FILE);
+
+# set the file's mtime to be that of the Date: field in the message
+my $date_time = $date_parser->parse_datetime( $date );
+if (defined($date_time)) {
+    my $message_time = $date_time->epoch();
+    utime time(),$message_time,$file;
+}
+
  goto repeat unless eof(STDIN);

  my $elapsed = time - $time;

