#!/usr/local/bin/perl5

# Need to handle arguments better, and print better
# diagnostics as we go along.  --MNP 6-5-97
#
# usage: fix linespeed oldfile newfile

#$bandwidth = shift @ARGV;

$| = 1;

$linespeed = $ARGV[0];
$oldfile   = $ARGV[1];
$newfile   = $ARGV[2];

if ( ($linespeed eq "" || ! $linespeed =~ m/^(\d)+/g) or 
     ($oldfile eq "" || $newfile eq "") or
     ($oldfile eq $newfile) ) {
        print STDERR "usage: $0 linespeed oldfile newfile\n";
        print STDERR "   (linespeed is in kbits/sec)\n";
        print STDERR "   e.g. $0 1544kb myfile newfile\n";
        print STDERR "   e.g. $0 1.544mb myfile newfile\n";
        exit(21);
     }
$mult = 1;
if ($linespeed =~ m/kb/i) { $mult = 1000; }
if ($linespeed =~ m/mb/i) { $mult = 1000000; }
# a,b,c,d are all bytes/sec, so we need to divide
# kbits/sec by 8, and mult. by 1000., for example
$bandwidth = ( $linespeed * $mult / 8 );

($dateold, $aold, $bold, $cold, $dold) = (0,0,0,0,0);
open(OLD,$oldfile) or die "Couldn't open $oldfile";
open(NEW,">$newfile") or die "Couldn't write to $newfile";
while () {
   my ($date, $a, $b, $c, $d) = split " ";
   if (! $dateold) {
      print NEW "$_";
   } elsif ($date) {
      my ($datedelta) = $dateold - $date;
      my ($flag) = 0;
      foreach $delta ($a, $b, $c, $d) {
         if ($delta > $bandwidth) {
            printf STDERR
               "error at $date     %02d-%02d-%4d %02d:%02d %7d %7d %7d %7d\n",
               (localtime ($date))[4] + 1,
               (localtime ($date))[3],
               (localtime ($date))[5] + 1900, 
               (localtime ($date))[2,1], $a, $b, $c, $d; 
            print NEW "$date 0 0 0 0\n";
            $flag = 1;
            last;
         }
      }
      print NEW "$date $a $b $c $d\n" if (! $flag);
   }
   ($dateold, $aold, $bold, $cold, $dold) = ($date, $a, $b, $c, $d);
}
close(OLD);
close(NEW);