#!/bin/bash
# This script converts hh:mm:ss (3 times 2 decimal digits, hh in range 0-20 mm
# in range 0-60 and ss in range 0-60) in file
# messages_160531_2300_g0n_TIMEShhmmss one entry per line, to sec in normal
# range of integers, and most importantly for my case of comparison of logs and
# network traces, getting the relative period of time offset from the starting
# time for each line.
#
# Doing it in haste, will be dirty.
#
if [ $# -eq 0 ]; then
	echo "Takes three args."
	echo "$1 is the section of the logs corresponding to either of the traces"
	echo "dump_160531_2119_g0n.pcap dump_160531_2145_g0n.pcap or dump_160531_2253_g0n.pcap"
	echo "They need to be manually cut."
	echo "$2 is the time of 'exec of /usr/local/bin/uncenz-1st'"
	echo "That surely can be gotten auto, but only three values there, so..."
	echo "Must be in hh:mm:ss as in the logs."
	echo "$2 will become the $zero_value_raw in the script"
	echo "$3 is the number of seconds btwn that time and the time"
	echo "that is recorded in the dumpcap (use capinfos) as 'First packet time'"
	echo "$3 will become $offset in the script"
	echo "Haste here, can't check the script in details."
	exit 0
fi

logs_section=$1
echo "\$logs_section: " $logs_section
zero_value_raw=$2
echo "\$zero_value_raw: " $zero_value_raw 
offset=$3
echo "\$offset: " $offset
#read FAKE;
# 2 PROBLEMS HERE
zero_value=""	# necessary?
# Nulling previous runs:
> zero_value	# necessary?
echo "\$zero_value: " $zero_value
#read FAKE;
> sec_rel
echo "\$sec_rel: " $sec_rel

zero_value_sec=""
echo "Must calculate \$zero_value_sec, $zero_value_sec, before the loop."
#read FAKE;

	hh=$(echo $zero_value_raw|cut -d: -f1);
	mm=$(echo $zero_value_raw|cut -d: -f2);
	ss=$(echo $zero_value_raw|cut -d: -f3);
	echo the values are $hh $mm $ss;
#read FAKE;
	zero_value_sec=$(echo $hh*3600 + $mm*60 + $ss|bc)
	echo "\$zero_value_sec: " $zero_value_sec
#read FAKE;

cat $logs_section | awk '{ print $3 }' > ${logs_section}_TIMEShhmmss
for i in $(cat ${logs_section}_TIMEShhmmss); do
	hh=$(echo $i|cut -d: -f1);
	mm=$(echo $i|cut -d: -f2);
	ss=$(echo $i|cut -d: -f3);
	echo $i is $hh $mm $ss;
#read FAKE;
	sec=$(echo $hh*3600 + $mm*60 + $ss|bc)
	echo $sec
#read FAKE;
	# However we only need seconds relative to 21:19:44
	# So we need the sec of the first entry to subtract it from any later time
	echo "\$zero_value at start of loop: " $zero_value
	echo "\$zero_value_sec at start of loop: " $zero_value_sec
	if [ "$zero_value" != "$zero_value_sec" ]; then
		if [ i=="zero_value_raw" ]; then
			echo $sec > zero_value ;
			zero_value=$(cat zero_value)
			echo "\$zero_value_sec in the if constuct: " $zero_value_sec
			echo "\$zero_value in the if constuct: " $zero_value
		fi ;
	fi
	echo "\$zero_value at end of loop: " $zero_value
	echo "\$zero_value_sec at end of loop: " $zero_value_sec
	# relative to the start of uncenz-1st script
	sec_rel_uncenz=$(echo $sec-$zero_value|bc)
	echo "\$sec_rel_uncenz: " echo $sec_rel_uncenz
	# the difference btwn the starts of uncenz scripts in how Wireshark (with
	# capinfos I found) reckons the timing in the traces are btwn 5 and 6
	# seconds. I take $offset.
	sec_rel=$(echo $sec_rel_uncenz-$offset|bc)
	echo "\$sec_rel: " echo $sec_rel
	# End of this hasty conversion, just stow this $sec_rel value into a file
	echo $sec_rel >> sec_rel
#read FAKE;
done
