#! /usr/bin/perl

###########################################################################
# This script executes a series of ping transfers with varying
# test interval between packets
###########################################################################
#
# Author: Yee-Ting Li, UCL HEP
# 
# Change History:
#  19-07-02: initial release
#  31-07-02: added argument mask parsing
#            changed default prime cache number to 0
#
###########################################################################

require 'ping-cook.pl';

$DEBUG = 1;

if ( $#ARGV < 0 ) {
    &printHelp;
}

# parse through the args
for ( $i=0; $i<$#ARGV+1; $i++ ) {

    if( $ARGV[$i] =~ /^-(.*)/ ) {

	$switch = $1;

	# connection host
	if ( $switch eq 'd' ) {
	    $destip = $ARGV[$i+1];
	}
	elsif ( $switch eq 'n' ) {
	    @pktss = split( /,/, &grab_input( $ARGV[$i+1]) );
#	    $pkts = $ARGV[$i+1];
	}
	elsif ( $switch eq 's' ) {
	    $size = $ARGV[$i+1] ;
	}
	elsif ( $switch eq 'f' ) {
	    $outfile = $ARGV[$i+1];
	}
	elsif ( $switch eq 'i' ) {
#	    @intervals = split( /,/, $ARGV[$i+1] );
	    $interval = $ARGV[$i+1];
	}
	elsif ( $switch eq 'c' ) {
	    $primeCacheNumber = $ARGV[$i+1];
	}
	elsif ( $switch eq 'h' ) {
	    &printHelp;
	}


    }


}


# go through an put default values
if ( $destip eq '' ) { &printHelp; }
if ( $size eq '' ) { $size = 1400; }
if ( $interval eq '' ) { $interval = 500; }
if ( $primeCacheNumber eq '' ) { $primeCacheNumber = 0; }
if ( $#pktss eq -1  ) { 
    @pktss = ( '1', '2', '3', '4', '5', '6' , '7', '8', '9', '10', '15', '20' , '50', '100' ); 
}


if ( $DEBUG ) { print "\ndest: $destip, packet size: $size, pkt interval: $interval\nnumber of packets: @pktss\n" ; }




$sourceip = `hostname`; chomp($sourceip);
$PING = `which ping`; chomp( $PING );

### Setup to go

# Get the time and date
my ($sec, $minutes, $hour, $mday, $month, $year, $wday, $yday, $isdst) = gmtime();

# create a directory based on dd-mm-yy in working directory
$dir = sprintf "%2.2d-%2.2d-%4d", $mday, $month+1, $year+1900;
`mkdir $dir \&> /dev/null`;

$count = 1;
$total = $#pktss + 1;

@files;

print STDERR '=' x 72 . "\n";

   $interval = $interval / 1000;

foreach my $pkts (@pktss) {

   print STDERR "Conducting test number of pings $pkts ($count/$total)\n";


   
    # get time again
    ($sec, $minutes, $hour, $mday, $month, $year, $wday, $yday, $isdst) = gmtime();

    # create a filename to store output
    $filename = sprintf "$dir\/ping_%4d%2.2d%2.2d-%2.2d%2.2d%2.2d_" . "$sourceip" . "_" . "$destip" . "_i" . $interval . '_s' . $size . "_n" . "$pkts" .".log", $year+1900, $month+1, $mday, $hour, $minutes, $sec;


    # add filename to array
    push ( @files, $filename );
    $ping_cmd = "echo \"interval: $interval\" \&> $filename;$PING $destip -s $size -c $primeCacheNumber -i $interval \&>/dev/null;$PING $destip -s $size -c $pkts -i $interval >> $filename";

#    print $ping_cmd;

    # execute iperf
    system( $ping_cmd );

    $count++ ;
}

print STDERR '=' x 72 . "\n";
print STDERR "Tests complete!\n";
print STDERR "\nCooking...\n";
print STDERR '=' x 72 . "\n";



if ( $outfile eq '' ) {
    &pingCook( @files );
}
else {
    
    open(SAVEOUT, ">&STDOUT");
    open (STDOUT,">$outfile") || warn "SSH_Open: Could not redirect STDOUT to $filename: $!\n";
    select(STDOUT); $| = 1;	
    &pingCook( @files );   
    # redirect stdout to stdout
    open(STDOUT, ">&SAVEOUT");
    open ( OUT, "<$outfile" );
    while ( <OUT> ) {
	print $_;
    }

}
print STDERR '=' x 72 . "\n";

print STDERR "\nComplete!\n";


sub printHelp{
    print "Usage: do_number.pl -d <sink> [-s <packet size>(bytes)] [-c <prime cache number>] [-i <interval>(msec)] [-n <n1,n2,n3...>]\n";
    exit;
}



