#!/bin/bash

export JOB_DIR=${LS_SUBCWD:-${PWD}}
export EXE_DIR=${JOB_DIR}

export INPUT_DMF_DATA=${SCRATCHDIR}/PSEUDO_DMNFS_light
export OUTPUT_STORE=${SCRATCHDIR}/PSEUDO_DMNFS_STORE
export OUTPUT_WORK=${SCRATCHDIR}/PSEUDO_DMNFS_WORK

SCRIPT_NAME=$(basename ${0} )

export IGCM_DEM=${SCRATCHDIR}/IGCM_DEM
export IGCM_TMP=${SCRATCHDIR}/IGCM_DEM/tmp

export config_card=${IGCM_DEM}/config_card.liste

source ${EXE_DIR}/DEM_utilities.sh


function getSimuName
{   
   config_card=$1
   
   baseName=$( basename $config_card )
   simuName=`echo ${baseName} | sed 's;config_card_;;' `
   echo $simuName

}

function getTimeTaken
{
   statFile=$1
   if [ ! -e $statFile ]
   then
      echo "inconnu : pas de fichier status"
      return
   fi
   
   timeLine=`sed -n '1p' $statFile `
   goodFormat=`echo $timeLine | grep -e '^meantime:[[:digit:]]*\.[[:digit:]]\{1,3\}$' | wc -l `
   if [ "x$goodFormat" == "x0" ]
   then
       echo "inconnu : mauvais format du temps"
       return
   fi
   
   timeTaken=`echo $timeLine | sed 's;^meantime:;;' `
   echo $timeTaken
   
}

function progressStatus
{
   statFile=$1
   echo "statFile=$1" >> showPackProgress.log
   if [ ! -e $statFile ]
   then
      echo "Non traitee"
      return
   fi
   echo "blabla" >> showPackProgress.log
   echo "----------------------------------------------" >> showPackProgress.log
   # echo "progressStatus..." # gpdebug : a virer
   
   # ------------------------------------------------------------------------------------------------
   listLineNumWithKey=`grep -n -E '(COMPLETED|FAILED|DELEGATE)' $statFile | awk -F":" '{print $1}' ` 
   # echo "listLineNumWithKey=$listLineNumWithKey" # gpdebug : a virer
   
  if [ "x$listLineNumWithKey" == "x" ]
  then
     echo "Statut illisible"
     return
  fi

   # On trouve la derniere ligne du fichier status comportant un mot cle 
   max=0
   for lineNum in $listLineNumWithKey
   do
      if [ $lineNum -gt $max ]
      then
          max=$lineNum
      fi
   done

   # echo "max=$max" # gpdebug : a virer

   lastLineWithKey=`sed -n "${max}p" $statFile `
   # echo "lastLineWithKey=$lastLineWithKey" # gpdebug : a virer   
   # ------------------------------------------------------------------------------------------------
     
   # lastLine=`tail -1 $statFile ` # gpdebug : a virer
   
   completed=`echo $lastLineWithKey | grep COMPLETED | sed 's; (.*)$;;' `
   if [ "x${completed}" != "x" ]
   then 
      echo $completed
      return
   fi
   
  failed=`echo $lastLineWithKey | grep FAILED | cut -d";" -f1 `
  if [ "x${failed}" != "x" ]
  then
     echo $failed
     return         
  fi
  
  delegate=`echo $lastLineWithKey | grep DELEGATE `
  if [ "x${delegate}" != "x" ]
  then
     echo "DELEGATE"
     return
  fi
  
}


#################################
####### Main script #############
#################################
> showPackProgress.log # vider le fichier de log

totalNbOfList=0
for CONFIG in $( awk '{print $1}' ${IGCM_DEM}/config_card.liste )
do
   PATH_SIMU=$( dirname $CONFIG )
   nbListInConfig=`find $PATH_SIMU -name "*list" | wc -l `
   totalNbOfList=$(( $totalNbOfList + $nbListInConfig ))
done

# echo "totalNbOfList=$totalNbOfList"
# exit 0 # gpdebug : a virer

nbOfListsPacked=0
nbOfListsFailed=0

for CONFIG in $( awk '{print $1}' ${IGCM_DEM}/config_card.liste ) ; do 

    DEM_state=$( DEM_read_state ${IGCM_DEM}/config_card.liste ${CONFIG} )

    if [[ "${DEM_state}" != "WriteListTarDone" ]] ; then

	 simuName=$( getSimuName $CONFIG )
	 echo "*******************************************"
	 echo "simulation : $simuName"
	 echo "*******************************************"
	 export PATH_SIMU=$( dirname $CONFIG )
	 # echo "PATH_SIMU = $PATH_SIMU"
	
	 listOfDir="output_ncrcat output_tar restart_tar debug_tar store_cp work_cp"
	 for dir in $listOfDir
	 do
	     echo "   ${dir} :"
	     if [ ! -d $PATH_SIMU/${dir} ]
	     then
	        echo "      ${dir} n'existe pas pour cette simu"
	        continue
	     fi
	   
	     listFilesInDir=`find $PATH_SIMU/${dir} -name "*list" | wc -l `
	     if [ "x$listFilesInDir" == "x0" ]
	     then
	        echo "       -- no list --"
	        continue	   
	     fi
	   
	     setList=$( ls $PATH_SIMU/${dir}/*list )

	     for set in $setList
	     do
	          listName=`echo $(basename $set) | sed 's;\.list$;;' | sed 's;\.nc$;;' `
	    
	          statusfile=${set%%.list}.status
	          status=$( progressStatus $statusfile ) # gpdebug : a retablir
		  timeTaken="???"
		  # progressStatus $statusfile # gpdebug : a virer
		  # exit 0 # gpdebug : a virer
	       
	          packFailed=`echo $status | grep -E '(DELEGATE|FAILED|illisible)' | wc -l `
	          if [ "x${packFailed}" != "x0" ]
	          then
		     nbOfListsFailed=$(( $nbOfListsFailed + 1 ))
	          fi
	       
	          packSuccess=`echo $status | grep "COMPLETED" | wc -l `
	          if [ "x${packSuccess}" != "x0" ]
	          then
		     timeTaken=$( getTimeTaken $statusfile )
	             nbOfListsPacked=$(( $nbOfListsPacked + 1 ))
	          fi
		  
		  echo "       $listName : $status | time : $timeTaken"
	     done
         done
    fi
done
echo
echo "##################"
echo "#####  BILAN #####"
echo "##################"
echo "nb of Lists packed with success : ${nbOfListsPacked} / ${totalNbOfList}"
echo "nb of fails : $nbOfListsFailed"




