#!/usr/local/bin/perl
our $VERSION = 0.5;	
# Parse 'nn' sentnews files and put them into a standard location for
# later use with News::Archive.  Requires News::Archive::Mbox

use Date::Parse;
use News::Article;
my $prefix = "invalid.notrealid";
my $domain = "news.killfile.org";
our $LOGDIR = "/home/tskirvin/news/posts/bymonth";

use lib '/home/tskirvin/dev/news-archive'; 
use News::Article::Mbox;

my @articles = News::Article::Mbox->read_mbox(\*STDIN);

# Rewrites
foreach my $article (@articles) { 	
  my $timestamp = $$article{TIMESTAMP} || "";  
  my $stamp = $timestamp;  $stamp =~ s/^From \S+\s*//; 

  $article->add_message_id($prefix, $domain);  
  if ($article->header('from') eq 'tskirvin') { 
    $article->set_headers('from', 
			'tskirvin@unknown.site.invalid (Tim Skirvin)');
  }
  my @body = $article->body;  
  map { s/([Tt])umati/$1\*mati/g; } @body;
  unless ($article->header('date')) {
    $article->set_headers('date', $stamp);  
  }
  $article->set_body(@body);

  my $posttime = str2time($article->header('date')) || str2time($stamp);
  my $date = sprintf("%04d-%02d", 
	(localtime($posttime))[5] + 1900, (localtime($posttime))[4] + 1);
  $list{$date} ||= mkfh($date);
  my $fh = $list{$date} or next;
  print $fh "$timestamp\n";
  $article->write($fh);
  print $fh "\n";
}

exit (0);

sub mkfh {
  my ($file) = @_;
  open (FH, ">> $LOGDIR/$file");
  \*FH;
}
