#!/bin/bash

function mesg_make { 
    paste -d ';' mesg_*.txt > trusting_${DATESTR}.txt
    cat trusting_${DATESTR}.txt
}

#=============================================
function mesg_publish { 
  
if [ $PUBLISH -eq 1 ]; then

  if [ -f ${DIR_TEST}/trusting.txt ] ; then
      tail -1 trusting_${DATESTR}.txt >> ${DIR_TEST}/trusting.txt
  else
      cp      trusting_${DATESTR}.txt    ${DIR_TEST}/trusting.txt
  fi

  cp trusting_${DATESTR}.txt ${DIR_TEST}
#  chmod o+r ${DIR_TEST}/trusting_${DATESTR}.txt
  
#  rm -rf              ${DIR_WORK}/../../dods/public/${LOGNAME}/trusting/${CONF_REF}
#  cp -alf ${DIR_TEST} ${DIR_WORK}/../../dods/public/${LOGNAME}/trusting/${CONF_REF}
#  chmod o+rX          ${DIR_WORK}/../../dods/public/${LOGNAME}/trusting/${CONF_REF}
  
  # Send mail only when FAILED
  if [ grep 'FAILED' mesg_02_status.txt ]; then
      cat << END_MAIL > ${DIR_TEST}/trusting.mail
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Dear $USER,

The trusting sequence for ${CONF_REF} has failed.
Please consult http://webservices.ipsl.fr/trusting/

Failed at step: `tail -n 1 mesg_03_step.txt`
Directory : ${DIR_TEST}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

END_MAIL
      mail -s "[trusting ${CONF_REF}] FAILED" ntmlod@locean-ipsl.upmc.fr < ${DIR_TEST}/trusting.mail
  fi

fi

}

#=============================================
function getout { 
	cd ${DIR_TEST}
        mesg_make; mesg_publish

        # Add tar saved into {DIR_STOR}
	find ${DIR_SCRA}/trusting/${CONF_REF}/* -prune -mtime +7 -exec /bin/rm -rf {} \; > /dev/null 2>&1
        [ -f ${DIR_STOR}/${CONF_REF}.tar ] && \rm ${DIR_STOR}/${CONF_REF}.tar
        tar -cf ${DIR_STOR}/${CONF_REF}.tar .

        exit
}

#=============================================
function diffnc {
    stat=100; file1=$1; file2=$2

    cdo diffv $file1 $file2 > out_tmp
    ( [ $? -ne 0 ] && stat=100 ) || stat=$( grep -m 1 "records differ" out_tmp | awk '-Fof' '{print $1}' )
    [ -f out_tmp ] && rm out_tmp

    return $stat
} 

#---------------------------------------------
function diff_trusting {

    name_first=${JOB_NAME}
    name_second=EXP00C
    rep_ref=EXP00C_REFE
    experiment_name=trusting
    tagname=TEST
    #listcomp="ATM SBG SRF CPL ICE MBG OCE"
    listcomp="ATM SRF CPL ICE MBG OCE"
    result_final=0

# Output  
    cd ${DIR_SCRA}/IGCM_OUT/${CONFIG_WORK}/${tagname}/${experiment_name}/${name_first}
    for comp in ${listcomp}; do
	echo ${comp}
	cd ${DIR_SCRA}/IGCM_OUT/${CONFIG_WORK}/${tagname}/${experiment_name}/${name_first}/${comp}/Output
	listfreq=$(ls -d MO/ DA/)
	for freq in ${listfreq}; do
            echo ${freq}
            cd ${DIR_SCRA}/IGCM_OUT/${CONFIG_WORK}/${tagname}/${experiment_name}/${name_first}/${comp}/Output/${freq}
            listfile=$( ls *.nc )
            for file in ${listfile}; do
		var=$( echo $file | sed -e "s/^${name_first}//" )
		result=$( diffnc ${name_first}${var} ${DIR_SCRA}/IGCM_OUT/${CONFIG_WORK}/${tagname}/${CONFIG_WORK}/${rep_ref}/${comp}/Output/${freq}${name_second}${var} )
		result=$?
		echo 'diffnc '${name_first}${var} ${DIR_SCRA}/IGCM_OUT/${CONFIG_WORK}/TEST/${CONFIG_WORK}/${rep_ref}/${comp}/Output/${freq}${name_second}${var}
		echo $result
		(( result_final = result_final + result ))
            done
	done
    done
    return $result_final

}
