#!/usr/bin/env perl
our $VERSION = "0.1";

###############################################################################
### Variables #################################################################
###############################################################################
use vars qw( $PROG );

$PROG = "mutt -F $ENV{HOME}/.config/mutt/muttrc.news";

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

use News::Article;
use News::Article::Clean;
use strict;
use File::Temp qw/ tempfile tempdir /;

my $dir = tempdir( CLEANUP => 1 );
my ($fh, $temp) = tempfile( DIR => $dir );

my $file = shift @ARGV;
die "No such file: $file\n" unless (-r $file);
my $article = News::Article->new($file, 1024 * 1024, 1024 * 64);
die "Couldn't read article: $!\n" unless $article;

my $from = $article->header('from') || "";
   $from = $article->clean_from( $from );

my $subject = $article->header('subject') || "(no subject)";
   $subject = "Re: $subject";
   $subject = $article->clean_subject( $subject );

my $msgid = $article->header('message-id') || "";

my $respond = $article->header('reply-to') || $article->header('from');

my @users;  push @users, $respond;
push @users, "FILLMEIN\@unknown.site.invalid" unless scalar @users;

print $fh "In-Reply-To: $msgid\n" if $msgid;
print $fh "Subject: $subject\n" if $subject;
print $fh "To: ", join(", ", @users), "\n" if @users;

print $fh "\n";
print $fh "$from writes:\n";
foreach ($article->rawheaders) { print $fh "> $_\n"; }
print $fh "\n";
foreach ($article->body) { print $fh "> $_\n"; }

my $cmd = "$PROG -H $temp";
# print "$cmd\n";
my $response = system($cmd);
