#!/usr/bin/perl

$max_seq = 0;

for($i = 0; $i < 1000; $i++) {
    $max_radio[$i] = 0;
    $min_radio[$i] = 10000;
}

while(<>) {

    if(/P ([\d\.]+) (\d+) (\d+) (\d+) (\d+) (\d+) \d+ \d+ (\d+) (\d+) (\d+) (\d+)/) {
        $node = $1;
        $seq = $2;
        $all_cpu = $3;
        $all_lpm = $4;
        $all_tx = $5;
        $all_rx = $6;
        $cpu = $7;
        $lpm = $8;
        $tx = $9;
        $rx = $10;

        $nodes{$node} = 1;
        
        $radio_now = $tx + $rx;
        $cpu_now = $lpm + $cpu;
        $dutycycle = $radio_now / $cpu_now;
        
        $dutycycle_for_node[$node][$seq] = $dutycycle;
        
#            print STDERR "Node $node Seq $seq duty cycle " . ($tx + $rx)/($lpm + $cpu) . ", " . ($tx + $rx) . "/" . ($lpm + $cpu) . "\n";
        $radio[$seq] += $radio_now;
        $time[$seq] += $cpu_now;
        
        if($seq >= 0) {
            $mean += $dutycycle;
            $num_mean++;
        }
        
        if($dutycycle > $max_radio[$seq]) {
            $max_radio[$seq] = $dutycycle;
        }
        
        if($dutycycle < $min_radio[$seq]) {
            $min_radio[$seq] = $dutycycle;
        }
        if($seq > $max_seq) {
            $max_seq = $seq;
        }

    }
}

for($i = 0; $i < $max_seq; $i++) {
    if($time[$i] != 0) {
        print "$i " . $radio[$i] / $time[$i] . " " . $min_radio[$i] . " ". $max_radio[$i] . " ";
        foreach $j (keys %nodes) {
            print $dutycycle_for_node[$j][$i] . " ";
        }
        print "\n";
    }
}

foreach $j (keys %nodes) {
    $avg = 0;
    for($i = 0; $i < $max_seq; $i++) {
        $avg += $dutycycle_for_node[$j][$i];
    }
    print STDERR "Node $j avg duty cycle " . 100 * $avg / $max_seq . "\n";
}

if($num_mean != 0) {
    print STDERR "Mean duty cycle " . 100 * $mean / $num_mean . "\n";
}
