#!/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"
NFILES=25

TZ="UTC"
export TZ

[ X$1 == "X-long" ] && NFILES=1000

#
# Routine to export a HTML table of status information
#
#
# Parameter 1: Filename identifier. (filename 5 character code)
# Parameter 2: Station Identifier (human format)
#
table_from()
{
	SITE=$1
	NAME=$2
	echo "<div class="tc">"
	echo "<table border=\"1\">"
	echo "<tr><th rowspan=2>Rev</th><th rowspan=2>$NAME</th><th rowspan=2>Rcvd On (UTC)</th><th colspan=3>Latency</th><th colspan=2>File Size</th><th colspan=2>Image Files</th><th colspan=2>Housekeeping</th><th colspan=2>Data Extent</th></tr>"
	echo "<tr><th>Orbit</th><th>Ground</th><th>Total</th><th>Comp</th><th>Uncomp</th><th>Valid</th><th>Invalid</th><th>Valid</th><th>Invalid</th><th>From</th><th>To</th></tr>"

	XOLD=`ls $V5 | sort | tail -5 | head -1`
	VOLD=`ls $V5 | sort | tail -4 | head -1`
	PREV=`ls $V5 | sort | tail -3 | head -1`
	LAST=`ls $V5 | sort | tail -2 | head -1`
	THIS=`ls $V5 | sort | tail -1`

	for f in `ls "$V5/$XOLD/"* "$V5/$VOLD/"* "$V5/$PREV/"* "$V5/$LAST/"* "$V5/$THIS/"* | grep "$SITE" | sort | tail -$NFILES`;
	do
	  # Extract the short file name, date and size for later.
	  set `get_file_info $f`
	  F_SECS=$1
	  F_SIZE=$2
	  F_STR=`secs_to_fmt $F_SECS "%Y-%j&nbsp;%T"`
	  FILE=${f//*cor/cor}
	  SHORT_FILE=${FILE//cor_/}
	  SHORT_FILE=${SHORT_FILE//_vcid5.tlm.gz/}

	  # Extract the dowload date from the filename
	  D_SECS=`name_to_secs $FILE`

	  # Calculate the file to download latency (ie, ground latency)
	  let LG_SECS=$F_SECS-$D_SECS
	  LG_STR=`secs_to_hm $LG_SECS`

	  # Highlight yellow for 6hr, red for 12hr latency
	  LG_HH=""
	  if [ $LG_SECS -gt 21600 ];
	      then
	      LG_HH='bgcolor="yellow"'
	  fi
	  if [ $LG_SECS -gt 43200 ];
	      then
	      LG_HH='bgcolor="red"'
	  fi
	  
	  # Read the file statistics
	  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
	      
	      # Pull out the seconds for the data range
	      set `egrep "HK15_TS_START: " "$V5/../log/decode_summary/d5_$FILE.log"` 4294967295 4294967295
	      TSSEC_START=$2
	      set `egrep "HK15_TS_END: " "$V5/../log/decode_summary/d5_$FILE.log"` 0 0
	      TSSEC_END=$2
	      
	      if [ $TSSEC_START != 4294967295 ];
	      then
		  # Calculate the orbital latency
		  let LO_SECS=$D_SECS-$TSSEC_START
		  LO_STR=`secs_to_hm $LO_SECS`

		  # Calculate the total latency
		  let LT_SECS=$LO_SECS+$LG_SECS
		  LT_STR=`secs_to_hm $LT_SECS`

		  TS15_FROM=`secs_to_fmt $TSSEC_START "%Y-%j %H:%M"`
		  TS15_TO=`secs_to_fmt $TSSEC_END "%Y-%j %H:%M"`
	      else
		  LO_SECS=0
		  LO_STR="N/A"
		  LT_STR="N/A"

		  TS15_FROM="N/A"
		  TS15_TO="N/A"
	      fi

	  else
	      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
	  
	  #let SDD=$SD/10
	  
	  if [ $IMG_REJ -gt 10 ];
	      then
	      IRCOL="BGCOLOR=\"red\""
	  else
	      IRCOL=""
	  fi
	  
	  if [ $F_SIZE -lt 70000000 ];
	      then
	      SZCOL="BGCOLOR=\"yellow\""
	  else
	      SZCOL=""
	  fi
	  
	  REV=`name_to_rev cor_$SHORT_FILE`
	  if [ "X$REV" == "X" ];
	  then
		REV="?"
	  fi

	  # Output the table row
	  echo "<tr><td>$REV</td><td>$SHORT_FILE</td><td>$F_STR</td><td align=\"right\">$LO_STR</td><td $LG_HH align=\"right\">$LG_STR</td><td align=\"right\">$LT_STR</td><td $SZCOL align=\"right\">$F_SIZE</td><td $SZCOL align=\"right\">$UNC_SIZE</td><td align=\"right\">$IMG_CNT</td><td $IRCOL align=\"right\">$IMG_REJ</td><td align=\"right\">$HK_CNT</td><td align=\"right\">$HK_REJ</td><td>$TS15_FROM</td><td>$TS15_TO</td></tr>";
	done
	echo "</table>"
	echo "</div>"
}

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

echo "<h2>Summary By Site</h2>"
echo "<p></p>"

table_from fbks0 Fairbanks

echo "<p></p>"

table_from sval4 Svalbard4
echo "<p class=\"last\"></p>"

# Last received

echo "<h2>Time Since Latest Download File</h2>"

THIS=`ls $V5 | sort | tail -1`
f=$V5/$THIS/`ls -tr $V5/$THIS | tail -1`
FILE=${f//*cor/cor}
SHORT_FILE=${FILE//cor_/}
SHORT_FILE=${SHORT_FILE//_vcid5.tlm.gz/}

set `get_file_info $f` X X
R_SECS=$1
R_SIZE=$2
NOW=`date +%s`
let DIFF=$NOW-$R_SECS
R_STR=`secs_to_hm $DIFF`
C_STR=`secs_to_fmt $R_SECS "%Y-%j %H:%M"`
N_STR=`secs_to_fmt $NOW "%Y-%j %H:%M"`

echo "<p>The following statement was generated at $N_STR UTC:</p>"
echo "<p class=\"last\">$R_STR (hr:mn) have elapsed since $SHORT_FILE ($R_SIZE bytes) was received at Birmingham at $C_STR.</p>"
