#!/bin/bash

DATAS="WS/chrono.visite"
MEANS="/dev/shm/means.visite"
MIMAX="/dev/shm/mimax.visite"
CURRP="/dev/shm/currp.visite"
IMAGE="chronovisite.png"

nbs=$(wc -l $DATAS | awk '{ print $1 }')
printf "visite: timings on %.3f runs\n" $(echo "$nbs / 2000" | bc -l)


	# compute the means values 

awk < $DATAS '
	{
	cumul[$1] += $2;
	count[$1] ++;
	}

END	{
	for (foo=0; foo<2000; foo++) {
		# escape from zero-divide
		if (count[foo] > 0)	print foo, cumul[foo]/count[foo]
		else			print foo, 0
		}
	}
# fini
' > $MEANS

#	compute minimum and maximum

awk < $DATAS '
BEGIN	{
	for (foo=0; foo<2000; foo++) {
		mini[foo] = 99999
		maxi[foo] = 0
		}
	}

	{
	if ($2 > maxi[$1])		maxi[$1] = $2
	if ($2 < mini[$1])		mini[$1] = $2
	}

END	{
	for (foo=0; foo<2000; foo++)
		print foo, mini[foo], maxi[foo]
	}
' > $MIMAX

# find the last times

awk < $DATAS '
	{
	current[$1] = $2;
	}

END	{
	for (foo=0; foo<2000; foo++) {
		print foo, current[foo]
		}
	}
' > $CURRP

timestamp=$(date --utc)

gnuplot << __EOC__
set term png    size 1280,720
set output      "${IMAGE}"

set yrange      [ 0.0 : 720.0 ]
set grid        front
set xtics       100
set ytics       60
set xlabel	"Frame number"
set ylabel	"Times (seconds)"
set title       "Visite du lieu inconnu, le ${timestamp}"

plot			\
	"${MIMAX}"  using 1:2 with lines title "Mini" lt rgb "#0000b0"	, \
	"${MIMAX}"  using 1:3 with lines title "Maxi" lt rgb "#b00000"	, \
	"${MEANS}"            with lines title "Mean" lt rgb "#00b000" , \
	"${CURRP}"            with lines title "Current" lt rgb "#000000" 

__EOC__

display ${IMAGE} &


