#!/usr/bin/perl

use strict;
use warnings;
use utf8;
use DateTime::Format::Mail;

my $dir = $ARGV[0];

if (! $dir)
{
  print "maildir2sympa is a script that helps to import messages form MailDir into Sympa mail archive.\n\n";
  print "Usage:
      ./maildir2sympa [path_to_mail_dir]

See 'perldoc maildir2sympa' for more info\n";
  exit 1;
}

if (! -d $dir)
{
  print "Error: $dir is not a directory\n";
  exit 1;
}

my %messages = ();

my @files = glob $dir."/*";
foreach my $file (@files)
{
  open F, $file;
  my $text = '';
  while (my $s = <F>) {$text.=$s;}
  close F;
  if (! ($text=~/^(.*?)\n\s?\n/s ) ) {print STDERR "Skipping message $file, headers is not found.\n"; next}  # /s is for possible dos endlines
  my $headers = $1;
  
  if (! ($headers =~ /\nDate:\s*(.*?)\n/s) ) {print STDERR "Skipping message $file, Date header is not found.\n"; next}
  my $date = $1;
  $date=~s/\([A-Za-z]*\)\s*$//;  # remove (MSK) from the end of the date
  
  my $datetime;
  
  eval {$datetime = DateTime::Format::Mail->parse_datetime($date);};
  if ($@)
  {
    print STDERR "Skipping message $file, Date format is wrong: '$date'\n"; next;
  }
  
  my $date_str =  $datetime->ymd." ".$datetime->hms;
  
  $messages{$date_str} ||=[];
  push @{$messages{$date_str}} ,$text;
}

my $last_dir = '';
my $dir_count;
foreach my $date (sort (keys %messages))
{
  $date =~ /^(\d*-\d*)-/;
  my $ym = $1;
  if ($last_dir ne $ym) 
  {
    if (-d $ym)
    {
      die "Directory $ym already exists, please remove all YYYY-MM dirs and rerun the script";
    }
    mkdir $ym;
    mkdir "$ym/arctxt";
    $last_dir=$ym;
    $dir_count = 1;
  }
  foreach my $text (@{$messages{$date}})
  {
    open F, ">", "$ym/arctxt/$dir_count";
    print F $text;
    close F;
    $dir_count ++;
  }
}


=pod

=head1 NAME

maildir2sympa.pl - A script that helps to import messages MailDir into Sympa mail archive

=head1 VERSION

version 0.01

=head1 SYNOPSIS

    ./maildir2sympa [path_to_mail_dir]

=head1 DESCRIPTION

Scipt scans specified dir for message files, parse the date from the headers and then copy all messages to directories YYYY-MM into current dir, as they are stored in 
Sympa mail archive.

=head1 USAGE

First create a new mail list as it is said in Sympa manual. For example it would be dev@lists.example.com

First run a script for MairDir you are going to import:

    ./maildir2sympa message_dir_to_import

Then copy all YYYY-MM dirs to the sympa archive dir of your list

    sudo -u sympa mkdir /var/lib/sympa/wwsarchive/dev@lists.example.com
    sudo -u sympa cp -R [0-9][0-9][0-9][0-9]-[0-9][0-9] /var/lib/sympa/wwsarchive/dev@lists.example.com

To rebuild html version of your list archive, you should create rebuild flag-file in /var/spool/sympa/outgoing/

    sudo -u sympa touch /var/spool/sympa/outgoing/.rebuild.dev@lists.example.com

Then restart sympa

    /etc/init.d/sympa restart

=head1 CODE

Check svn://nataraj.su/misc/sympa for new versions

=head1 AUTHOR

Swami Dhyan Nataraj (Nikolay Shaplov)  <n@shaplov.ru>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2011 by Swami Dhyan Nataraj (Nikolay Shaplov).

You may distribute it under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl 5.10.0 README file.

=cut