#!/usr/bin/perl

open(ACTIVE,"</usr/lib/news/active");
@active = <ACTIVE>;
close(ACTIVE); 

system("find /var/spool/news -type f -mtime +7 -exec rm {} \\;");

for($i=0; $i<=$#active; $i++) {
   ($name,$last,$first,$posting) =
     ($active[$i] =~ /^(.*) (\d+) (\d+) (\w)$/);
   $of=$first;
   print "Doing $name - ";
   $exp = 0;
   ($path = $name) =~ s#\.#/#g;
   $path = "/var/spool/news/$path/";
   opendir(DIR,$path);
   @files = readdir(DIR);
   closedir(DIR);
   shift @files; shift @files;  # dumps . and ..
   @sorted = sort {$a <=> $b} @files;
   if($first < $sorted[0]) { $first=$sorted[0]; }
   print "Expired ".($first-$of)."\n";
   $active[$i] = &fiddle($name,$last,$first,$posting);
}

open(ACTIVE,">/usr/lib/news/active");
print ACTIVE @active;
close(ACTIVE);

sub fiddle {
  ($nm,$nl,$nf,$p) = @_;
  $blah = "0000000000".$nl;
  $nl = substr($blah,length($blah)-10,10);
  $blah = "0000000000".$nf;
  $nf = substr($blah,length($blah)-10,10);
  return sprintf("%s %s %s %s\n",$nm,$nl,$nf,$p);
}
