echo "------------------------------------------------"
echo "This script provides the users an inventory of RAOB"  
echo "report parts on the WEB in near real time. The code"
echo "THKS means a complete report with data received "
echo "------------------------------------------------"
echo "Author: Jeffrey Stoudt"
echo "NCO/Data Management & Quality Assessment Branch June 2004"
echo "History: June 2004 - First implementation of this new script."

########################### CHANGE LOG #############################
#
# 11 Feb 2011 - Jeff Stoudt added auto-cleanup code to remove the
#               daily subdirectory which has just aged to 63 days.
# 19 Jul 2011 - Chris Magee - Removing hardcoded paths and lines of
#                             code that were commented out (i.e. no
#                             longer used). Removed DATE_65 and    
#                             DATE_66 (not used).                
#
####################################################################

#####################################################################
set -xa
cd $DATA
########################################
set -x
msg="exthanks_realtime.sh.sms HAS BEGUN!"
postmsg "$jlogfile" "$msg"
########################################

echo "Script to prepare inventory of of 00Z/12Z upper air reports"

#  Time variables

export runtime=`date -u +"%a %b %d %H:%M:%S UTC %Y"`
export rundate=`date -u +%Y%m%d%H%M%S`
export locpdy=`echo $rundate | cut -c1-8`
export rtime=`echo $rundate | cut -c9-14`
export hhmm=`echo $rundate | cut -c9-12`

###
### Determine date which is 63 days ago and build date string.
###
export DATE_63=`$utilexec/ndate -1512 $PDY$cyc | cut -c1-8`
export DATE_64=`$utilexec/ndate -1536 $PDY$cyc | cut -c1-8`

rm -r $COMOUT/$DATE_64

export YEAR=`echo $DATE_63 | cut -c1-4`
export MON=`echo $DATE_63 | cut -c5-6`
export DAY=`echo $DATE_63 | cut -c7-8`


###
###  Remove any subdirectories and contents which are 63 days old,
### then remove corresponding anchor code in previous html (php) scripts.
###
###  The following "if [ ]; then...fi" construct was added to install auto cleanup
### of the daily subdirectory which has just aged to 63 days old. It will attempt
### to rm only if subdirectory so named with date string of date which is 63 days
### ago exists. Any 64 or more days old would have to be removed manually. If for
### some reason a problem with determining the date of 63 days ago occurs, the
### operation will cause at most one day's worth of damage.
###

### Code added by Jeff Stoudt 11 Feb 2011 ###
 if [ -d $COMOUT/$DATE_63 ]; then  # Check if any such subdirectory exists.
  rm -rf $COMOUT/$DATE_63   # Remove the subdirectory and its contents.
 fi
###

 grep -v "yr=$YEAR\&mo=$MON\&day=$DAY" $COMIN/previous_body.thankusa.php > temp11.php
 grep -v $DATE_63 temp11.php > temp1.php
 cp temp1.php $COMOUT/previous_body.thankusa.php
 grep -v "yr=$YEAR\&mo=$MON\&day=$DAY" $COMIN/previous_body.thankmil.php > temp22.php
 grep -v $DATE_63 temp22.php > temp2.php
 cp temp2.php $COMOUT/previous_body.thankmil.php
 grep -v "yr=$YEAR\&mo=$MON\&day=$DAY" $COMIN/previous_body.thankint.php > temp33.php
 grep -v $DATE_63 temp33.php > temp3.php
 cp temp3.php $COMOUT/previous_body.thankint.php
 grep -v "yr=$YEAR\&mo=$MON\&day=$DAY" $COMIN/previous_body.thankcan.php > temp44.php
 grep -v $DATE_63 temp44.php > temp4.php
 cp temp4.php $COMOUT/previous_body.thankcan.php

filename1=thanks1.$locpdy.$rtime
filename2=thanks2.$locpdy.$rtime
filename3=thanks3.$locpdy.$rtime
filename4=thanks4.$locpdy.$rtime

# prepare control card for standard input. 

echo ${locpdy} ${cyc} ${runtime} > cardec

#### 
####   DUMP THE RAOB DATA
#### 

BACK=off
DUPC=off
loud=on
$ushscript/dumpjb ${locpdy}${cyc} 1.0 raobf
errdmp1=$?
mv raobf.ibm  raobf.${locpdy}${cyc}

####
####  Run the thanks_realtime for doing the thank_usa report 
####  for US only
####
export pgm=thanks_realtime
. prep_step
startmsg

export XLFUNIT_11=raobf.$locpdy$cyc               
export XLFUNIT_21=$FIXPREP/thanks_realtime_usanws
export XLFUNIT_22=$FIXPREP/thanks_realtime_usaother
export XLFUNIT_23=$FIXPREP/thanks_realtime_latinamer
export XLFUNIT_24=$FIXPREP/thanks_realtime_canada
export XLFUNIT_51=$filename1
export XLFUNIT_52=$filename2
export XLFUNIT_53=$filename3
export XLFUNIT_54=$filename4
$EXECPREP/thanks_realtimex  < cardec >> $pgmout  2> errfile
export err=$? ;err_chk
export err1=$err

if [ $err1 = 0 ]; then
  if test "$SENDCOM" = 'YES'
  then
    cat $filename1 | cat >> $COMOUT/thank_usa.$locpdy$hhmm
    cat $filename2 | cat >> $COMOUT/thank_mil.$locpdy$hhmm
    cat $filename3 | cat >> $COMOUT/thank_int.$locpdy$hhmm
    cat $filename4 | cat >> $COMOUT/thank_can.$locpdy$hhmm
  fi
fi

if [ $err1 != 0 ]; then
  if test "$SENDCOM" = 'YES'
  then
    echo " REPORT IS MISSING " | cat >> file1
    cp file1 $COMOUT/thank_usa.$locpdy$hhmm
  fi
fi

# if [ $err2 = 0 ]; then
# if test "$SENDCOM" = 'YES' 
# then
#   grep -v CONTACT $filename2 > filename3
#   cat  filename3 | cat >> $COMOUT/thank_int.$locpdy$hhmm
# fi
# fi

# if [ $err2 != 0 ]; then
# if test "$SENDCOM" = 'YES'
# then
#   echo " REPORT IS MISSING " | cat >>  file2
# fi
# fi
#
# Build the web page structure for the two new reports on the ccs.
#
export thankslog=$DATA/log.$PDY
$utilscript/ncothanksphp.pl thankusa $COMOUT/thank_usa.$locpdy$hhmm
$utilscript/ncothanksphp.pl thankmil $COMOUT/thank_mil.$locpdy$hhmm
$utilscript/ncothanksphp.pl thankint $COMOUT/thank_int.$locpdy$hhmm
$utilscript/ncothanksphp.pl thankcan $COMOUT/thank_can.$locpdy$hhmm

#cat out log contents from ncothanksphp.pl to stdout
cat $thankslog

#
# Replicate the modified web page structure from ccs to WOC.
# Rsync method modified 12/03/2007 by J. Stoudt to replace the rsync script with a single-line command because the cleanup logs step in the script is unnecessary, and it has been hanging. Therefore, this step was eliminated.
#

/usrx/local/bin/rsync -v --recursive --links --perms --rsh=/usr/bin/ssh --stats --delete --whole-file --update $COMOUT/  nwprod@ncorzdm.ncep.noaa.gov:/home/www/nco/htdocs/pmb/nw${envir}/thanks/

########################################################
set +x
echo " ***** $job PROCESSING COMPLETED NORMALLY *****"
echo " ***** $job PROCESSING COMPLETED NORMALLY *****"
echo " ***** $job PROCESSING COMPLETED NORMALLY *****"
set -x
########################################################
