#!/bin/bash

if [ ! -d logs/ ]; then
mkdir logs;

echo "Generating log data..."

# compare high-level and cpu schedulers
for hs in {1..4}; do
	for cs in 0 1 2; do
		echo "hs: $hs, cs: $cs";

		# Data for "geheugentoewijzing", "first cycle" and
		# "totale verwerkingstijd" 
		for load in $(seq 0.5 0.01 0.99); do
			cpu=$load;
			io=$load;
			mem=$load;
			for ps in 1000; do
				for slice in 100; do
					#echo $cpu $io $mem $ps $hs $cs $slice
					echo $cpu $io $mem $ps $hs $cs $slice|./scheduler \
						&> logs/$cpu-$io-$mem-$ps-$hs-$cs-$slice.log 
				done
			done
		done
		
		# Data for "executie-tijd vanaf geheugentoewijzing"
		for load in $(seq 0.5 0.01 0.99); do
			cpu=$load;
			io=$load;
			mem=0.70;
			for ps in 1000; do
				for slice in 100; do
					#echo $cpu $io $mem $ps $hs $cs $slice
					echo $cpu $io $mem $ps $hs $cs $slice|./scheduler \
						&> logs/$cpu-$io-$mem-$ps-$hs-$cs-$slice.log  
				done
			done 
		done
	done
done
fi;

for log in high-level first-cycle run-time total-time; do
	rm -f logs/$log.log;
done

echo "Parsing log data..."

echo "Generating data for 'wachttijd op geheugentoewijzing'..."

for log in `ls -1 logs/*-0-100.log | sort`; do

	mem=`echo $log|cut -d- -f1| sed "s/[^0-9.]//g"`;
	hs=`echo $log|cut -d- -f5`;

	if [ "$mem" != "`echo $log|cut -d- -f3`" ]; then
		continue;
	fi
	
	echo -n "$hs $mem " >> logs/high-level.log;
	grep -m 1 "Gemiddelde waarde:" $log | cut -d, -f1 | sed -E "s/[^0-9.]//g" \
		>> logs/high-level.log;

done

echo "Generating data for 'wachttijd op eerste CPU cycle'..."

for log in `ls -1 logs/*-4-*-100.log | sort`; do

	cpu=`echo $log|cut -d- -f1| sed "s/[^0-9.]//g"`;
	cs=`echo $log|cut -d- -f6`;

	if [ "$cpu" != "`echo $log|cut -d- -f3`" ]; then
		continue;
	fi

	echo -n "$cs $cpu " >> logs/first-cycle.log;
	grep -m 2 "Gemiddelde waarde:" $log | cut -d, -f1 | sed -E "s/[^0-9.]//g" \
		| tail -n 1 >> logs/first-cycle.log;

done

echo "Generating data for 'executie-tijd vanaf geheugentoewijzing'..."

for log in `ls -1 logs/*-4-*-100.log | sort`; do

	cpu=`echo $log|cut -d- -f1| sed "s/[^0-9.]//g"`;
	cs=`echo $log|cut -d- -f6`;

	if [ "$cpu" == "`echo $log|cut -d- -f3`" ]; then
		continue;
	fi

	echo -n "$cs $cpu " >> logs/run-time.log;
	grep -m 3 "Gemiddelde waarde:" $log | cut -d, -f1 | sed -E "s/[^0-9.]//g" \
		| tail -n 1 >> logs/run-time.log;

done

echo "Generating data for 'totale verwerkingstijd'..."

for log in `ls -1 logs/*-100.log | sort`; do

	load=`echo $log|cut -d- -f1| sed "s/[^0-9.]//g"`;
	hs=`echo $log|cut -d- -f5`;
	cs=`echo $log|cut -d- -f6`;

	if [ "$load" != "`echo $log|cut -d- -f3`" ]; then
		continue;
	fi

	echo -n "$hs $cs $load " >> logs/total-time.log;
	grep -m 4 "Gemiddelde waarde:" $log | cut -d, -f1 | sed -E "s/[^0-9.]//g" \
		| tail -n 1 >> logs/total-time.log;

done
