#!/bin/bash


function waitingFor
{
   waitedFile=$1
   
   # verif que la variable MonitoringDir est bien define
   if [ "x${MonitoringDir}" == "x" ] 
   then
      echo "variable MonitoringDir not defined. stop."
      exit -1
   fi
   
   # Le fichier teste doit etre dans le rep de suivi
   isWaitedFileName_In_MonitoringDir=`echo $waitedFile | grep ${MonitoringDir} | wc -l `
   if [ "x${isWaitedFileName_In_MonitoringDir}" == "x0" ]
   then
      echo "The waited file name :"
      echo "$waitedFile"
      echo "is incorrect. The location of the file must be in the monitoring dir."
      exit -1
   fi
   
   waitedFileBasename=$(basename ${waitedFile})
   # echo "On verifie la presence de ${waitedFileBasename}..."
   iter=0
   found=0
   while [ ${iter} -lt 60 ]
   do
      if [ -e $waitedFile ]
      then
         found=1
         break
      fi      
      iter=$(( $iter + 1 ))
      echo "waiting for ${waitedFileBasename}...${iter}..."
      sleep 1
   done
   
   if [ "x${found}" == "x0"  ]
   then
      echo "Le fichier ${waitedFileBasename} n'a pas ete trouve. stop."
      exit -1
   fi
   
   # echo "fichier trouve !"

}


#######################
# Main program ########
#######################
. load_ipslPack_env.sh
# fichier de progression d'execution
# Le seul arg du script doit etre un fichier param
# paramFile=${1}
# paramFileBasename=$(basename ${1})

# if [ ! -e $paramFile ] || [ "x${paramFile}" == "x" ]
# then
#     echo "Le fichier en argument n existe pas"
#     exit 0
# fi

if [ ! -e ${1} ] || [ "x${1}" == "x" ]
then
    echo "Le fichier en argument n existe pas"
    exit 0
else
    paramFile=${1}
    paramFileBasename=$(basename ${1})
fi


extSuiviListes=`echo ${paramFileBasename} | sed 's;\.[[:alnum:]]*$;;' `
export MonitoringDir="${TMP_MIGR_DATA}/SuiviListes_${extSuiviListes}"
# echo "MonitoringDir=$MonitoringDir"
export generalMonitorFile=${MonitoringDir}/general.txt
export nbSimuFile=${MonitoringDir}/nbSimu.txt

declare -a tabGeneralSteps=()
tabGeneralSteps[0]="find_directory_simul.sh-->OK"
tabGeneralSteps[1]="create_config_card.sh-->OK"

declare -a tabSimuSteps=()
tabSimuSteps[0]="calcul_size_simul.sh-->OK"
tabSimuSteps[1]="find_size_pack.sh-->OK"
tabSimuSteps[2]="write_liste_pack.sh-->OK"
tabSimuSteps[3]="archive_restart.sh-->OK"
tabSimuSteps[4]="archive_debug.sh-->OK"

echo "#################################################"
echo "###  ETAT D'AVANCEMENT DES SCRIPTS DE lISTES  ###"
echo "#################################################"
echo

# Si le rep de suivi n'existe pas, stop.
if [ ! -d ${MonitoringDir} ]
then
   echo "${MonitoringDir} n'existe pas. stop."
   exit -1 
fi

cd ${MonitoringDir}
if [ "x$?" != "x0" ]
then
  echo "Pb avec cd ${MonitoringDir}"
  exit -1
fi

# Etapes generales
# ----------------
echo "--------------------------------------------------"
echo "---------------- Etapes generales ----------------"
echo "--------------------------------------------------"
waitingFor "${generalMonitorFile}"


generalStepsDoneList=`cat ${generalMonitorFile} `
for ((i = 0; i < ${#tabGeneralSteps[*]}; i += 1))
do
  isEltInDoneList=`echo $generalStepsDoneList | grep "${tabGeneralSteps[$i]}" | wc -l `
  if [ "x$isEltInDoneList" == "x0" ]
  then
     stringKO=`echo ${tabGeneralSteps[$i]} | sed 's;OK;?? #####;' `
     echo "$stringKO"
  else
     echo "${tabGeneralSteps[$i]}"
  fi
  
done


# Etapes createListing
# --------------------
echo
echo "--------------------------------------------------" 
echo "---------------- Etape createListing -------------"
echo "--------------------------------------------------" 
listSubDir=`ls -l | grep -e '^d' | awk '{print $NF}' `
# echo "listSubDir=$listSubDir"
if [ "x$listSubDir" == "x" ]
then
   echo "Aucune liste n'est prete."
fi

# echo "listSubDir=$listSubDir"
for rep in $listSubDir
do
   cd $rep
   if [ ! -e createListingOK.txt ]
   then
      echo "${rep}: createListing.sh-->?? #####"
   else
      echo "${rep}: createListing.sh-->OK"
   fi
   
   cd ..
done
echo

echo "--------------------------------------------------" 
echo "---------------  Etape par simu ------------------"
echo "--------------------------------------------------"
if [ "x$listSubDir" == "x" ]
then
   echo "Aucune simu n'a ete traitee."
fi

nbSimuOK=0
for rep in $listSubDir
do
   cd $rep
   listSimu=`ls -1 | grep -v "createListingOK.txt" `
   if [ ! -e createListingOK.txt ] || [ "x${listSimu}" == "x" ]
   then
      echo "${rep}: Pas de simu traitee"
   fi
   # echo "listSimu=$listSimu"
   for simu in $listSimu
   do
       simuDone=1
       simuName=`echo $simu | sed 's;_#_;/;g' | sed 's;\.txt;;' `
       
       # Cas des simu speciales ( < 1 Go ou de type etat0 ) #############
       isSpecialSimuOK=`cat ${simu} | grep "special simu==>OK" | wc -l `
       if [ "x${isSpecialSimuOK}" != "x0" ]
       then
           echo "$simuName : special simulation ==> OK"
	   nbSimuOK=$(( $nbSimuOK + 1 ))
	   continue
       fi
       
       
       # Cas des simus normales
       echo $simuName
       simuStepsDoneList=`cat ${simu} `
       for ((i = 0; i < ${#tabSimuSteps[*]}; i += 1))
       do
           isEltInDoneList=`echo $simuStepsDoneList | grep "${tabSimuSteps[$i]}" | wc -l `
           if [ "x$isEltInDoneList" == "x0" ]
           then
              stringKO=`echo ${tabSimuSteps[$i]} | sed 's;OK;?? #####;' `
	      simuDone=$(( $simuDone && 0 ))
              echo "    $stringKO"
           else
              echo "    ${tabSimuSteps[$i]}"
           fi         
       done
       
       # si toutes les operations sur la simu sont OK, on incremente le nb de simu OK
       if [ "x$simuDone" == "x1" ]
       then
          nbSimuOK=$(( $nbSimuOK + 1 ))
       fi                 
   done   
   cd ..
done


waitingFor "${nbSimuFile}"

nbSimuTot=`cat ${nbSimuFile} `
nbSimuFormatOK=`echo $nbSimuTot | grep -e '^[0-9]\{1,4\}$' | wc -l`
if [ "x$nbSimuFormatOK" == "x0" ]
then
   echo "Le nombre de simulation contenues dans le fichier $nbSimuFile est incorrect."
   exit -1
fi

echo
echo "============================================="
echo "nbre de simu OK : $nbSimuOK / $nbSimuTot"
echo "============================================="










