#!/bin/bash
#
# Simple script to show received data stats
#

# Script dir
COMMON=/home/mpc/Projects/SMEI/Code/web/TelemCGI/scripts

# Include the common script
. "$COMMON/common_time"

V5="/var/smei/new/TelemProcess/VCID5"
RF="/var/smei/new/TelemProcess/log/ranges"
CF="/var/smei/new/TelemProcess/log/contacts"
TF="/var/tmp/rfs.$$"
CTF="/var/tmp/cfs.$$"
RFILES=200
NFILES=100
TAIL=4

TZ="UTC"
export TZ

[ X$1 == "X-long" ] && NFILES=1200
[ X$1 == "X-long" ] && RFILES=2000
[ X$1 == "X-long" ] && TAIL=800

#
# Get the last modified date of a file, in gnu secs since 1970
# Get the file size in bytes
#
get_file_info()
{
    stat -L -c "%Y %s" $1
}

#
# Routine to export a HTML table of status information
#
range_from()
{
	local oel;
	
	echo "<div class=\"tc\">"
	echo "<table border=\"1\">"
	echo "<tr><th rowspan=\"2\">Rev</th><th rowspan=\"2\">Filename</th><th rowspan=\"2\">Rcvd At UoB (UTC)</th><th rowspan=\"2\">Lat.</th><th rowspan=\"2\">Uncompressed Size</th><th colspan=\"2\">Overall Stats</th><th colspan=\"4\">Range Statistics</th></tr>"
	echo "<tr><th>Img Files</th><th>Rej Img</th><th>From</th><th>To</th><th>Duration</th><th>Gap</th></tr>"

	TS_LAST=99999999999
	oel=0
	cts=0

	# Empty the log file
	cp /dev/null $TF

	cd "$RF"

	# Build a date sorted list of the latest vcid5 files
	for f in `ls | sort | tail -$TAIL | xargs -i find "{}" -type f | sort -t _ -k 5 | tail -$RFILES`;
	do
		zcat $f | grep "Cx" | while read s;
		do
			set `echo $f $s`
			echo "$7^$9^$3^$5^$1" >> $TF
		done
	done

	sort -n $TF | tail -$NFILES | while read l;
	do

		set `echo $l | sed -e 's:\^: :g'`

		os=$1
		oe=$2
		ts=$3
		te=$4
		f=$5

		# We're in a sub-shell so store this for later
		# echo "$os | $oe | $ts | $te | $f"

		FILE=`basename $f | cut -f 2- -d '_' | cut -f 1-3 -d '.'`
		SHORT_FILE=`echo $FILE | cut -f 2-6 -d '_'`

		# Get the local file information - date and size
		set `get_file_info $f`
		F_SECS=$1
		F_SIZE=$2

		# The download date from the file name
		D_SECS=`name_to_secs $FILE`
		
		# Human readable latency
		let L_SECS=$F_SECS-$D_SECS
		L_STR=`secs_to_hm $L_SECS`
		
		# Human readable reception date
		RCVD=`secs_to_fmt $F_SECS "%Y-%j&nbsp;%R"`

		# Calculate duration of the range. (os/oe appear to be leap-year buggy)
		TS_SECS=`yds_to_secs $ts`
		TE_SECS=`yds_to_secs $te`
		let T_SECS=$TE_SECS-$TS_SECS
		T_STR=`secs_to_hms $T_SECS`
	
		if [ $TE_SECS -gt $cts ];
		then	
			echo $TE_SECS > $CTF
			cts=$TE_SECS
		fi

		if [ -f "$V5/../log/decode_summary/d5_$FILE.log" ];
		then
			set `egrep "(IMAGES|IMAGE_COUNT): " "$V5/../log/decode_summary/d5_$FILE.log"` 0 N/A
			IMG_CNT=$2
			set `egrep "IMAGE_REJ: " "$V5/../log/decode_summary/d5_$FILE.log"` 0 N/A
			IMG_REJ=$2
			set `egrep "HK_COUNT: " "$V5/../log/decode_summary/d5_$FILE.log"` 0 N/A
			HK_CNT=$2
			set `egrep "HK_REJ: " "$V5/../log/decode_summary/d5_$FILE.log"` 0 N/A
			HK_REJ=$2
			set `egrep "BLOCKS_IN_FILE: " "$V5/../log/decode_summary/d5_$FILE.log"` 0 X
			if [ $2 == X ];
			then
				UNC_SIZE="N/A"
			else
				let UNC_SIZE=$2*1044
			fi
			set `egrep "HK15_TS_START_HR: " "$V5/../log/decode_summary/d5_$FILE.log"` 0 N/A
			if [ $2 != "N/A" ];
			then
				# Adjust to 'real time'
				TS15_FROM="$2&nbsp;$3"
			else
				TS15_FROM="N/A";
			fi

			set `egrep "HK15_TS_END_HR: " "$V5/../log/decode_summary/d5_$FILE.log"` 0 N/A
			if [ $2 != "N/A" ];
			then
				# Adjust to 'real time'
				TS15_TO="$2&nbsp;$3"
			else
				TS15_TO="N/A"
			fi
		else
			# echo "COULDN'T FIND FILE: $V5/../log/decode_summary/d5_$FILE.log"
			IMG_CNT="N/A"
			IMG_REJ="N/A"
			HK_CNT="N/A"
			HK_REJ="N/A"
			UNC_SIZE="N/A"
			TS15_FROM="N/A"
			TS15_TO="N/A"
		fi

		TS_FROM=`echo $TS15_FROM | sed -e 's:-::g' -e 's:&nbsp;::g' -e 's+:++'`
		
                if [ $TS15_FROM == '1969-365&nbsp;23:59' ];
                then
                        TS15_FROM_O="N/A"
		else
			TS15_FROM_O=$TS15_FROM
                fi

                if [ $TS15_TO == '1970-001&nbsp;00:00' ];
                then
                        TS15_TO_O="N/A"
		else
			TS15_TO_O=$TS15_TO
                fi

		# Output the table row

		if [ $oel != 0 ];
		then
			let SLACK=$os-$oel
		else
			SLACK=0
		fi

		# Any late file highlighting
		LHH=""
		if [ $L_SECS -gt 21600 ];
		then
			LHH="bgcolor=\"yellow\""
		fi

		if [ $L_SECS -gt 43200 ];
		then
			LHH="bgcolor=\"red\""
		fi

		REV=`name_to_rev cor_$SHORT_FILE`
		if [ X$REV == X ];
                then
		    REV="?"
                fi

		if [ $SLACK -gt 300 ];
		then
			echo "<tr bgcolor=\"yellow\"><td>$REV</td><td>$SHORT_FILE</td><td>$RCVD</td><td $LHH>$L_STR</td><td align=\"right\">$UNC_SIZE</td><td align=\"right\">$IMG_CNT</td><td align=\"right\">$IMG_REJ</td><td>$ts</td><td>$te</td><td align=\"right\">$T_STR</td><td align=\"right\">$SLACK</td></tr>";
		else
			echo "<tr><td>$REV</td><td>$SHORT_FILE</td><td>$RCVD</td><td $LHH>$L_STR</td><td align=\"right\">$UNC_SIZE</td><td align=\"right\">$IMG_CNT</td><td align=\"right\">$IMG_REJ</td><td>$ts</td><td>$te</td><td align=\"right\">$T_STR</td><td align=\"right\">&lt;&nbsp;300</td></tr>";
		fi

		if [ X$TS15_TO_O != X'N/A' ];
		then
			TS_LAST=`echo $TS15_TO | sed -e 's:-::g' -e 's:&nbsp;::g' -e 's+:++'`
		fi

		if [ $oel -lt $oe ];
		then
			oel=$oe
		fi
	done

	# Calculate the next expected 4 contacts
	# Build a date sorted list of the latest vcid5 files
	cd $CF
	COUNT=0
	TE=`cat $CTF`
	rm $CTF
	NOW=`date +%s`
        ls | sort | grep "txt" | tail -2 | xargs cat | sort -n | while read f
	do
		set -- $f
		RISE=$1
		FALL=$2
		REV=$3
		SITE=$4

		if [ $RISE -lt $TE ];
		then
			continue;
		fi

		[ $SITE == "SVAL" ] && SITE="sval4"
		[ $SITE == "FBKS" ] && SITE="fbks0"
		
		R_STR=`secs_to_fmt $RISE "%Y_%j_%H_%M"`

		GUESS="${SITE}_$R_STR"

		let L_SEC=$NOW-$RISE
		if [ $L_SEC -gt 0 ];
		then
			L_STR=`secs_to_hm $L_SEC`

			# Any late file highlighting
			LHH=""
			if [ $L_SEC -gt 21600 ];
			then
				LHH="bgcolor=\"yellow\""
			fi

			if [ $L_SEC -gt 43200 ];
			then
				LHH="bgcolor=\"red\""
			fi
		else
			L_STR=""
			LHH=""
		fi

		echo "<tr bgcolor=\"pink\"><td>$REV</td><td>$GUESS</td><td>Projection</td><td $LHH>$L_STR</td><td align=right>Unknown</td><td></td><td></td><td></td><td></td><td></td><td></td></tr>"
		let COUNT=$COUNT+1
		if [ $COUNT -eq 8 ];
		then
			break;
		fi
	done

	echo "<tr><th colspan=11 align=center>Generated: `date -u +'%Y-%m-%d %H:%M UTC'`</th></tr>"
	
	echo "</table>"
	echo "</div>"
}

# The calls to the procedure above for each downlink station / filename prefix

echo "<h2>Combined Downloads By Data Range</h2>"
echo "<p></p>"

range_from

echo "<p class=\"last\"></p>"
