#!/usr/bin/perl
#

die "Usage: $0 file(s)\n" if ($#ARGV < 0);

foreach $file ( @ARGV )
{
  open( OFILE, ">$file.new" ) || die "create $file.new - $!\n";
  open( IFILE, "$file" ) || die "open $file - $!\n";
  print ("Filtering file: $file\n" );
 
  $out = 1;	
	$st = 0;
  line: while ($ln = <IFILE>)
  {
		if ( $st ) 
		{
		  if( $ln !~ /^8\d\./ ) 
			{
				print( OFILE "$oln");
				$out = 1;
			}
			$st = 0;
		}
    $out = 0, $st = 1, $oln = $ln if ($ln =~ /^1 0 0 rg/ );
    $out = 1 if ($ln =~ /^q/ );
    print( OFILE "$ln") if( $out );
  }
  close( IFILE ) || die "close $file $!\n";
  close( OFILE );
  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
   $atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
  unlink "$file";
  rename "$file.new", "$file" || die "move $file.new to $file\n";
  utime $atime,$mtime,"$file";
}
exit;

# end
