#!/bin/bash
#Author Martin Prudek

#check for empty string
if [ "$#" -lt 1 ]; then
	echo "Enter file name from '../data/' directory.";
	echo "Turning to default: generating statistics for all the data from '../data/'"
	data_dir="../data";
else
	data_dir="$1";
fi

if [ "$2" = "-v" ]; then
	verbose=1;
	echo verbose
else
	verbose=0;
fi

function stats {
for file in `ls "$1"/$2/*.dat`; do
	filename=`echo "${file}" | sed 's:.*/::'`;
	. ./parse_name.sh "${filename}";
	if [ ! "${verbose}" -eq 1 ]; then
		if [ "${nm[t]}" = "can" -a ! "${canp}" = "1" ]; then
			canp="1";
			echo "------------CAN offsets-------------------------------------------------"
			echo -e "HW \t& load \t& kern \t\t\t& nslp \t& avg \t& max \t\t& note"
			echo "------------------------------------------------------------------------"
		fi


		if [ "${nm[t]}" = "lat" -a ! "${latp}" = "1" ]; then
			latp="1";
			echo "----------Latency offsets-----------------------------------------------"
			echo -e "HW \t& load \t& kern \t\t\t& nslp \t& avg \t& max \t\t& note"
			echo "------------------------------------------------------------------------"
		fi
	else
		if [ "${nm[t]}" = "can" -a ! "${canp}" = "1" ]; then
			canp="1";
			echo "------------CAN offsets-----------------------------------------------------------"
			echo -e "HW \t& load \t& kern \t\t\t& nslp \t& avg \t& max \t\t& prio \t& si \t& note"
			echo "----------------------------------------------------------------------------------"
		fi

		if [ "${nm[t]}" = "lat" -a ! "${latp}" = "1" ]; then
			latp="1";
			echo "----------Latency offsets---------------------------------------------------------"
			echo -e "HW \t& load \t& kern \t\t\t& nslp \t& avg \t& max \t\t& prio \t& si \t& note"
			echo "----------------------------------------------------------------------------------"
		fi
fi

	#Optional renaming
	ert='';
	if [ "${nm[ert]}" = "CTU" ]; then
        	ert="ert-linux";
		if [ "${#nm[n]}" -ne 0 ]; then
			ert="${ert},"
		fi
	fi

	stats=`cat "${file}" | sed '/^#/ d' | ./comp_statistics`;
	IFSo=${IFS};
	IFS=' '
	stats2=($stats);
	echo -ne "${nm[hw]} \t& ${nm[lo]} \t& ${nm[vk]},${nm[k]} \t\t& ";
	echo -ne "${nm[cn]} \t& ${stats2[0]}\t& ";
	echo -ne  "${stats2[1]}     \t& ";
	if [ "${verbose}" -eq 1 ]; then
		echo -ne  "${nm[p]} \t& ${nm[si]} \t& ";
	fi
	echo -ne  "${ert}${nm[n]}"
	if [ -n "${nm[bn]}" ]; then
		echo ",V${nm[bn]}";
	else
		echo;
	fi
	IFS=${IFSo};
done
}
echo "***Statistiky diferenci mezi casy.***"
canp=0;
latp=0;
stats ${data_dir} "__offsets_diff"
echo "***Statistiky diferenci mezi casy a linearni aproximaci.***"
canp=0;
latp=0;
stats ${data_dir} "__offsets_reg"
