#!/bin/bash

prev=0
offset=5
value=0

cat ch13.txt | cut -d ' ' -f 4 | cut -c 14- | sed -r 's/:/ /g' | while read h m s ; do
	new=$((h * 60 * 60 + m * 60 + s))
	if [ $prev -ne 0 ] ; then
		dt=$((new - prev))
		delta=$((dt / 2))
		if [ $offset -eq 0 ] ; then
			[ $dt -eq 2 ] && delta=0
			[ $dt -eq 4 ] && delta=1
		fi
		val=$((delta * (2 ** offset)))
		#echo "dt = $dt ; delta = $delta ; offset = $offset ; value = $val"
		value=$((val + value))
		if [ $offset -eq 0 ] ; then
			[ $dt -eq 0 ] && value=$((value / 2))
			#echo $value
			printf \\$(printf '%03o' $value)
			#echo
			offset=5
			value=0
		elif [ $offset -eq 1 ] ; then
			offset=0
		else
			offset=$((offset - 2))
		fi
	fi
	
	prev=$new
done
