#!/bin/bash
#
# Simple script to show received data xfer rates
#

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

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

CF="/var/smei/new/TelemProcess/log/contacts"

TZ="UTC"
export TZ

#
# Routine to export a HTML table of status information
#
table_support()
{
	echo "<div class="tc">"
	echo "<table border=\"1\">"
	echo "<tr><th colspan=3 rowspan=2>Support Information</th><th colspan=2>Timing Data</th><th colspan=4 rowspan=2>VCID5 Information</th></tr>"
	echo "<tr><th>Duration</th><th>Gap</th></tr>"
	echo "<tr><th>Rev</th><th>Site</th><th>Date</th><th>MM:SS</th><th>HH:MM:SS</th><th>Files</th><th>Bytes</th><th>Since</th><th>Status</th></tr>"

	# Loop showing projections
        FROM=`date -d "144 hours ago" +%s`
	UNTIL=`date -d "+6 hours" +%s`
        NOW=`date +%s`
	LAST=0
	cd $CF
        ls | grep "^20" | sort | tail -5 | xargs cat | sort -n | uniq | while read f
        do
                set -- $f
                RISE=$1
                FALL=$2
                REV=$3
                SITE=$4

                if [ $RISE -le $FROM ];
                then
                        continue;
                fi

		if [ $RISE -ge $UNTIL ];
		then
			break;
		fi

		if [ $RISE -le $NOW ];
		then
			HL="bgcolor='#d0d0d0'"
		else
			HL=""
		fi

                [ $SITE == "SVAL" ] && SITE="sval4"
                [ $SITE == "FBKS" ] && SITE="fbks0"

                R_STR=`secs_to_fmt $RISE "%Y_%j_%H_%M"`

		let DUR_S=$FALL-$RISE
		DUR_STR=`secs_to_fmt $DUR_S '%M:%S'`

		if [ $LAST -gt 0 ];
		then
			let GAP_S=$RISE-$LAST
			GAP_STR=`secs_to_hms $GAP_S`
			GAP_H=""
			
			if [ $GAP_S -gt 21600 ];
			then
				GAP_H="bgcolor='yellow'"
			fi

			if [ $GAP_S -gt 43200 ];
			then
				GAP_H="bgcolor='red'"
			fi

		else
			GAP_STR="-"
			GAP_H=""
		fi

		# Do the file accounting

		let SINCE=$NOW-$FALL
		SINCE_T=`secs_to_hm $SINCE`
		
		if [ $SINCE -gt 10800 ];
		then
			STAT_T="Late"
		else
			STAT_T=""
		fi

		if [ $SINCE -lt 0 ];
		then
			STAT_T=""
			SINCE_T=""
			SINCE=0
		fi


		let RISE_M=$RISE-300
		let FALL_M=$FALL+300
		FILE_LIST=`files_between $RISE_M $FALL_M`
		if [ "X$FILE_LIST" == "X" ];
		then
			FILE_CNT="0"
			BYTE="0"
			BYTE_H="bgcolor=yellow"
			STAT_S="Short"
		else
			FILE_CNT=`echo $FILE_LIST | wc -w`
			BYTE=0
			
			for F in $FILE_LIST;
			do
				S=`name_to_secs $F`
				YD=`secs_to_fmt $S "%Y_%j" | cut -c 1-7`
				SZ=`stat -c "%s" "/var/smei/new/TelemProcess/VCID5/$YD/$F"`

				let BYTE=$BYTE+$SZ
			done

			if [ $BYTE -lt 70000000 ];
			then
				BYTE_H="bgcolor=yellow"
				STAT_S="Short"
			else
				BYTE_H=""
				STAT_S=""
			fi
		fi

		if [ "X$STAT_S" == "X" ];
		then
			STAT="OK"
			STAT_H=""
		else
			if [ "X$STAT_T" == "X" ];
			then
				STAT="Short&nbsp;Data&nbsp;Files"
				STAT_H="bgcolor=yellow"
			else
				STAT="Short&nbsp;and&nbsp;Late&nbsp;Data&nbsp;Files"
				STAT_H="bgcolor=red"
			fi

			if [ $SINCE == 0 ];
			then
				STAT="Predicted Pass"
				STAT_H=""
				BYTE="-"
				BYTE_H=""
				FILE_CNT="-"
			fi
		fi

                echo "<tr $HL><td>$REV</td><td>${SITE}</td><td>$R_STR</td><td align=center>$DUR_STR</td><td align=center $GAP_H>$GAP_STR</td><td>$FILE_CNT</td><td $BYTE_H>$BYTE</td><td>$SINCE_T</td><td $STAT_H>$STAT</td></tr>"

		# Remember for next time
		LAST=$FALL
        done

	echo "<tr><th colspan=9 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

transfer_status

echo "<h2>Support Summary</h2>"
echo "<p></p>"

table_support

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