#!/bin/bash

FILE=bbb

get_hours () {
    line="$1"
    echo "$line" | cut -c 23-24
}

get_minutes () {
    line="$1"
    echo "$line" | cut -c 26-27
}

get_seconds () {
    line="$1"
    echo "$line" | cut -c 29-30
}

get_total_minutes () {
    line="$1"
    hh=$(get_hours "$line")
    hh=${hh#"0"}
    mm=$(get_minutes "$line")
    mm=${mm#"0"}
    ss=$(get_seconds "$line")
    ss=${ss#"0"}
    tt=$((hh * 3600 + mm * 60 + ss))
#    echo "hh=$hh mm=$mm ss=$ss tt=$tt" >&2
    echo $tt
}

get_deltas () {
    var="$1"
    sss=0
    last_sss=none
    cat "$FILE" | grep -v "592TXR" | grep "$var" | while read -r line
    do
	sss=$(get_total_minutes "$line")
#	echo "sss=$sss  last_sss=$last_sss" >&2
	if [[ "x$last_sss" == "xnone" ]]; then
	    DDD="0"
	else
	    DDD=$((sss - last_sss))
	fi
	MMM=$((DDD / 60))
	SSS=$((DDD % 60))
	printf '%3d:%02d' $MMM $SSS
	echo " $line"
#	echo "DDD=$DDD " 
	last_sss=$sss
    done
}

blanks='^  0:00 *{'
separator="--------------------------"
get_deltas temperature | grep -v "$blanks"
echo "$separator"
get_deltas lux | grep -v "$blanks"
echo "$separator"
get_deltas rain | grep -v "$blanks"


# eee eof
