#####################################################################
echo "-----------------------------------------------------"
echo " exgefs_restart_copy.sh.sms" 
echo " Sep 11 - Wobus - restart copy job"
echo "-----------------------------------------------------"
#####################################################################

set -x
na=`basename $0`
export PS4=' + $SECONDS $RUN $na $LINENO: '

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

export MP_LABELIO=YES

############################################################
#  Define Variables:
#  -----------------
#  SLEEP_TIME   is the number of seconds to sleep before exiting with error.
#  SLEEP_INT    is the number of seconds to sleep between restrt file checks.
#  fhrini       is the initial forecast hour
#  fhrint       is the restart file write interval
#  fhrlim       is the last forecast hour
#  fhour        is the forecast hour read from the restart file header
#  fhr          is the current forecast hour
############################################################
SLEEP_TIME=1800
SLEEP_INT=3
SLEEP_LOOP_MAX=`expr $SLEEP_TIME / $SLEEP_INT`
###############################################################
#  NOTE:  SLEEP_INT is used after each member, so
#  SLEEP_INT * (number of members) 
#  is the total wait time during the processing of all the 
#  members which must be less than the wall clock interval 
#  between restart file writes
###############################################################

export pgm="restartcopy"

echo fhrini=$fhrini
echo fhrint=$fhrint
echo fhrlim=$fhrlim

ic=0
(( fhri = fhrini ))
if test $fhri -lt 10
then
  fhri="0$fhri"
fi
echo fhri=$fhri
(( fhr = fhri - fhrint ))

dira=$COMOUT/$cyc/restarta
echo dira=$dira
dirb=$COMOUT/$cyc/restartb
echo dirb=$dirb

echo fhmax3=$fhmax3

nmemt=0
for mem in $memberlist
do
  (( nmemt = nmemt + 1 ))
done
echo nmemt=$nmemt

doneall=no

while [ $ic -le $SLEEP_LOOP_MAX ] && [[ $doneall = no ]]
do
  cpmvlist=
  ncpmvf=0
  fhrt=-9
  fhrmatch=yes
  for file in sigs1 sigr1 sigs2 sigr2 sfcs sfcr
  do
    hdrprogram=global_sighdr
    case $file in
      (sfcs)
	 hdrprogram=global_sfchdr
      ;;
      (sfcr)
	 hdrprogram=global_sfchdr
      ;;
    esac
    size=0
    nsize=1
    ncpmv=0
    for mem in $memberlist
    do
      if [[ $fhrmatch = yes ]]; then
	dirfilea=$dira/ge$mem.$cycle.$file
	echo dirfilea=$dirfilea
	if [[ -f $dirfilea ]]; then
	  fhour=`$EXECGLOBAL/$hdrprogram $dirfilea fhour`
	  rc=$?
	  echo rc=$rc
	  if (( rc == 0 )); then
	    echo fhour=$fhour
	    fhr=`echo $fhour | sed -e "s/\.00//"`
	    if test $fhr -lt 10
	    then
	      export fhr="0$fhr"
	    fi
	    echo fhr=$fhr
	    if (( fhr > fhri )); then
	      if (( fhrt >= 0 )) then
		if (( fhr == fhrt )); then
		  echo fhrt=$fhrt matches fhr=$fhr
		else
		  echo fhrt=$fhrt does not match fhr=$fhr
		  fhrmatch=no
		fi
	      else
		fhrt=$fhr
		echo initial setting fhrt=$fhrt
	      fi
	      if [[ $fhrmatch = yes ]]; then
		size_test=`eval ls -l $dirfilea |awk -F" " '{print $5}'`
		echo size_test=$size_test
		if (( size < size_test )); then
		  nsize=1
		  echo size=$size and match size_test=$size_test do not match nsize=$nsize
		  size=size_test
		elif (( size == size_test )); then
		  (( nsize = nsize + 1 ))
		  echo size=$size match nsize=$nsize
		fi
		fileb=ge$mem.$cycle.${file}f$fhr
		dirfileb=$dirb/$fileb
		echo dirfileb=$dirfileb
		if [[ -f $dirfileb ]]; then
		  if [[ $dirfileb -ot $dirfilea ]]; then
		    echo $dirfileb is older than $dirfilea
		    cpmv=yes
		    (( ncpmv = ncpmv + 1 ))
		    (( ic = 0 ))
		  else
		    echo dirfileb=$dirfileb already exists
		    cpmv=no
		  fi
		else
		  echo $dirfileb does not yet exist
		  (( ncpmv = ncpmv + 1 ))
		  cpmv=yes
		  (( ic = 0 ))
		fi
	      fi
	    else
	      echo fhr=$fhr is not after fhri=$fhri
	      fhrmatch=no
	    fi
	  fi
	fi
      else
	echo skip processing file=$file mem=$mem because fhrmatch=no
      fi
    done
    echo file=$file ncpmv=$ncpmv
    if (( ncpmv == nmemt )); then
      cpmvlist="$cpmvlist $file"
      (( ncpmvf = ncpmvf + 1 ))
      echo ncpmvf=$ncpmvf cpmvlist=$cpmvlist
    fi
  done
  if (( ncpmvf == 3 )); then
    echo ncpmvf=$ncpmvf fhr=$fhr ready to copy or move
    if (( fhr == fhmax3 )); then
      for file in $cpmvlist
      do
	for mem in $memberlist
	do
	  filea=ge$mem.$cycle.$file
	  dirfilea=$dira/$filea
	  echo copy dirfilea=$dirfilea
	  cp $dirfilea $filea
	done
      done
    else
      for file in $cpmvlist
      do
	for mem in $memberlist
	do
	  filea=ge$mem.$cycle.$file
	  dirfilea=$dira/$filea
	  echo move dirfilea=$dirfilea
	  mv $dirfilea $filea
	done
      done
    fi
    for file in $cpmvlist
    do
      echo file=$file fhr=$fhr process and move begin
      hdrprogram=global_sighdr
      case $file in
	(sfcs)
	   hdrprogram=global_sfchdr
	;;
	(sfcr)
	   hdrprogram=global_sfchdr
	;;
      esac
      for mem in $memberlist
      do
	filea=ge$mem.$cycle.$file
	echo test filea=$filea
	if [[ -f $filea ]]; then
	  fhour=`$EXECGLOBAL/$hdrprogram $filea fhour`
	  rc=$?
	  if (( rc == 0 )); then
	    echo fhour=$fhour
	    fhr=`echo $fhour | sed -e "s/\.00//"`
	    if test $fhr -lt 10
	    then
	      export fhr="0$fhr"
	    fi
	    echo fhr=$fhr
	    fileb=ge$mem.$cycle.${file}f$fhr
	    dirfileb=$dirb/$fileb
	    echo dirfileb=$dirfileb
	    if [[ -f $dirfileb ]]; then
	      echo dirfileb=$dirfileb already exists
	    else
	      echo move filea=$filea dirfileb=$dirfileb
	      mv $filea $dirfileb
	    fi
	  else
	    echo processing $filea FAILED rc=$rc
	  fi
	else
	  echo filea=$filea DOES NOT EXIST
	fi
      done
      echo file=$file fhr=$fhr process and move end
    done
    (( ic = 0 ))
  else
    echo not ready to move or copy yet, ncpmvf=$ncpmvf
    if (( ic > 0 )); then
      sleep $SLEEP_INT
      echo `date` ic=$ic sleep $SLEEP_INT
    else
      echo `date` ic=$ic sleep 1
      sleep 1
    fi
  fi
  if (( ic > 1 )) && (( fhr + fhrint > fhrlim )) ; then
    echo terminate for ic=$ic fhrint=$fhrint fhrlim=$fhrlim fhr=$fhr
    doneall=yes
  else
    echo `date` do not terminate for ic=$ic fhrint=$fhrint fhrlim=$fhrlim fhr=$fhr
  fi
  (( ic = ic + 1 ))
done
echo `date` terminated when ic=$ic fhr=$fhr
echo
echo `date` list restarta
echo
ls -al $dira
echo
echo `date` list restartb
echo
ls -al $dirb
echo
echo `date` list end

########################################################
msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################
