#!/bin/ksh 

# VARIAVEIS

typeset -Z6 Fct
typeset -Z6 FctPos
FctPos=${Fcts}
cd ${Eta_run}
LastArq="False"
mmddhh=`echo ${Run_Date} |cut -c 5-10`

while [ "${LastArq}" != "True" ] ; do
# NJobs=`qstat |grep Eta${mmddhh} | grep ${USER} | wc -l`
# if ((${NJobs}<7)) ; then
  if [ -s ${Eta_run}/fcstdone${FctPos}.t00s ] || [ -s ${Eta_run}/FCSTDONE${FctPos}.T00S ] ; then
    hora=`echo ${FctPos} |cut -c 1-6`
    ln -s  ${Eta_run}/RESTRT${FctPos}.T00S    ${Eta_run}/restrt${FctPos}.t00s
    ln -s  ${Eta_run}/FCSTDONE${FctPos}.T00S    ${Eta_run}/fcstdone${FctPos}.t00s
    cat ${Eta_run}/outjob_special.ksh | sed -e "s:HFCT:${FctPos}:g" | sed -e "s:hor:${hora}:g" | sed -e "s:datap:${mmddhh}:g" > ${Eta_run}/${mmddhh}oj${FctPos}.ksh
    echo "submetido ${FctPos}"
      chmod 755 ${Eta_run}/${mmddhh}oj${FctPos}.ksh
      ${Eta_run}/${mmddhh}oj${FctPos}.ksh
    cat ${Eta_run}/Indice.ksh | sed -e "s:HFCT:${FctPos}:g" > ${Eta_run}/Indice_${FctPos}.ksh    
      chmod 755 ${Eta_run}/Indice_${FctPos}.ksh 
      ${Eta_run}/Indice_${FctPos}.ksh     
    
    if [ "${FctPos}" == "${Fct}" ] ; then
      LastArq="True" 
    fi  
    let FctPos=${FctPos}+${IntFct}
#   fi
#  else
#   sleep 20  
#  fi
 else
	  echo -e "AGUARDANDO O restrt${FctPos}.t00s PARA O POS-PROCESSAMENTO!"
   sleep 15
 fi 
done

exit
