#!/bin/ksh -x
#
#

# VARIAVEIS
cd ${Eta_scr}

datasst=`echo ${Run_Date} | cut -c1-8`
hh=`echo ${Run_Date} | cut -c9-10`
FctCont=0000
cont=0
GlobalOK="False"

if [ "${Cond_Glob}" == "TQ0126L028" ] ; then
dir_Global="/scratchout/oper/tempo/oens_MCGA/poseta/dataout/TQ0126L028/"${Run_Date}
 else
dir_Global="/scratchout/oper/tempo/MCGA/poseta/dataout/TQ0213L042/"${Run_Date}
fi
 
typeset -Z4 Fct FctCont

while [ "${GlobalOK}" != "True" ] ; do

   if [ "${Cond_Glob}" == "TQ0126L028" ] ; then
   arqin=`${Eta_home}/util/caldate.3.0 ${Run_Date} + ${FctCont}h ${dir_Global}/'GPOSNMCYYYYMMDDHHyyyymmddhhE.icn.TQ0126L028'`
     elif [ "${Cond_Glob}" == "TQ0213L042" ] ; then
   arqin=`${Eta_home}/util/caldate.3.0 ${Run_Date} + ${FctCont}h ${dir_Global}/'GPOSETAYYYYMMDDHHyyyymmddhhE.icn.TQ0213L042'`
   fi
     
  if [ -s ${arqin} ] ; then
     echo "ARQ1"$arqin >> ${Eta_run}/cond$Run_Date  
     cp ${arqin} ${Eta_run}/${Cond_Glob}_${Run_Date}E.${FctCont}
       
       else
       
     if [ "${Cond_Glob}" == "TQ0126L028" ] ; then
       arqin=`${Eta_home}/util/caldate.3.0 ${Run_Date} + ${FctCont}h ${dir_Global}/'GPOSNMCYYYYMMDDHHyyyymmddhhE.fct.TQ0126L028'`
      elif [ "${Cond_Glob}" == "TQ0213L042" ] ; then
       arqin=`${Eta_home}/util/caldate.3.0 ${Run_Date} + ${FctCont}h ${dir_Global}/'GPOSETAYYYYMMDDHHyyyymmddhhE.fct.TQ0213L042'`
     fi
     
     if [ -s ${arqin} ] ; then
     echo "ARQ2"$arqin >> ${Eta_run}/cond$Run_Date
     cp ${arqin} ${Eta_run}/${Cond_Glob}_${Run_Date}E.${FctCont}
     fi
  fi
  
  if ((${cont}>=100)); then
    echo "cont1"=$cont
    exit 
  fi
  
  if [ -s ${arqin} ] ; then
    cont=0
    
    if [ "${FctCont}" == "${Fct}" ] ; then
      GlobalOK="True"
      exit
    fi  
    let FctCont=${FctCont}+${InitBC}
  else
   sleep 15
   
   let cont=$cont+1  
  fi
done

