#!/bin/sh

officeID=`echo $HOSTNAME | cut -c 5-7 | awk '{print toupper($0)}'`
officeIDforFile=`echo $HOSTNAME | cut -c 5-7 | awk '{print toupper($0)}'`
if [ $officeID == "HPCN" ]
then
   officeID="HUS"
   officeIDforFile="HUS"
fi


############ GRAB NBM Times ##################

date=`date +%Y%m%d`
hour=`date -d '1 hour ago' +%H`
if [ $hour == 23 ]
then
   date=`date -d '-1 day' +%Y%m%d`
fi

cd /data_store/grib2/${date}/${hour}/NationalBlend/GRID255/

firstFile=`ls -tl *APCP*KWEA* --ignore="*HTSGW*" | tail -1 | awk '{ print $8 }'`
lastFile=`ls -tl *APCP*KWEA* --ignore="*TPWDIR*" --ignore="*TPWSPD*" --ignore="*DRCT-O*" --ignore="*SPED-O*" | head -2 | tail -1 | awk '{ print $8 }'`

year=`date +%Y`
month=`date +%m`
day=`date +%d`

cd /awips2/edex/data/hdf5/gfe/${officeID}/NationalBlend/${year}_${month}_${day}_${hour}00/

lastGFEFile=`ls -tl | head -2 | tail -1 | awk '{ print $8 }'`

stringToFile="${date},${firstFile},${lastFile},${lastGFEFile}"

echo $stringToFile >> /data/local/NBMTimes/NBMupdateTime_${hour}_${officeIDforFile}.csv

############# GRAB HRRR TIMES ##########################

date=`date +%Y%m%d`
hour=`date -d '1 hour ago' +%H`
if [ $hour == 23 ]
then
   date=`date -d '-1 day' +%Y%m%d`
fi

cd /data_store/grib2/${date}/${hour}/HRRR/GRID255/

firstFile=`ls -tl | tail -1 | awk '{ print $8 }'`
lastFile=`ls -tl | head -2 | tail -1 | awk '{ print $8 }'`

year=`date +%Y`
month=`date +%m`
day=`date +%d`

cd /awips2/edex/data/hdf5/gfe/${officeID}/HRRR/${year}_${month}_${day}_${hour}00/

lastGFEFile=`ls -tl | head -2 | tail -1 | awk '{ print $8 }'`

stringToFile="${date},${firstFile},${lastFile},${lastGFEFile}"

echo $stringToFile >> /data/local/NBMTimes/HRRRupdateTime_${hour}_${officeIDforFile}.csv


############# GRAB GFS TIMES ############################

# Only check for 00Z, 06Z, 12Z and 18Z cycles
# kick off at 05Z, 11Z, 17Z and 23Z

curHour=`date +%H`
if [ $curHour == "05" ] || [ $curHour == 11 ] || [ $curHour == 17 ] || [ $curHour == 23 ]
then

  date=`date +%Y%m%d`
  hour=`date -d '5 hour ago' +%H`

  cd /data_store/grib2/${date}/${hour}/GFS/GRID215/

  firstFile=`ls -tl | tail -1 | awk '{ print $8 }'`
  lastFile=`ls -tl | head -2 | tail -1 | awk '{ print $8 }'`

  year=`date +%Y`
  month=`date +%m`
  day=`date +%d`

  cd /awips2/edex/data/hdf5/gfe/${officeID}/GFS/${year}_${month}_${day}_${hour}00/

  lastGFEFile=`ls -tl | head -2 | tail -1 | awk '{ print $8 }'`

  stringToFile="${date},${firstFile},${lastFile},${lastGFEFile}"

  echo $stringToFile >> /data/local/NBMTimes/GFSupdateTime_${hour}_${officeIDforFile}.csv
fi

############# GRAB WPC QPF TIMES ############################

# Only check for 06Z and 18Z cycles

curHour=`date +%H`
if [ $curHour == "06" ] || [ $curHour == "08" ] || [ $curHour == 18 ] || [ $curHour == 20 ]
then

  date=`date +%Y%m%d`
  hour=$curHour
  #hour=`date -d '9 hour ago' +%H`

  cd /data_store/grib2/${date}/${hour}/NCEP_QPF/GRID255/

  firstFile=`ls -tl | tail -1 | awk '{ print $8 }'`
  lastFile=`ls -tl | head -2 | tail -1 | awk '{ print $8 }'`

  year=`date +%Y`
  month=`date +%m`
  day=`date +%d`

  cd /awips2/edex/data/hdf5/gfe/${officeID}/HPCERP/${year}_${month}_${day}_${hour}00/

  lastGFEFile=`ls -tl | head -2 | tail -1 | awk '{ print $8 }'`

  stringToFile="${date},${firstFile},${lastFile},${lastGFEFile}"

  echo $stringToFile >> /data/local/NBMTimes/WPCupdateTime_${hour}_${officeIDforFile}.csv
fi

