#!/bin/bash
BASEDIR=/Users/rjh/Roborobo/
FULLCOMMAND="$0 $@"

# Define the flags
. ${BASEDIR}shflags
DEFINE_string 'steps' '5' 'The number of lines to read from the output file' 's';
DEFINE_string 'logfile' 'tmp_output' 'logfile to read from' 'l';

# Parse the flags
FLAGS "$@" || exit 1
eval set -- "${FlAGS_ARGV}"

# get the list of fitness values
values=`tail -n ${FLAGS_steps} ${BASEDIR}${FLAGS_logfile} | cut -f1 -d' ' | sed 's/.*e.*/0/'`
#echo "values: $values" > WTF

# compute the average fitness value over the past 5.000 ticks
total=0
n=0

for fitness in $values
do
  n=$(( $n+1 ))
  total=$(echo "scale=6; $total + $fitness" | bc)
done

# write the average fitness value of the past 5.000 ticks to stdout and logfile
#echo "# values: $n" >> ${FLAGS_logfile}_summary
#echo $FULLCOMMAND
echo "scale=6; $total / $n" | bc > ${FLAGS_logfile}_summary
#echo `echo "scale=6; $total / $n" | bc` : $TIMESTAMP.$RID : $FULLCOMMAND >> commands.log