#!/bin/bash

##
## Job permettant de recreer une partie du config.card d'une simulation
## Champs vises : JobName / DateBegin / DateEnd / Calendar 
##

# Prend en argument un fichier contenant une liste de simulations
# cree pour chacune son config.card
# Ex : ./create_config_card.job liste_simul_fichier_param_test1.txt liste_fichier_dmnfs
# Ex : ./create_config_card.job liste_simul_param_SL.txt /cscratch/cont003/labetoul/FROM_CCRT_INFORMATIONS_DMNFS/information_dmnfs_2012-03-21


LISTE_SIMUL=${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}
clean_tmp=${clean_tmp:-yes}
EXE_DIR=$(dirname ${0})
SCRIPT_NAME=$(basename ${0} )
source ${EXE_DIR}/DEM_utilities.sh
DEM_log -0 "Demarrage de ${SCRIPT_NAME}"
cd ${JOB_DIR}
# ----

DEM_log -0 "LISTE_SIMUL : ${LISTE_SIMUL}"
DEM_log -0 "LISTE_DMNFS : ${LISTE_DMNFS}"

[[ -f ${IGCM_DEM}/config_card.liste ]] && rm ${IGCM_DEM}/config_card.liste

#ROOT_DIR=/dmnfs05/cont003/labetoul # soit DMFDIR, soit le tampon. a ameliorer ...


cd ${JOB_DIR}

for PATH_SIMUL_FULL in `awk  '{print $0}' ${LISTE_SIMUL}` ; do 

    DEM_log -3 PATH_SIMUL_FULL : ${PATH_SIMUL_FULL}
    #PATH_SIMUL_SHORT=${PATH_SIMUL_FULL##${ROOT_DIR}} # Enleve le debut du path
    PATH_SIMUL_SHORT=$(echo ${PATH_SIMUL_FULL} | sed 's%/[^/]*/[^/]*/[^/]*/%%' )
    DEM_log -3 PATH_SIMUL_SHORT : ${PATH_SIMUL_SHORT}
    
    IGCM_DEM_SIMU=${IGCM_DEM}/${PATH_SIMUL_SHORT}
    mkdir -p ${IGCM_DEM_SIMU}
    rm -rf ${IGCM_DEM_SIMU}/*

    #creation du repertoire permettant de stocker 
    #les listes de fichiers qui ne sont pas au format output ou restart
    #les listes de fichiers d'analyse, les monitos et les atlas
    OTHER_TAR=${IGCM_DEM_SIMU}/other_tar
    STORE_CP=${IGCM_DEM_SIMU}/store_cp
    WORK_CP=${IGCM_DEM_SIMU}/work_cp

    DEM_log -2 "creation des repertoires : $OTHER_TAR  - $STORE_CP - $WORK_CP"

    mkdir -p ${OTHER_TAR}
    mkdir -p ${STORE_CP}
    mkdir -p ${WORK_CP}

    # on recherche le nom du job
    JobName=""
    
    # Etablir la liste des fichiers d'Analyse
    grep ${PATH_SIMUL_FULL}/ ${LISTE_DMNFS} | grep Analyse  | awk '{print $3}' |  sort >> ${IGCM_TMP}/${$}.cp_analyse_files.list
    awk -F/ '{if ($(NF)=="Analyse") print $0} ' ${IGCM_TMP}/${$}.cp_analyse_files.list > ${STORE_CP}/cp_files.list

    #On recupere egalement le fichier mesh_mask si il existe 
    grep ${PATH_SIMUL_FULL}/ ${LISTE_DMNFS} | grep mesh_mask | awk '{print $3}' >> ${STORE_CP}/cp_files.list
    sed '/mesh_mask/d' ${LISTE_DMNFS} > ${IGCM_TMP}/${$}.Listing_tmp.txt; mv ${IGCM_TMP}/${$}.Listing_tmp.txt ${LISTE_DMNFS}

    # Etablir la liste des fichiers Monito et Atlas
    grep ${PATH_SIMUL_FULL}/ ${LISTE_DMNFS} | grep  MONITORING | awk '{print $3}' | sort >> ${IGCM_TMP}/${$}.cp_monito_files.list
    awk -F/ '{if ($(NF)=="MONITORING") print $0} ' ${IGCM_TMP}/${$}.cp_monito_files.list > ${WORK_CP}/cp_files.list

    grep ${PATH_SIMUL_FULL}/ ${LISTE_DMNFS} | grep  ATLAS | awk '{print $3}' | sort >> ${IGCM_TMP}/${$}.cp_atlas_files.list
    awk -F/ '{if ($(NF)=="ATLAS") print $0} ' ${IGCM_TMP}/${$}.cp_atlas_files.list >> ${WORK_CP}/cp_files.list
    

    # Etablir la liste des fichiers de restart
    grep ${PATH_SIMUL_FULL}/ ${LISTE_DMNFS} | grep Restart | awk '{print $2 " " $3}' | grep -e '.*/Restart/.*\..*' |grep -v "jnl" | sort  >> ${IGCM_TMP}/${$}.liste_restart_files_config.txt

    # Retirons de la liste les fichiers que l'on n'attends pas, en particulier si il  y a des sous-repertoires au repetoire restart/
    for file in `awk  '{print $2}'  ${IGCM_TMP}/${$}.liste_restart_files_config.txt ` ; do 
	dir=$( echo $file | sed -e "s%.*Restart/%%") 
	testdir=$(dirname ${dir}) 
	if [ $testdir != "." ]; then 
	    echo $( grep ${file} ${IGCM_TMP}/${$}.liste_restart_files_config.txt)  >> ${IGCM_TMP}/${$}.liste_no_restart_files_config.txt
	fi
    done
    if [ -f ${IGCM_TMP}/${$}.liste_no_restart_files_config.txt ]; then 
	join -t % -v 1 ${IGCM_TMP}/${$}.liste_restart_files_config.txt ${IGCM_TMP}/${$}.liste_no_restart_files_config.txt > ${IGCM_TMP}/${$}.info_tmp.txt
	mv ${IGCM_TMP}/${$}.info_tmp.txt ${IGCM_TMP}/${$}.liste_restart_files_config.txt
    fi
    
    # Etablir la liste des fichiers output
    grep ${PATH_SIMUL_FULL}/ ${LISTE_DMNFS} | grep Output  | awk '{print $2 " "  $3}' | grep -e '.*/Output/.*\.nc$'  | sort >> ${IGCM_TMP}/${$}.liste_output_files_config.txt
    # Retirons de la liste les fichiers que l'on n'attends pas, en particulier si il  y a des sous-repertoires aux repetoires Output/*/
    for file in `awk  '{print $2}'  ${IGCM_TMP}/${$}.liste_output_files_config.txt ` ; do 
	dir=$( echo $file | sed -e "s%.*Output/[^/]*/%%") 
	testdir=$(dirname ${dir}) 
	if [ $testdir != "." ]; then 
	    echo $( grep ${file} ${IGCM_TMP}/${$}.liste_output_files_config.txt ) >> ${IGCM_TMP}/${$}.liste_no_output_files_config.txt
	fi
    done
    if [ -f ${IGCM_TMP}/${$}.liste_no_output_files_config.txt ]; then 
	join -t % -v 1 ${IGCM_TMP}/${$}.liste_output_files_config.txt ${IGCM_TMP}/${$}.liste_no_output_files_config.txt > ${IGCM_TMP}/${$}.info_tmp.txt
	mv ${IGCM_TMP}/${$}.info_tmp.txt ${IGCM_TMP}/${$}.liste_output_files_config.txt
    fi




    # Lecture du nom du job
    JobName=`awk -F/ '{if (NR==1) print $(NF-3)}'  ${IGCM_TMP}/${$}.liste_restart_files_config.txt`
    DEM_log -2 JobName ${JobName}


# Maintenant que nous avons JobName verifions que tous les fichiers de restart/Output sont bien du type JobName_date_*
    sed -e "s%.*${JobName}_\([0-9]\{8\}\)_%%" ${IGCM_TMP}/${$}.liste_restart_files_config.txt  >  ${IGCM_TMP}/${$}.info_tmp.txt
    cat ${IGCM_TMP}/${$}.info_tmp.txt |grep "/" >  ${IGCM_TMP}/${$}.info_tmp_bis.txt
    join -t % -v 1 ${IGCM_TMP}/${$}.liste_restart_files_config.txt ${IGCM_TMP}/${$}.info_tmp_bis.txt > ${IGCM_TMP}/${$}.restart.tmp
    sort -k 2 ${IGCM_TMP}/${$}.restart.tmp > ${IGCM_TMP}/${$}.restart_bis.tmp
    mv ${IGCM_TMP}/${$}.restart_bis.tmp ${IGCM_DEM_SIMU}/liste_restart_files_config.txt
# Sauvegarde des fichiers qui ne sont pas dans la norme
    cat ${IGCM_TMP}/${$}.info_tmp_bis.txt >> ${IGCM_TMP}/${$}.liste_no_restart_files_config.txt
    mv ${IGCM_TMP}/${$}.liste_no_restart_files_config.txt ${OTHER_TAR}/tar_no_restart_files.list


    sed -e "s%.*${JobName}_\([0-9]\{8\}\)_%%" ${IGCM_TMP}/${$}.liste_output_files_config.txt  >  ${IGCM_TMP}/${$}.info_tmp.txt
    cat ${IGCM_TMP}/${$}.info_tmp.txt |grep "/" >  ${IGCM_TMP}/${$}.info_tmp_bis.txt
    join -t % -v 1 ${IGCM_TMP}/${$}.liste_output_files_config.txt ${IGCM_TMP}/${$}.info_tmp_bis.txt > ${IGCM_TMP}/${$}.output.tmp
    sort -k 2 ${IGCM_TMP}/${$}.output.tmp > ${IGCM_TMP}/${$}.output_bis.tmp
    mv ${IGCM_TMP}/${$}.output_bis.tmp ${IGCM_DEM_SIMU}/liste_output_files_config.txt
# Sauvegarde des fichiers qui ne sont pas dans la norme
    cat ${IGCM_TMP}/${$}.info_tmp_bis.txt >> ${IGCM_TMP}/${$}.liste_no_output_files_config.txt
    mv ${IGCM_TMP}/${$}.liste_no_output_files_config.txt ${OTHER_TAR}/tar_no_output_files.list


# Recherche de DateBegin et DateEnd pour les Restarts
    first_file=$(        head -n 1 ${IGCM_DEM_SIMU}/liste_restart_files_config.txt )
    extract_date_file=$( echo ${first_file}  | sed -e "s/.*${JobName}_//" )
    DateBegin_Restart=$( echo ${extract_date_file}| sed 's/\([0-9]\{8\}\)_.*$/\1/g' )
    
    last_file=$(         tail -n 1 ${IGCM_DEM_SIMU}/liste_restart_files_config.txt )
    extract_date_file=$( echo ${last_file}  | sed -e "s/.*${JobName}_//" )
    DateEnd_Restart=$(   echo ${extract_date_file}| sed 's/\([0-9]\{8\}\)_.*$/\1/g' )
    
# Recherche de DateBegin et DateEnd pour les Output
    first_file=$( head -n 1 ${IGCM_DEM_SIMU}/liste_output_files_config.txt )
    extract_date_file=$( echo ${first_file}  | sed -e "s/.*${JobName}_//" )
    DateBegin_Output=$(  echo ${extract_date_file}| sed 's/\([0-9]\{8\}\)_.*$/\1/g' )
    
    last_file=$(         tail -n 1 ${IGCM_DEM_SIMU}/liste_output_files_config.txt )
    extract_date_file=$( echo ${last_file}  | sed -e "s/.*${JobName}_//" )
    DateEnd_Output=$(    echo ${extract_date_file}| sed 's/\([0-9]\{8\}\)_.*$/\1/g' )
    
    DateBegin=${DateBegin_Restart}
    DateEnd=${DateEnd_Restart}

    DEM_log -2 "DateBegin DateEnd : ${DateBegin} ${DateEnd}"
    DEM_log -2 "DateBegin_Output DateEnd_Output : ${DateBegin_Output} ${DateEnd_Output}"
    DEM_log -2 "DateBegin_Restart DateEnd_Restart : ${DateBegin_Restart} ${DateEnd_Restart}"

    DateBegin=$( DEM_min ${DateBegin_Output} ${DateBegin} ) 
    DateEnd=$(   DEM_max ${DateEnd_Output}   ${DateEnd}   )
    
    DEM_log -2  "DateBegin DateEnd : ${DateBegin} ${DateEnd}"
    
    [[ "${clean_tmp}" = "yes" ]] && rm -f ${IGCM_TMP}/${$}.config_name ${IGCM_TMP}/${$}.liste_output_files_config.txt ${IGCM_TMP}/${$}.liste_restart_files_config.txt
    
    rm -f ${IGCM_DEM_SIMU}/config_card_${JobName}
    cat<<EOD>${IGCM_DEM_SIMU}/config_card_${JobName}
JobName=${JobName}
DateBegin=${DateBegin}
DateEnd=${DateEnd}
PATH_SIMUL_FULL=${PATH_SIMUL_FULL}
IGCM_DEM_SIMU=${IGCM_DEM_SIMU}
EOD
    
    echo "${IGCM_DEM_SIMU}/config_card_${JobName} ListToBeDone" >> ${IGCM_DEM}/config_card.liste

    DEM_log -1 "${IGCM_DEM_SIMU}/config_card_${JobName} ListToBeDone >> ${IGCM_DEM}/config_card.liste"


#si la liste des fichiers d'output est vide on tar la simul 
if [[ !  -s ${IGCM_DEM_SIMU}/liste_output_files_config.txt ]]; then
DEM_log -2 "liste output vide"
    ./write_liste_tar.sh ${IGCM_DEM_SIMU}/config_card_${JobName}
fi 

# si les listes no_output et no_restart sont vides on les efface
if [[ !  -s ${OTHER_TAR}/tar_no_output_files.list ]]; then 
    rm -f ${OTHER_TAR}/tar_no_output_files.list
fi 

if [[ !  -s ${OTHER_TAR}/tar_no_restart_files.list ]]; then 
    rm -f ${OTHER_TAR}/tar_no_restart_files.list
fi 


done

DEM_log -0 "Fin de ${SCRIPT_NAME}"

