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

##############################################################################
### Configuration ############################################################
##############################################################################

my $prefix = "invalid.notrealid";
my $domain = "news.killfile.org";
our $LOGDIR = "/home/tskirvin/news/posts/bymonth";

##############################################################################
### Declarations #############################################################
##############################################################################

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

##############################################################################
### main () ##################################################################
##############################################################################

my (@lines, @from);
while ( my $line = <STDIN>) { 
    if ($line =~ /^From: .*/) { 
        push @from, $line;
    } elsif ($line =~ /^\s*$/) { 
        push @lines, reverse @from if @from; @from = ();  
        push @lines, $line;
    } else { 
        push @lines, $line; 
    }
}

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

# 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);

##############################################################################
### Subroutines ##############################################################
##############################################################################

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