#!/bin/bash


#### On veut calculer si l'ensemble des fichiers d'une simulation 
#### est bien superieur a 1G

config_card=${1}
LISTE_DMNFS=${2}

export JOB_DIR=${JOB_DIR:-${PWD}}
export IGCM_DEM=${IGCM_DEM:-${SCRATCHDIR}/IGCM_DEM}
export IGCM_TMP=${IGCM_TMP:-${IGCM_DEM}/tmp}
mkdir -p ${IGCM_DEM}
mkdir -p ${IGCM_TMP}

EXE_DIR=$(dirname ${0})
SCRIPT_NAME=$(basename ${0} )
source ${EXE_DIR}/DEM_utilities.sh

# gpdebug : verif si le present script a ete execute jusqu'au bout #########
MonitoringInfo="${SCRIPT_NAME}-->OK"
simuMonitoring_check "$MonitoringInfo" "$config_card"

DEM_log -0 "Demarrage de ${SCRIPT_NAME}"

eval $(grep JobName ${config_card}) 
eval $(grep PATH_SIMUL_FULL ${config_card}) 
eval $(grep DateBegin ${config_card}) 
eval $(grep DateEnd ${config_card}) 
eval $(grep IGCM_DEM_SIMU ${config_card})



## 1 Go
limitmin=1000000000

#cree la liste de l'ensemble des fichiers de la simulation
#echo ${PATH_SIMUL_FULL} ${LISTE_DMNFS}
grep ${PATH_SIMUL_FULL} ${LISTE_DMNFS} | grep 'f ' > ${IGCM_TMP}/$$.info_tmp.txt 
awk '{print $(NF-1) " " $(NF)}' ${IGCM_TMP}/$$.info_tmp.txt > ${IGCM_DEM_SIMU}/All_files_simul.txt

size=`awk 'BEGIN {y = 0} {x = $1 ; y = y + x } END{print y}'  ${IGCM_DEM_SIMU}/All_files_simul.txt`
DEM_log -3 "la taille de la simulation est  $size"
if [  ${size} -lt ${limitmin} ]
then

    DEM_log -0  "Simulation < 1Go -  on l'archive avec tar"
    ./write_liste_tar.sh ${config_card}
    MonitoringInfo="${SCRIPT_NAME}-->special simu==>OK"
    
fi

# gpdebug : le script n'a pas rencontre d'erreur. On l'inscrit dans le fichier de suivi
simuMonitoring_OK "$MonitoringInfo" "$config_card"

DEM_log -0 "Fin de ${SCRIPT_NAME}"
