#!/bin/bash

getval() {
    zcat -f -- "$1" \
    | grep -e '^\[main/' \
    | awk '
      /\| Load/ { total_load = $(NF - 4); load_dev = $NF; next; }
      /\| Computation/ { comp_sum = $(NF - 4); next; }
      /\| Data send amount/ { data_sum = $(NF - 4); next; }
      /\| Ctrl send count/ { ctrl_sum = $(NF - 4); next; }
      /\| Total simulated time/ { duration = $NF; next; }
      /caught CTRL-C/ { interrupted = 1; nextfile; }
      /Simulation succeeded/ { success = 1; next; }
      END{
        ok = success && !interrupted
        if (ok)
            print total_load, load_dev, comp_sum,
                  data_sum, ctrl_sum, duration;
        else
            print "n/a", "n/a", "n/a", "n/a", "n/a", "n/a"
        exit (ok? 0: 1);
      }
    '
}

format='%-75s %6s %11s %11s %11s %11s %9s\n'
printf "$format" \
    "# Experimentation" \
    "Total" "Stddev" "Comput." \
    "Data amnt" "Ctrl mesg" "Duration"
for file; do
    name=${file%%.gz}
    name=${name%%.out}
    if values=$(getval "$file"); then
        printf "$format" "$name" $values
    else
        printf "$format" "#$name" $values
    fi
done