#!/usr/bin/perl -w
#
# $Revision: 1.6 $ $Date: 2012-07-26 17:11:49-04 $
# $Source: /home/vogelke/bin/RCS/sortbydate,v $
# $Host: sys7.com $
# $UUID: 08924531-ad34-3a73-8048-eff056345bd3 $
#
#<sortbydate: print filenames holding email in date-sorted order.

my $rcsid  = '$Id: sortbydate,v 1.6 2012-07-26 17:11:49-04 vogelke Exp $';
my $rcssrc = '$Source: /home/vogelke/bin/RCS/sortbydate,v $';

use strict;
use Getopt::Long;
use Pod::Usage;
use File::Basename;
use Date::Parse;
use Date::Format qw(time2str);

sub numeric { $a <=> $b; }    # sort in numeric order.

$ENV{'PATH'} = join ":", qw(/bin /usr/bin /usr/local/bin);

my $myname = basename($0);
$myname =~ s/\.\w*$//;        # strip any extension

my %options;
my @getopt_args = (
    'h|?',                    # print usage
    'm',                      # print manpage
    'v',                      # print version
    );

Getopt::Long::config("noignorecase", "bundling");
usage() unless GetOptions(\%options, @getopt_args);

manpage() if $options{'m'};
version() if $options{'v'};
usage()   if $options{'h'} || !@ARGV;

#
# Read each file, looking for a date somewhere in the header.
# Go through the list, get each date, and make another list which
# holds the proper order for printing.
#
# FIXME: add an option to not only find the date but fix it as well.
#

my %filelist;
my $mintime = 0;    # time to use when we don't find a date.
my $x;

foreach my $path (@ARGV) {
    my $match = 0;    # no date found.
    my ($ifh, $t, $y);
    open($ifh, "$path") || die "$path: $!\n";

    while (<$ifh>) {
        chomp;
        s/^\s*//g;

        if (/^Date:|^Sent:/) {
            s/^....://;
            $t = str2time($_);

            if (defined $t) {
                $match = 1;
                $y = time2str("%s", $t);
		$x = time2str ("%a %e %b %Y %T", $t);
                $filelist{$y} .= "$path\t$x=";
            }
            last;
        }
    }

    close($ifh);

    if ($match == 0) {
        $filelist{$mintime} .= "$path ";
    }
}

#
# Print each filename in date-sorted order.  Files with no date
# come first.
#

foreach $x (sort numeric keys %filelist) {
    $_ = $filelist{$x};
    s/=/\n/g;
    print;
}

exit(0);

#---------------------------------------------------------------------
# Print a usage message from the comment header and exit.

sub usage {
    my ($emsg) = @_;

    require Pod::Usage;
    import Pod::Usage qw(pod2usage);
    warn "$emsg\n" if defined $emsg;
    pod2usage(-verbose => 1);
}

sub manpage {
    require Pod::Usage;
    import Pod::Usage qw(pod2usage);
    pod2usage(-exitstatus => 0, -verbose => 2);
}

#---------------------------------------------------------------------
# Print the current version and exit.

sub version {
    $_ = $rcsid;
    s/,v / /;
    @_ = split;
    print "$myname  v$_[2]  $_[3] $_[4]\n";
    exit(0);
}

#---------------------------------------------------------------------
__END__

=head1 NAME

sortbydate - print filenames holding email in date-sorted order

=head1 SYNOPSIS

sortbydate [-hmv] file [file...]

=head1 OPTIONS

=over 4

=item B<-h>

Print a brief help message and exit.

=item B<-m>

Print the manual page and exit.

=item B<-v>

Prints the version and exits.

=back

=head1 DESCRIPTION

B<sortbydate> reads one or more files generated by "csplit".
Outlook writes messages in any order it likes, usually separated by
a row of underscores.  To break up those files into separate messages,
you can use something like this:

 csplit some-filename '/^____/' '{*}'

Each file is assumed to contain a date.  If a date is found,
the program will attempt to rewrite it as "weekday dd mmm yyyy hh:mm:ss".

The filenames are sorted by date and written to stdout.
Files with no date are written first.

=head1 EXAMPLE

  me% ./sortbydate msg*
  msg-71192-205   Tue 24 Apr 2007 15:51:00
  msg-71192-204   Mon 30 Apr 2007 15:06:00
  msg-71192-6.msg Tue  1 May 2007 07:17:58
  msg-71192-203   Tue  1 May 2007 07:18:00
  msg-71192-5.msg Tue  1 May 2007 08:33:56
  msg-71192-202   Tue  1 May 2007 08:34:00
  msg-71192-4.msg Tue  1 May 2007 08:43:04
  msg-71192-3.msg Tue  1 May 2007 08:53:30
  msg-71192-201   Tue  1 May 2007 08:54:00
  msg-71192-200   Tue  1 May 2007 09:05:55

=head1 AUTHOR

 Karl Vogel <vogelke@pobox.com>
 Sumaria Systems, Inc.

=cut
