#!/usr/bin/perl

#  Copyright (C) 1997 Anthony DeBoer
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of version 2 of the GNU General Public License as
#  published by the Free Software Foundation.
#
#  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.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

$test = $ARGV[0] eq '-v';

$HOME = $ENV{'HOME'};
$MAILDIR = $HOME . '/Mail';
$LISTSRC = $MAILDIR . '/.newsrc';

open(LISTSRC) || die "Cannot open $LISTSRC: $!\n";
while(<LISTSRC>) {
	chop;
	next unless /^([^\:]+)\:\s*(\d.*)$/;
	$group = $1, $rc = $2;
	$DIR = $MAILDIR . "/$group";
	opendir(DIR, $DIR) || die "Cannot opendir $DIR: $!\n";
	@bye = ();
	@more = ();
	for $f (readdir(DIR)) {
		push(@more, $f) if $f =~ /^\.(mh_sequences|xmhcache)$/;
		next unless $f =~ /^\d+$/;
		$rd = 0;
		for $rrange (split(/\,/, $rc)) {
			if ($rrange =~ /(\d+)\-(\d+)/) {
				$rd = 1, last if $f >= $1 && $f <= $2;
				}
			elsif ($f == $rrange) {
				$rd = 1, last;
				}
			}
		push(@bye, $f) if $rd;
		}
	closedir(DIR);
	$nrm = $#bye + 1;
	if ($nrm) {
		print "$group - $nrm unlinked";
		push(@bye, @more);
		unless ($test) {
			chdir($DIR) && unlink(@bye);
			}
		print ' ', join(' ', @bye);
		print "\n";
		}
	}
close(LISTSRC);

