#!/bin/sh

# copy hotstart file, and hotstart time file (hotstart.dat, time_hotstart.txt)
# from work to input for next run using nos_glofs_hotstartcopy.sh

set -x

cd $DATA/$fcstsys

case "$RESTARTASK"
in
  "restart_copy")

if [ $fcstsys == 'leofs' ]
then
testsize=1936240 
elif [ $fcstsys == 'lmofs' ]
then
testsize=9471328
elif [ $fcstsys == 'lhofs' ]
then
testsize=10643416
elif [ $fcstsys == 'loofs' ]
then
testsize=2671816
elif [ $fcstsys == 'lsofs' ]
then
testsize=3206176 
fi

# determine the hotstartout file size in bytes
filesize=`wc -c $1 | awk '{print $1}'`

echo $1 $2 $3 $4 
echo $filesize
echo $testsize

if [ $filesize -eq $testsize ];
then
 echo 'RESTART FILE IS CORRECT SIZE AND WILL BE COPIED FOR NEXT MODEL RUN INTO' $GESDIR
 cp $1 $GESDIR/$2

  if test $# == 4 
   then
   cp $3 $GESDIR/$4
  fi
 echo "RESTART COPY 100" >> $FCSTSYSLOG 
else
 echo "RESTART COPY 0" >> $FCSTSYSLOG
fi

;;

    "restart_grab")

#loop through the hotstart directories (starting from today
#till seven days ago for the latest good hotstart file for each lake

for PDYS in $PDY $PDYm1 $PDYm2 $PDYm3 $PDYm4 $PDYm5 $PDYm6 $PDYm7
do
    export GESDIR=$GES_IN/${NET}.${PDYS}
    echo "LOOKING IN $GESDIR FOR RESTART FILE"

    if [ -f $GESDIR/${NET}_${fcstsys}_hotstart.dat ]
    then
     echo FOUND FILE IN $GESDIR
    cp $GESDIR/${NET}_${fcstsys}_hotstart.dat ${NET}_${fcstsys}_hotstart.dat
    cat $GESDIR/${NET}_${fcstsys}_timehotstart.txt >  ${NET}_${fcstsys}_timehotstart.txt
    exit
    fi

done

;;

esac



