#!/bin/ksh -x
#PBS -lselect=1:ncpus=1
#PBS -N SPosdate_in                                                                       
#PBS -o cam/SubmitPos
#PBS -A CPTEC
#PBS -j oe 
#PBS -q pesq.q

# VARIAVEIS

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

while [ "${LastArq}" != "True" ] ; do
 NJobs=`qstat | grep EP${anomesdia} | grep ${USER} | wc -l`
 if ((${NJobs}<5)) ; then
  if [ -s ${Eta_run}/fcstdone${FctPos}.t00s ] ; then
#   tam=`du -s ${Eta_run}/restrt${FctPos}.t00s | awk '{print $1}'`
#   if ((${tam} >= 204990)) ; then
    hora=`echo ${FctPos} |cut -c 1-6`
    cat outjob_special.ksh | sed -e "s:HFCT:${FctPos}:g" | sed -e "s:dia:${anomesdia}:g" | sed -e "s:hor:${hora}:g" > ${mmddhh}oj${FctPos}.ksh
    echo "submetido ${FctPos}"
#   qsub outjob_special${FctPos}.ksh
    export PBS_SERVER=aux20-eth4
    qsub ${mmddhh}oj${FctPos}.ksh
    
    if [ "${FctPos}" == "${Fct}" ] ; then
      LastArq="True" 
    fi  
    let FctPos=${FctPos}+${IntPhisAcum}
#   fi
  else
   sleep 40  
  fi
 else
   sleep 30
 fi 
done

exit
