#!/usr/bin/perl
# move junked articles                                            -*- perl -*-

# Look for articles in "junk", ask to create the newsgroups they occupy,
# and repost the junked articles locally.
# The reposting is done by leaving batches in in.coming.
# The program asks interactively for the newsgroups flags, leave it empty to
# not create a group. Parameter -n for not creating groups at all,
# -r to remove junk articles after reposting.

# Assembled from spare parts of the psl project by Olaf Titz, Jun.95.
# $Id: xmvjunk,v 1.6 1995/12/03 18:45:37 olaf Exp $

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# ----------------------------------------------------------------------------

require "getopts.pl";
require "psl-aux.pl";

$opt_b = 1000000;               # approx. maximum of batch size
&Getopts("rnb:");
if (!-t STDIN) {
    $opt_n=1;
}
&auxinit("xmvjunk");

@arts=(<$newsarts/junk/[1-9]*>);
$progress=50;

# First pass: Collect unknown newsgroups
foreach $art (@arts) {
    if (++$progress>50) {
       print STDOUT "."; $progress=0;
    }
    if (open(A, $art)) {
       while(<A>) {
           /^Newsgroups: (.*)$/i && do {
               split(/,/, $1);
               foreach $i (@_) {
                   $newact{$i}=1 unless $active{$i};
               }
               last;
           };
       }
       close A;
    } else {
       print STDOUT "?";
    }
}

# Ask for generation of new groups
$havenew=0;
if (!$opt_n) {
    @g = (sort keys %newact);
    $tstr = sprintf("%d %s@%s (mvjunk)", time, $logname, $hostname);
    if ($#g>=0) {
       print STDOUT "\nEnter newsgroup flag for group or empty:\n";
       foreach $i (@g) {
           Input: while (1) {
               print STDOUT "$i ";
               $_=<STDIN>; y/\r\n//d;
               /^=(.*)$/ && do { last Input if ($active{$1}); };
               last Input if (/^[ymn]?$/);
           }
           if ($_) {
               &addgroup($i,$_);
               ++$havenew;
           }
       }
    }
}

# Second pass: Repost and remove
$progress=50; $count=0;
&open_batch;
Article: foreach $art (@arts) {
    if (++$progress>50) {
       print STDOUT "."; $progress=0;
    }
    if (open(A, $art)) {
       $inhdr=1; $article=""; $dothis=0;
       Line: while(<A>) {
           y/\n//d;
           if ($inhdr) {
               /^Newsgroups:\s+(.*)$/i && do {
                    split(/,/, $1);
                    foreach $i (@_) {
                        next Article if ($i eq "junk");
                        # don't repost to "junk"
                        if ($active{$i}) {
                            $dothis=1;
                        }
                    }
               };
               /^Message-ID:\s+<([^>]+)>$/i && do {
                   $_="Message-ID: <mvjunk.$1>";
               };
               /^Distribution:/i && next Line;
               /^$/ && do {
                    next Article unless $dothis;
                    $inhdr=0;
                    $_="Distribution: local\n";
               };
           }
           $article .= "$_\n";
       }
       close A;
       ++$count;
       $s=length($article);
       $batchsize+=$s;
       if ($opt_r) {
           unlink($art);
       }
       if ($batchsize>$opt_b) {
               &close_batch; &open_batch;
       }
       print BATCH "#! rnews $s\n$article";
    } else {
       print STDOUT "?";
    }
}
&close_batch;

print LOG "$count articles reposted.\n";
&auxexit($havenew);
unlink("LOCK");
exit 0;
