#!/bin/sh

input=$1
extimes=10
fcount=8
M=1
J=1

if [ $# -le 0 -o ! -f $input ]
then
  echo "usage: analyze inputfile [outputile]"
  exit 1
fi

if [ $# == 2 ]
then
  outfile=$2
else
  if [ -f analyze.out ]
  then
    rm analyze.out
  fi
  outfile=analyze.out
fi

while [ $M -le $fcount ]
do
  N=1
  sum=0

  while [ $N -le $extimes ]
  do
    set `cat $input | tail +$J | head -1`
    value=$4
    if [ $5 == "kB/s" ]
    then
      echo 'scale=2;' $value/1024 > dat.$$
      value=`bc < dat.$$`
    fi
    echo  $sum+$value > dat.$$
    sum=`bc < dat.$$`
    N=`expr $N + 1`
    J=`expr $J + 1`
  done

  echo 'scale=2;' $sum/$extimes > dat.$$
  sum=`bc < dat.$$`
  echo $1 "    " $2 $3 $sum $5 >> $outfile
  M=`expr $M + 1`
done

rm dat.$$