#! /bin/ksh

# This is a helper script for the hwrf_output script to run in non-NCO
# mode, without NCO having to read the contents.  Never call this
# except from hwrf_output.sh.

set -e -u -x

nidymdh="$1"
donefile="$DATA/com-archive.done"
rm -f "$donefile"

###### WAIT FOR TPCPOST

echo WAIT FOR TPCPOST TO COMPLETE
echo IT SHOULD MAKE: "${CSTREAM}/${MODEL}_FCST/COMBINE/${STORM}${STORMID}.${YYYYMMDDHH}.stats.short"
echo ...
if ( ! $USHhwrf/hwrf_file_complete.pl -s 1 -o 20 -v -w 5400 -S 20 \
    "${CSTREAM}/${MODEL}_FCST/COMBINE/${STORM}${STORMID}.${YYYYMMDDHH}.{stats.short,no.storm}"  ) ; then
    sh ${utilscript}/setup.sh
    err=911
    err_exit "TIMED OUT WAITING FOR TPCPOST.  ABORTING."
    exit 19
fi
echo ...
echo TPCPOST IS COMPLETE

####################################################################
# Wait a limited amount of time for diagnostic products to complete
function now {
    date +%s
}

start=$( now )

# Decide how long to wait:
if [[ "$WHERE_AM_I" == jet ]] ; then
    # Shorter wait time on Jet to avoid overuse of mss queue
    giveup=$(( start + 3600 ))
elif [[ "$PARAFLAG" == YES ]] ; then
    # Long wait time on CCS, Zeus or Vapor to ensure buggy CRTM jobs can be rerun:
    giveup=$(( start + 5400 ))
else
    # OPERATIONAL CONFIGURATION
    # Wait 15 minutes.
    giveup=$(( start + 900 ))
fi

# NOTE: With satellite products, Jet and Zeus wait for the GRIB1 files
# since cnvgrib does not exist there.  Other platforms wait for the
# GRIB2 files to ensure that the hwrf_satgrib2.sh works.
if [[ "$WHERE_AM_I" == jet || "$WHERE_AM_I" == zeus ]] ; then
    two=''
else
    two=2
fi

# Wait the specified amount of time or until the products are all present.
have_all=no
if [[ "$ATMOS_DOMAINS" == 3 ]] ; then
    min_sat_files=88
    satmatch="hwrfsat_[pnic].grb${two}f"
elif [[ "$ATMOS_DOMAINS" == 2 ]] ; then
    min_sat_files=66
    satmatch="hwrfsat_[pnc].grb${two}f"
else
    min_sat_files=22
    satmatch="hwrfsat_p.grb${two}f"
fi
not_really=no
if [[ $RUN_SATPOST == NO ]] ; then
 min_sat_files=0
fi

while [[ $( now ) -le "$giveup" ]] ; do

    if [[ "$PARAFLAG" == YES ]] ; then
        set +x  # have to disable -x here due to huge size of the ls -1 command
        num_sat_files=$( echo $( ls -1 $COMOUT/$nidymdh.$satmatch* | sort -u | grep hwrfsat_ | wc -l ) )
        set -x
        echo $num_sat_files files match "$COMOUT/$nidymdh.$satmatch*"

        if [[ "$num_sat_files" -ge "$min_sat_files" ]] ; then
          # Have everything.
          have_all=yes
          break
        fi

        # Missing some files, so sleep for a while before checking again:
        sleep 15

    else
      set +x  # have to disable -x here due to huge size of the ls -1 command
      num_sat_files=$( echo $( ls -1 $COMOUT/$nidymdh.$satmatch* | wc -l ) )
      set -x
      echo $num_sat_files files match "$COMOUT/$nidymdh.$satmatch*"

      if [[ "$num_sat_files" -ge "$min_sat_files" ]] ; then
         have_all=yes
         break
      elif [[ "$PARAFLAG" == NO ]] ; then
         echo SOME DIAGNOSTIC PRODUCTS ARE MISSING
         echo CONTINUING ANYWAY, BUT SOME PRODUCTS WILL NOT BE IN THE ARCHIVE.
         have_all=yes
         not_really=yes
         break
      else
         echo SOME DIAGNOSTIC PRODUCTS ARE MISSING
         echo ABORTING WITHOUT CREATING AN ARCHIVE
         have_all=no
      fi

    fi
done

########################################################################
# If diagnostic products did not complete, list what files are missing.

if [[ "$have_all" != yes ]] ; then
    echo SOME DIAGNOSTIC PRODUCTS ARE NOT COMPLETE BUT I HAVE TO MAKE THE ARCHIVE NOW

    abort=NO

    # We're missing some files.  Let's figure out which ones.
    set +x
    sat2=2
    if [[ "$WHERE_AM_I" == jet || "$WHERE_AM_I" == zeus ]] ; then
        sat2=''
    fi
    for ihour in $( seq 0 3 126 ) ; do
        hour00=$( printf %02d $ihour )
        for x in p n c ; do
            if [[ ! -s $COMOUT/$nidymdh.hwrfsat_$x.grb${sat2}f$hour00 ]] ; then
                echo MISSING OR EMPTY: $COMOUT/$nidymdh.hwrfsat_$x.grb${sat2}f$hour00
                if [[ "$PARAFLAG" == YES && "$WHERE_AM_I" != jet && "$WHERE_AM_I" != zeus ]] ; then
                    abort=YES
                fi
            fi
        done
    done
    set -x
    
    if [[ "$abort" == YES ]] ; then
        msg='ABORTING BECAUSE OF MISSING DIAGNOSTIC OUTPUT FILES'
        echo $msg
        echo NO ARCHIVE WILL BE CREATED
        echo NO TRACK FILES WILL BE SAVED
        sh ${utilscript}/setup.sh
        err=911
        err_exit "$msg"
        exit 19
    else
        echo SOME DIAGNOSTIC PRODUCTS ARE MISSING
        echo CONTINUING ANYWAY, BUT SOME PRODUCTS WILL NOT BE IN THE ARCHIVE.
    fi
else
    if [[ $not_really != yes ]] ; then
       echo ALL DIAGNOSTIC PRODUCTS ARE COMPLETE
       echo WILL CREATE ARCHIVE
    fi
fi
