#!/bin/bash


# 1- faire un mv de la simu d'origine en JobName.BF (BF = Before Pack)
# 2- copier sur le store la simu repackee sur Pseudo_dmnfs_store
# 3- effacer dans JobName_BF les fichiers contenus dans les listes correctement traitees

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

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

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
#----------------------------------------------------------------
#----------------------------------------------------------------

# Gestion de l'option de lancement 
# si -R --> alors on active le stockage sur le store et le remove des anciennes donnees

execRmv=0
forceDirect2Rmv=0
while [ $# -gt 0 ]
do
    case $1 in
	-h)  cat <<fin
script permettant de finaliser l'operation de surpack
1- creer la liste des fichiers a effacer sur le store (tous les fichiers repackes) 
2- copie l'ancienne simulation en $SIMU.BP (for before Pack) 
3- copie sur le store la simulation repackee
4- efface dans $SIMU.BP les fichiers repackes

Usage : 
-h : mode d'emploi
-rmv : lance automatiquement la phase de copie sur le store et de remove des anciennes donnees. 
       sinon le script s'arrete a la creation de la liste de fichiers a effacer. 
fin
	    exit;;
	-rmv) execRmv=1 
	    ;;
	*) echo "option non reconnue"
	    exit
	    ;;
    esac
    shift
done



# liste des simus 

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

# liste des tar RESTART et DEBUG a effacer 
    config_card=$PATH_SIMU/config_card_*
    eval $(grep PATH_SIMUL_FULL ${config_card}) 
    eval $(grep IGCM_DEM_SIMU ${config_card})
    eval $(grep JobName ${config_card}) 
    
    SIMU=${PATH_SIMUL_FULL}
    SIMU_BP=${SIMU}.BP
    
    LITTLE_PATH=`echo $PATH_SIMUL_FULL | sed  "s%${INPUT_DMF_DATA}%%"`


    cd ${USER_OUTPUT_PROGRESS}
    find . -name "removedFiles*" > liste_files.txt

    rmv_file_bis=${USER_OUTPUT_PROGRESS}/rmv_afterSurPack.txt
    rm -f ${rmv_file_bis}

    for removedfile in `awk '{print $0}' liste_files.txt`; do 
	rmv_file=${removedfile}
	    
	
	if [[ -f  ${rmv_file_bis} ]] ; then 
	    cp ${rmv_file} ${rmv_file_bis}
	else
	    cat  ${rmv_file} >> ${rmv_file_bis}
	fi
	
	mkdir -p ${IGCM_DEM_SIMU}/restart_rmv
	rm -rf ${IGCM_DEM_SIMU}/restart_rmv/*
	for archive in  `awk '{print $2}' ${IGCM_DEM_SIMU}/liste_restart_files_config.txt`; do 
	    not_rmv=0
	    ccc_hsm get $archive
	    tar tf $archive > ${IGCM_DEM_SIMU}/restart_rmv/archive.rmv
	    for fichier in  `awk '{print $0}' ${IGCM_DEM_SIMU}/restart_rmv/archive.rmv`; do 
		FindFile=`grep $fichier $rmv_file`
		if [ "X$FindFile" == "X" ]; then
		    echo "$archive tar to be conserved"
		    not_rmv=1
		fi 
	    done
	    if [ $not_rmv == 0 ] ; then 
		echo "rm -f $archive" >> ${rmv_file_bis}
	    fi
	    rm -f ${IGCM_DEM_SIMU}/restart_rmv/archive.rmv
	done
	
	mkdir -p ${IGCM_DEM_SIMU}/debug_rmv
	rm -rf ${IGCM_DEM_SIMU}/debug_rmv/*
	for archive in  `awk '{print $0}' ${IGCM_DEM_SIMU}/liste_debug_files_config.txt`; do 
	    not_rmv=0
	    ccc_hsm get $archive
	    tar tf $archive > ${IGCM_DEM_SIMU}/debug_rmv/archive.rmv
	    for fichier in  `awk '{print $0}' ${IGCM_DEM_SIMU}/debug_rmv/archive.rmv`; do 
		FindFile=`grep $fichier $rmv_file`
		    if [ "X$FindFile" == "X" ]; then
			echo "$archive tar to be conserved"
			not_rmv=1
		    fi 
	    done
	    if [ $not_rmv == 0 ] ; then 
		echo "rm -f $archive" >> ${rmv_file_bis}
	    fi
	    rm -f ${IGCM_DEM_SIMU}/debug_rmv/archive.rmv
	done
    done
    sed -i -e "s%${SIMU}\/%${SIMU_BP}\/%g" ${rmv_file_bis}
    echo "grep ${JobName} ${rmv_file_bis} > ${rmv_file_bis}.tmp"
    grep ${JobName} ${rmv_file_bis} > ${rmv_file_bis}.tmp; mv  ${rmv_file_bis}.tmp  ${rmv_file_bis}
    chmod 755 ${rmv_file_bis}
	


    if [[ $execRmv == 1 ]] ; then 
# sauver l'ancienne version 
	mv ${SIMU} ${SIMU_BP}
#copier sur le store la nouvelle
	mv ${OUTPUT_STORE}${LITTLE_PATH}  ${SIMU}
#copie du repertoire Exe qui ne change pas 
	mv ${SIMU_BP}/Exe ${SIMU}/.
# effacer dans l'ancienne version les fichiers de rmv_file_bis
	${rmv_file_bis}
#creation de la liste des fichiers restant 
	find ${SIMU_BP} -printf "%y %s %p \n" > ${USER_OUTPUT_PROGRESS}/lastFiles_afterSurPack.txt

    fi 
done


