#!/afs/athena/contrib/perl/perl

$newsrcfile = $ENV{'NEWSRC'} || "$ENV{'HOME'}/.newsrc";

push (@INC, "/afs/sipb/contrib/perl/nntp");

require "nntp.pl";

%groups = ();

$server = &nntp'connect;

foreach $ng (&nntp'list ($server, "all"))
{
    $groups{$ng} = 1;
}

$| = 1;

foreach $ng (&nntp'newsrc_list ($newsrcfile))
{
    $old = &nntp'newsrc_get ($newsrcfile, $ng, 0);
    
    delete $groups{$ng};
    next if ! $old;

    @lim = &nntp'setgroup ($server, $ng);
    
    next if ! @lim;         # this should never ever happen
    next if $lim[0] == 0; # skip if no articles
    
    $new = "$lim[1]-$lim[2]";
    $new = &nntp'newsrc_merge ($newsrcfile, $ng, $new);
    
    # count up the old and new articles
    $cnt = 0;

    # add up new article ranges
    foreach $rng (&nntp'canon_expand ($new))
    {
	($first, $last) = split (/-/, $rng);
	$cnt += $last - $first + 1;
    }
    
    # subtract off old article ranges
    foreach $rng (&nntp'canon_expand ($old))
    {
	($first, $last) = split (/-/, $rng);
	$cnt -= $last - $first + 1;
    }
    
    if ($cnt > 0) 
    {
	print "$cnt unread articles in $ng\n";
    }

    $rangelist = &nntp'canon_inverse (&nntp'canon_artlist ($old), 
				      $lim[1], $lim[2]);

    foreach $rng (&nntp'canon_expand ($rangelist))
    {
	local ($first, $last) = split (/-/, $rng);
	local (@lines);
	
	&nntp'fields ($server, *flds, "Subject", $first, $last);
	
	foreach (@flds)
	{
	    s/^([0-9]+) /$1: /;
	    print "$_\n";
	}
    }
    if ($cnt > 0) {
	print "\014\n";
    }
}

&chat'print ($server, "quit\n");
