#!/usr/bin/perl
#
# This file is a part of EPAN - the ethernet protocol analyzer
#
# You can use this file to pass the original or preprocessed data to
# other applications like gnuplot.

require 5.000;

# use strict;

my $debug = 0;

my ($time_peak, $maxbps, $speed, $progf, $cnt, $i, @tmp, @fps, %data);
my ($uti_peaka, $uti_peakv, $uti_peakt);
my ($bps_peaka, $bps_peakv, $bps_peakt);
my ($fps_peaka, $fps_peakv, $fps_peakt);


# basename
$progf = $0;
$0 =~ s#.*/##;

while ($ARGV[0] =~ m/^-/) {
  $_ = shift(@ARGV);
  if (/--speed$/) {
    $speed = shift(@ARGV);
    die "$0: Option \`--speed' requires an argument\n" unless ($speed and not
      ($speed =~ m/^--/)); 
    $speed -= 0.08 * $speed;		# estimated overhead
    $maxbps = $speed / 8 / 100;		# speed / 8 / 100
  } else {
    print "$0: Unknown option: $_\n";
    exit(10);
  }
}

if (not defined($maxbps)) {
    $maxbps = 12500;			# Ethernet
}

if ($#ARGV != 0) {
    print "Usage: $0 filename\n";
    exit(1);
}

# read data file
open(RFILE, $ARGV[0]) || die "$0: Can't open datafile: $!\n";
while(<RFILE>) {
    @tmp = split(' ');	# split line in x and y value
    $data{$tmp[0]} += $tmp[1];	# add all y values for the same x value
    $fps[$tmp[0]]++;		# count how many x values have been added
}
close(RFILE);

foreach (keys %data) {
    $data{$_} = $data{$_} / $maxbps;	# utilization
    if ($data{$_} > 100) {
        $data{$_} = 100;		# this may happen due to time offsets
    }

    if ($data{$_} >= $uti_peakv) {
        $uti_peakv = $data{$_};		# save highest utilization value (y)
        $uti_peakt = $_;		# save time of highest utilization (x)
    }
    if ($_ >= $time_peak) {
        $time_peak = $_;		# save highest time value
    }

    $uti_peaka += $data{$_};		# count all bytes (for av. utilization)
    $cnt++;
}

$time_peak = 1 if ($time_peak == 0);	# if we have frames time can't be 0

$uti_peaka /= $time_peak;		# calculate average utilization

foreach $i (0..$time_peak) {
  if (not exists $data{$i}) {
    $data{$i} = 0;		# set the y value of undefined x values to 0
  }

  if ($fps[$i] >= $fps_peakv) {
    $fps_peakv = $fps[$i];		# save highest FPS value
    $fps_peakt = $i;			# save time of highest FPS value
  }
  $fps_peaka += $fps[$i];		# count all frames (for av. FPS)
}
$fps_peaka /= $time_peak;		# calculate average FPS

$bps_peaka = $uti_peaka * $maxbps;	# calculate average BPS
$bps_peakv = $uti_peakv * $maxbps;	# calculate peak BPS
$bps_peakt = $uti_peakt;		# save time of highest BPS value

# save results
open(WFILE, ">$ARGV[0].sum") || die "$0: can't open savefile: $!\n";
printf WFILE "#UTI: %.2f %.2f %d\n", $uti_peaka, $uti_peakv, $uti_peakt;
printf WFILE "#BPS: %.2f %d %d\n", $bps_peaka, $bps_peakv, $bps_peakt;
printf WFILE "#FPS: %.2f %d %d\n#\n", $fps_peaka, $fps_peakv, $fps_peakt;

foreach (sort {$a <=> $b} keys %data) {
   printf WFILE "%-d %-.2f\n", $_ , $data{$_};
}
close(WFILE);

# print additional information on the screen
if ($debug != 0) {
    print STDERR "DEBUG: Time peak value    :  $time_peak\n";
    print STDERR "DEBUG: Number of values   :  $cnt\n";
    print STDERR "DEBUG: -------------------:\n";
    print STDERR "DEBUG: Average Utilization:  $uti_peaka\n";
    print STDERR "DEBUG: Peak Utilization   :  $uti_peakv\n";
    print STDERR "DEBUG: Peak Utilization at:  $uti_peakt\n";
    print STDERR "DEBUG: -------------------:\n";
    print STDERR "DEBUG: Average BPS        :  $bps_peaka\n";
    print STDERR "DEBUG: Peak BPS           :  $bps_peakv\n";
    print STDERR "DEBUG: Peak BPS at        :  $bps_peakt\n";
    print STDERR "DEBUG: -------------------:\n";
    print STDERR "DEBUG: Average FPS        :  $fps_peaka\n";
    print STDERR "DEBUG: Peak FPS           :  $fps_peakv\n";
    print STDERR "DEBUG: Peak FPS at        :  $fps_peakt\n";
}

# system("echo \"set grid;set nokey;plot '$ARGV[0].s' matrix smooth uniq with lines\" | gnuplot -persist");

# system("echo \"set grid;set nokey;plot '$ARGV[0].s' matrix smooth uniq with impulses\" | gnuplot -persist");

# system("echo \"set grid;set nokey;plot '$ARGV[0].s' matrix smooth uniq with boxes\" | gnuplot -persist");

