#! /usr/bin/perl

sub pingCook
{

    my ( @filenames ) = @_;

    print "date\tsource\tsink\tpacket_size\tinterval\tfraction_lost\tpkts_sent\tpkts_rcvd\tmin_rtt\tmax_rtt\tave_rtt\tstdev_rtt\tsequence\trtts\tttl\n";

    foreach my $file (@filenames) {

	
	my $udpPort;
	my $pktSize;


	my ( $temp, $date, $source, $sink ) = split /_/, $file;
	
	open ( PING, "<$file");
	
	my $pingI=0;
	my @seq;
	my @ttl;
	my @rtt;
	my $min;
	my $avg;
	my $max;
	my $pkts_sent;
	my $pkts_rcvd;
	my @pkt_size;
	my $minrtt;
	my $maxrtt;
	my $avertt;
	my $interval;
	
	while (<PING>) {
	    
#	print "@_";
	    
	    if (/^(\d+) packets transmitted, (\d+) /) {
		$pkts_sent = $1;
		$pkts_rcvd = $2;
	    }
	    elsif (/(\d+\.?\d*)\/(\d+\.?\d*)\/(\d+\.?\d*)/) {  #look for num/num/num
		$min = $1;
		$avg = $2;
		$max = $3;
	    }
	    elsif (/^(\d+).*seq=(\d+).*ttl=(\d+).*time=(\d+\.?\d*) ms/) {
		# look for packet size seq=NN and time=NN
		$pkt_size[$pingI]=$1;
		$seq[$pingI]=$2;
		$ttl[$pingI]=$3;
		$rtt[$pingI]=$4;
		$pingI++;
	    }
	    elsif (/^(\d+).*seq=(\d+).*ttl=(\d+).*time=(\d+\.?\d*) usec/) {
		# look for packet size seq=NN and time=NN
		$pkt_size[$pingI]=$1;
		$seq[$pingI]=$2;
		$ttl[$pingI]=$3;
		$rtt[$pingI]=$4/1000;
		$pingI++;
	    }
	    elsif( /^interval: (\d+\.?\d*)/ ) {
		$interval = $1;
	    }
	}
	
	
	close ( PING);

	
	my $perc_lost;
	if ( $pkts_sent > 0 ) {
	    $perc_lost = 1 - ( $pkts_rcvd / $pkts_sent  ) ;
	}
	my $pkt_size = $pkt_size[0];
	
	if ( !defined $minrtt ) {
	    ( $minrtt, $maxrtt, $avertt ) = &getMinMaxAve( @rtt );
	}
	
	# work out the standard deviation
	my $stddev;
	if( $#rtt > 0 ) {
	    $stddev = &getStandardDeviation( @rtt );
	}	
	
	
	my ( $sourceAdd, $sinkAdd ) = &pingGetIPs( $source, $sink );
	

	$output = sprintf( "$date\t$source:$sourceAdd\t$sink:$sinkAdd\t$pkt_size\t$interval\t%.2f\t$pkts_sent\t$pkts_rcvd\t%.3f\t%.3f\t%.3f\t%.3f\t@seq\t@rtt\t@ttl", $perc_lost,$minrtt,$maxrtt,$avertt,$stddev );
	
	print $output;

	print "\n";

    }
    
}




sub pingGetIPs {

    local ( $source, $sink ) = @_;

    # find out the IP addresses of source and destination
    my ($true_name,$aliases,$addrtype,$addrlength,@addrs) = gethostbyname($source);
    if (!defined $true_name) {
	print STDERR "+++Data_GetIPs: Error! Can not resolve $source to IP address\n";
    }
    else {
	($a, $b, $c, $d) = unpack('C4', $addrs[0]);
	$src_ip = "$a.$b.$c.$d";
    }
    
    my ($true_name,$aliases,$addrtype,$addrlength,@addrs) = gethostbyname($sink);
    if (!defined $true_name) {
	print STDERR "+++Data_GetIPs: Error! Can not resolve $sink to IP address\n";
    }
    else {
	($a, $b, $c, $d) = unpack('C4', $addrs[0]);
	$sink_ip = "$a.$b.$c.$d";
    }
    
    if ( $_debug eq 10 ) {
	print "+Data_GetIPs: source ip: " . $src_ip . "\t" . "sink ip: " . $sink_ip . "\n";;
    }
    
    return my @ips = ($src_ip, $sink_ip);
    
}




###
# &Data_getMinMaxAve( @array )
###
# takes in an array of bits and spits min, max and ave values
###
#
sub getMinMaxAve
{
    my @array = @_;

    my $minVal =''; 
    my $maxVal = '';
    my $totalVal = '';
    my $ave ='';

#    print "-->@array, $#array\n";

    foreach my $item (@array) {

#	print "   item: $item\n";
	if( $item < $minVal || $minVal eq '' ) {
	    $minVal = $item;
	}
	
	if( $item > $maxVal || $maxVal eq '' ) {
	    $maxVal = $item;
	}

	$totalVal += $item;
    }

    if ( $#array > 0 ) {
	$aveVal = $totalVal / ($#array+1);
    }

    $minVal = sprintf( "%.4f", $minVal );
    $maxVal = sprintf( "%.4f", $maxVal );
    $aveVal = sprintf( "%.4f", $aveVal );

    return ( $minVal, $maxVal, $aveVal );
}



###
# &Data_getStandardDeviation( @array )
###
# takes in an array of bits and spits out the standard deviation of the array
###
#
sub getStandardDeviation
{
    my @array = @_;

#    print "@array\n";

    my $totalVal;
    my $ave ='';
    my @diff;

    for ( my $i=0; $i<$#array+1 ; $i++ ) {
	$totalVal += $array[$i];
    }
    if ( $#array > 0 ) {
	$aveVal = $totalVal / ($#array+1);
    }

#    $aveVal = sprintf( "%.4f", $aveVal );
#    print "total: $totalVal\n";
#    print "average: $aveVal\n";

    for  ( my $i=0; $i<$#array+1 ; $i++ ) {
	# find the difference
	$diff[$i] = $array[$i] - $aveVal;
	# sqaure it
	$diff[$i] *= $diff[$i];
    }

#    print "diffs: @diff\n";

    my $totalDiffs;

    foreach my $item (@diff) {
	$totalDiffs += $item;
    }

    my $standardDeviation = 0.000;

    # to get the standev
    if ( $#diff > -1 ) {
	$standardDeviation = $totalDiffs / ($#diff+1) ;
	$standardDeviation = sqrt( $standardDeviation );
    }
    
    $standardDeviation = sprintf( "%.4f", $standardDeviation );
  
    return $standardDeviation;
}




sub grab_input {
    my $arg = @_[0];

    my @args = split /\,/, $arg;

    my $output;

    for( my $a=0; $a<=$#args; $a++ ) {
	my $this =  &parse( $args[$a] );
	if ( $a < $#args ) {
	    $output = sprintf( $output . "$this," );
	}
	else {
	    $output = sprintf( $output . "$this" );
	}
    }

    # print "\n" . $output . "\n" ;
    return $output;
}

sub parse
{
  my $argument = @_[0];
  my ( $startend, $interval ) = split /\:/, $argument;

  my ( $start, $end ) = split /\.\./, $startend;

  my $line;

  if ( $start ne '' && $end ne '' && $interval ne '' ) {
      for ( my $i=$start; $i<=$end; $i+=$interval ) {
	  
	  $line = sprintf( $line . $i );
	  unless ( ($i+$interval)>$end ) {
	      $line = sprintf( $line . ',' );
	  }
	  
      }
  }
  else {
      $line = $argument;
  }

  return $line;

}

1;
