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

# "newsrc-to-message-id.pl"
# 
# Copyright (c) 1991 by Jonathan Kamens (jik@athena.mit.edu).
# 
# This file may be freely redistributed as long as this entire
# comment (up to the first blank line below) stays on it, and as long
# as you don't try to make any money distributing it.
# 
# Furthermore, if you make any changes to it and then redistribute
# it, please mark in some ways the changes you have made my version,
# and, if possible, forward the changes back to me.
# 
# $Id: newsrc-to-message-id.pl,v 1.8 1992/11/15 04:35:05 jik Exp $

# This script reads a .newsrc file.  The output consists of the list
# of newsgroups with ! or : after them to indicate whether they are
# subscribed or not, and then on subsequent lines, the Message-ID's
# of read messages on the specified (either by $NNTPSERVER) or by the
# command line flag '-server') NNTP server.
# 
# This output can then be fed into the 'message-id-to-newsrc.pl'
# script to turn it back into a .newsrc for another NNTP server.

# $max_xhdr = 500;

push(@INC, '/afs/athena.mit.edu/user/j/i/jik/lib/perl');

require 'nntp.pl';

# Defaults

$opt_D = 0;
$ticks = 1;
$unbuffered = 0;
$skip_unsubbed = 0;
$server = $ENV{"NNTPSERVER"};
undef $outfile;

while ($_ = $ARGV[0], /^[-+]/) {
    shift;
    if (/^-server$/) {
	die "Missing argument to \"$_\".\n" if (@ARGV == 0);
	$server = shift;
	next;
    }
    if (/^-out$/) {
	die "Missing argument to \"$_\".\n" if (@ARGV == 0);
	$outfile = shift;
	next;
    }
    elsif (/^(\+|-)ticks$/) {
	$ticks = ($1 eq "+") ? 1 : 0;
	next;
    }
    elsif (/^(\+|-)nntp_debug$/) {
	$opt_D = ($1 eq "+") ? 1 : 0;
	next;
    }
    elsif (/^(\+|-)buffered$/) {
	$unbuffered = ($1 eq "+") ? 0 : 1;
	next;
    }
    elsif (/^(\+|-)unsubbed$/) {
	$skip_unsubbed = ($1 eq "+") ? 0 : 1;
	next;
    }
    die "Unknown argument \"$_\".\n";
}
	
die "No NNTP server specified!\n" if (! $server);

&news_open($server);

if ($outfile) {
    close(STDOUT);
    open(STDOUT, ">$outfile") || die "Opening $outfile for write: $!.\n";
    select(STDOUT);
}

if ($unbuffered) {
    $| = 1;
}

while (<>) {
    if (/^(.*)([:!]) *(.*)$/) {
	($group, $sub) = ($1, $2);
	@ranges = split(/,/, $3);

	print "$group$sub\n";

	if ((@ranges > 0) && (($sub eq ":") || (! $skip_unsubbed))) {
	    @group = &news_select_ng($group);

	    if (@group != 3) {
		print STDERR "Bogus group \"$group\"?\n";
	    }
	    else {
		($ng_first, $ng_last, $ng_num) = @group;
	    }

	    foreach $range (@ranges) {
		if ($range =~ /^([0-9]+)-([0-9]+)$/) {
		    $start = $1;
		    $end = $2;
		}
		else {
		    $start = $end = $range;
		}
		if ($start < $ng_first) {
		    $start = $ng_first;
		}
		if ($end > $ng_last) {
		    $end = $ng_last;
		}
		if ($end && ($start <= $end)) {
		    &print_hdrs($start, $end);
		}
	    }
	}
    }
    if ($ticks) {
	if (++$counter % 10 == 0) {
	    print STDERR $counter;
	}
	else {
	    print STDERR ".";
	}
    }
}

print STDERR "\n" if ($ticks);

sub print_hdrs {
    local($first, $last) = @_;
    local(%ids);
    local($number, $id);
    local($span) = $max_xhdr ? $max_xhdr : ($last - $first + 1);

    while ($first <= $last) {
	$range = sprintf("%d-%d",
			 $first,
			 ($first + $span - 1 <= $last) ? 
			 $first + $span - 1 : $last);
	last if (! &news_xhdr_init('message-id', $range));
	$first += $span;

	while ((($number, $id) = &news_xhdr_next()) == 2) {
	    print "$id\n";
	}
    }
}

&news_close();
