#!/bin/bash

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

# On renseigne les variables d'environnement 
. load_ipslPack_env.sh

SCRIPT_NAME=$(basename ${0} )

export IGCM_TMP="${IGCM_DEM}/tmp"  # gpdebug : added

export config_card=${IGCM_DEM}/config_card.liste

source ${EXE_DIR}/DEM_utilities.sh

export timeHandlingFile="${USER_OUTPUT_PROGRESS}/timeHandlingFile.txt"
export timeLaunchStartFile="${USER_OUTPUT_PROGRESS}/timeLaunchStartFile.txt"
export timeEndFile="${USER_OUTPUT_PROGRESS}/timeEndFile.txt"


function getSimuName
{   
   config_card=$1
   
   dirSimu=$( dirname $config_card )
   simuName=`echo ${dirSimu} | sed "s;${IGCM_DEM}/;;" `
   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
  
}


function getWaitingTime
{
    timeNow_=$1
    stillWaiting_=1
    
    launchTimeString=`cat $timeHandlingFile | grep -e 'launch time:[[:digit:].]*$' `
    if [ "x${launchTimeString}" == "x" ]
    then
        echo "no launch time"
	return
    fi
    launchTime_=`echo $launchTimeString | awk -F":" '{print $2}' `
    
    
    startExecTimeString=`cat $timeHandlingFile | grep -e 'start time:[[:digit:].]*$' `
    if [ "x${startExecTimeString}" == "x" ]
    then
        execTime_=${timeNow_}	
    else
        execTime_=`echo $startExecTimeString | awk -F":" '{print $2}' `
	stillWaiting_=0
    fi
    
    waitingTime_=$( awk 'BEGIN { print '${execTime_}'-'${launchTime_}' }' )
    waitingTime_=$( awk 'BEGIN { print '${waitingTime_}'/'1000' }' )
    if [ "x$stillWaiting_" == "x1" ]
    then
       waitingTime_="${waitingTime_}, still waiting..."
    fi
    
    echo $waitingTime_
}
    
    
function getTimeSinceExecutionStart
{  
    
    timeNow_=$1
    stillWaiting_=1    
    
    execTimeString=`cat $timeHandlingFile | grep -e 'start time:[[:digit:].]*$' `
    if [ "x${execTimeString}" == "x" ]
    then
        echo "no start time"
	return
    fi
    execTime_=`echo $execTimeString | awk -F":" '{print $2}' `
    
        
    endTimeString=`cat $timeHandlingFile | grep -e 'end time:[[:digit:].]*$' `
    if [ "x${endTimeString}" == "x" ]
    then
	endTime_=${timeNow_}
    else
	endTime_=`echo $endTimeString | awk -F":" '{print $2}' `
	stillWaiting_=0
    fi
    
    timeSinceExec_=$( awk 'BEGIN { print '${endTime_}'-'${execTime_}' }' )
    timeSinceExec_=$( awk 'BEGIN { print '${timeSinceExec_}'/'1000' }' )
    if [ "x$stillWaiting_" == "x1" ]
    then
       timeSinceExec_="${timeSinceExec_}, still executing..."
    fi
    
    echo $timeSinceExec_
}

function getTimeSinceLaunchStart
{
    timeNow_=$1
    stillWaiting_=1
    
    launchTimeString=`cat $timeLaunchStartFile | grep -e 'launch time:[[:digit:].]*$' `
    if [ "x${launchTimeString}" == "x" ]
    then
        echo "no launch time"
	return
    fi
    launchTime_=`echo $launchTimeString | awk -F":" '{print $2}' `
    
    
    endTimeString=`cat $timeEndFile | grep -e 'end time:[[:digit:].]*$' `
    if [ "x${endTimeString}" == "x" ]
    then
	endTime_=${timeNow_}
    else
	endTime_=`echo $endTimeString | awk -F":" '{print $2}' `
	stillWaiting_=0
    fi
    
    timeSinceLaunch_=$( awk 'BEGIN { print '${endTime_}'-'${launchTime_}' }' )
    timeSinceLaunch_=$( awk 'BEGIN { print '${timeSinceLaunch_}'/'1000' }' )
    if [ "x$stillWaiting_" == "x1" ]
    then
       timeSinceLaunch_="${timeSinceLaunch_}, still executing..."
    fi
    
    echo $timeSinceLaunch_

}


function getListProgress
{
     set=$1     
     
     listName=`echo $(basename $set) | sed 's;\.list$;;' | sed 's;\.nc$;;' `
     ListNbInode=`cat $set |wc -l`
     TotalNbInodes=$(( $TotalNbInodes + $ListNbInode ))
     statusfile=${set%%.list}.status
     status=$( progressStatus $statusfile ) # gpdebug : a retablir
     timeTaken="???"
  
     packFailed=`echo $status | grep -E '(DELEGATE|FAILED)' | wc -l `
     if [ "x${packFailed}" != "x0" ]
     then
   	nbOfListsFailed=$(( $nbOfListsFailed + 1 ))
	simuOK=$(( $simuOK && 0 )) # marque une simu pas OK
     fi
  
     packSuccess=`echo $status | grep "COMPLETED" | wc -l `
     if [ "x${packSuccess}" != "x0" ]
     then
   	timeTaken=$( getTimeTaken $statusfile )
	nbOfListsPacked=$(( $nbOfListsPacked + 1 ))
     fi
     
     packNottreated=`echo $status | grep -E '(Non traitee|illisible)' | wc -l `
     if [ "x${packNottreated}" != "x0" ]
     then
	 nbOfListsNottreated=$(( $nbOfListsNottreated + 1 ))
	 simuOK=$(( $simuOK && 0 )) # marque une simu pas OK
     simutreated=$(( $simutreated && 0 )) # marque une simu non completement traitee
     fi

     echo "	  $listName : $status | time : $timeTaken" >> simuPrint.txt
     
     timeInSecondsOK=`echo $timeTaken | grep -e '^[[:digit:].]*$' | wc -l `
     if [ "x${timeInSecondsOK}" != "x0" ]
     then
   	 totalTime=$(awk 'BEGIN { print '$totalTime'+'$timeTaken' }')
     fi



}

#################################
####### Main script #############
#################################

########  Options du script ########################################################
focusOnSimu=
outputFile=
detailed_mode=1
publish_mode=1
while [ $# -gt 0 ]
do
   # echo "boucle sur les arguments du script ..."
   # echo "@=$@"
   case $1 in
   -s)  echo "option -s selectionnee : afficher une simulation"
        shift
	focusOnSimu=$1
	is_simu_an_option=`echo $focusOnSimu | grep -e '^-' | wc -l `
	if [ "x${is_simu_an_option}" != "x0" ] || [ "x$focusOnSimu" == "x" ]
	then
	   echo "L'option -s doit etre suivie d'une simulation !"
	   exit 1
	fi
	;;
   -o)  echo "option -o selectionnee : fichier d'output"
        shift
	outputFile=$1
	is_output_an_option=`echo $outputFile | grep -e '^-' | wc -l `
	if [ "x${is_output_an_option}" != "x0" ] || [ "x$outputFile" == "x" ]
	then
	   echo "L'option -o doit etre suivie d'un fichier d'output !"
	   exit 1
	fi
	;;
   --not-detailled)  echo "option '--not-detailled' selectionnee"
                     detailed_mode=0
                     ;;
   -p) echo "option -p selectionnee : publication"
       publish_mode=0      
   esac
   shift
done

# echo "focusOnSimu=$focusOnSimu"
# echo "outputFile=$outputFile"
# echo "detailed_mode=$detailed_mode"
######################################################################################
# exit 0 # gpdebug : a virer



> showPackProgress.log # vider le fichier de log


# Recuperation des temps d'attente et ecoule depuis de demarrage des traitements en batch
if [ -e $timeLaunchStartFile ]
then
    timeNow=$( getDateMilliSeconds )
    # waitingTime=$( getWaitingTime $timeNow ) # supprime pour le moment : pb si +sieurs instances ==> on simplifie
    # timeSinceExecutionStart=$( getTimeSinceExecutionStart $timeNow ) # supprime pour le moment : pb si +sieurs instances ==> on simplifie
    timeSinceLaunchStart=$( getTimeSinceLaunchStart $timeNow ) # added
else
    # waitingTime="no handling time file available." # supprime pour le moment : pb si +sieurs instances ==> on simplifie
    # timeFromExecutionStart="no handling time file available." # supprime pour le moment : pb si +sieurs instances ==> on simplifie
    timeSinceLaunchStart="no launch time file available."
fi


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

TotalNbSimu=0
nbOfSimuPacked=0
nbOfSimuFailed=0
nbOfSimuNottreated=0
nbOfListsPacked=0
nbOfListsFailed=0
nbOfListsNottreated=0
TotalNbInodes=0
TotalSimuTar=0
totalTime=0

TotalNbSimu=`cat ${IGCM_DEM}/config_card.liste |wc -l`

if [ "x${outputFile}" == "x" ] && [ "x${publish_mode}" == "x0" ]
then
# Publication
echo "Simulation;Status" > ${outputFile}_${USER}
fi

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

    DEM_state=$( DEM_read_state ${IGCM_DEM}/config_card.liste ${CONFIG} )
    
    # Eviter les simus autre que celle selectionnee par l'option -s
    showCurrentSimu=`echo $CONFIG | grep "${focusOnSimu}" | wc -l `
    if [ "x${showCurrentSimu}" == "x0" ]
    then
        continue
    fi

    simuName=$( getSimuName $CONFIG )
    
    > simuPrint.txt

    export PATH_SIMU=$( dirname $CONFIG )
    # echo "PATH_SIMU = $PATH_SIMU"
    
    simuOK=1 
    simutreated=1
 
    if [ ! -e "${PATH_SIMU}/tar_full_simul.list" ]
    then
    	
	 listOfDir="output_ncrcat output_tar restart_tar debug_tar store_cp work_cp"
	 for dir in $listOfDir
	 do
	     echo "   ${dir} :" >> simuPrint.txt
	     if [ ! -d $PATH_SIMU/${dir} ]
	     then
	        echo "      ${dir} n'existe pas pour cette simu" >> simuPrint.txt
#		simuOK=$(( $simuOK && 0 )) # marque une simu pas OK
	        continue
	     fi
	   
	     listFilesInDir=`find $PATH_SIMU/${dir} -name "*list" | wc -l `
	     if [ "x$listFilesInDir" == "x0" ]
	     then
	        echo "       -- no list --" >> simuPrint.txt
	        continue	   
	     fi
	   
	     setList=$( ls $PATH_SIMU/${dir}/*list )

	     for set in $setList
	     do
	          getListProgress $set
	     done
         done
    else	 
	 getListProgress ${PATH_SIMU}/tar_full_simul.list
	 TotalSimuTar=$(( $TotalSimuTar + 1 ))
    fi
    
    resSimu="OK"
    if [ "x${simuOK}" == "x0" ]
    then
        resSimu="not OK"
    fi

    if [ "x${simutreated}" == "x0" ]
    then
        resSimu="not treated"
    fi

    if [ "x${resSimu}" == "xnot OK" ]
    then
       nbOfSimuFailed=$(( $nbOfSimuFailed + 1 )) 
    elif [ "x${resSimu}" == "xnot treated" ]
    then
       nbOfSimuNottreated=$(( $nbOfSimuNottreated + 1 ))
    else
        nbOfSimuPacked=$(( $nbOfSimuPacked + 1 ))
    fi

    if [ "x${outputFile}" == "x" ]
    then
        echo "***************************************************"
        echo "simulation : $simuName  ===> $resSimu"
        echo "***************************************************"
        if [ "x${detailed_mode}" == "x1" ]
        then
    	    cat simuPrint.txt  $outRedirectStatement
        fi
    else
        if [ "x${publish_mode}" == "x1" ]
        then
        echo "***************************************************" >> ${outputFile}
        echo "simulation : $simuName  ===> $resSimu"               >> ${outputFile}
        echo "***************************************************" >> ${outputFile}
        if [ "x${detailed_mode}" == "x1" ]
        then
    	    cat simuPrint.txt  $outRedirectStatement               >> ${outputFile}
        fi     
        else
# Publication
        echo "$simuName;$resSimu" >> ${outputFile}_${USER}
        fi
    fi 
    rm simuPrint.txt
done

NbInodeBefore=`cat ${IGCM_DEM}/Listing.txt |wc -l`
# find $IGCM_DEM -printf "%y %s %p \n" >> ${IGCM_DEM}/ListingEnd.txt
rm -f ${IGCM_DEM}/ListingEnd.txt
 if [ -d "${OUTPUT_STORE}" ]
 then
     find $OUTPUT_STORE -printf "%y %s %p \n" >> ${IGCM_DEM}/ListingEnd.txt
 fi
 if [ -d "${OUTPUT_WORK}" ]
 then
     find $OUTPUT_WORK -printf "%y %s %p \n" >> ${IGCM_DEM}/ListingEnd.txt
 fi
NbInodeAfter=`cat ${IGCM_DEM}/ListingEnd.txt |wc -l`

if [ "x${outputFile}" == "x" ]
then
    echo
    echo "##################"
    echo "#####  BILAN #####"
    echo "##################"
    echo "nb of Simulations packed with success : ${nbOfSimuPacked} / ${TotalNbSimu}"
    echo "nb of Simus failed : ${nbOfSimuFailed}"
    echo "nb of Simus not treated : ${nbOfSimuNottreated}"
    echo "nb of Lists packed with success : ${nbOfListsPacked} / ${totalNbOfList}"
    echo "nb of fails : $nbOfListsFailed"
    echo "nb of not treated : ${nbOfListsNottreated}"
    echo "nb of inodes packed : $TotalNbInodes"
    echo "nb simul full tared : $TotalSimuTar"
    echo "nb inode before : $NbInodeBefore"
    echo "nb inode after : $NbInodeAfter"
    echo "Total time for elementary operations : $totalTime" 
    echo "Time since launch start : $timeSinceLaunchStart"

else
    if [ "x${publish_mode}" == "x1" ]
        then
    echo
    echo "##################" >> ${outputFile}
    echo "#####  BILAN #####" >> ${outputFile}
    echo "##################" >> ${outputFile}
    echo "nb of Simulations packed with success : ${nbOfSimuPacked} / ${TotalNbSimu}" >> ${outputFile}
    echo "nb of Simus failed : ${nbOfSimuFailed}" >> ${outputFile}
    echo "nb of Simus not treated : ${nbOfSimuNottreated}" >> ${outputFile}
    echo "nb of Lists packed with success : ${nbOfListsPacked} / ${totalNbOfList}" >> ${outputFile}
    echo "nb of fails : $nbOfListsFailed" >> ${outputFile}
    echo "nb of not treated : ${nbOfListsNottreated}" >> ${outputFile}
    echo "nb of inodes packed : $TotalNbInodes" >> ${outputFile}
    echo "nb simul full tared : $TotalSimuTar" >> ${outputFile}
    echo "nb inode before : $NbInodeBefore" >> ${outputFile}
    echo "nb inode after : $NbInodeAfter" >> ${outputFile}
    echo "Total time for elementary operations : $totalTime" >> ${outputFile}
    echo "Time since launch start : $timeSinceLaunchStart" >> ${outputFile}
    else
# Publication
    echo "User;TotalNbSimu;NbOfSimuPacked;NbOfSimuFailed;NbOfSimuNottreated;TotalNbOfList;NbOfListsPacked;NbOfListsFailed;NbOfListsNottreated;TotalNbFiles;Nbfilestreated;Nbfilesafter;NbSimuTar" > ${outputFile}
    echo "${USER};${TotalNbSimu};${nbOfSimuPacked};${nbOfSimuFailed};${nbOfSimuNottreated};${totalNbOfList};${nbOfListsPacked};$nbOfListsFailed;${nbOfListsNottreated};$TotalNbInodes;$NbInodeBefore;$NbInodeAfter;$TotalSimuTar" >> ${outputFile}
    fi
fi
