#!/bin/bash


comments() {
    echo 'Comments'      | tee    mesg_10_comments_${CFG_USER}_${CFG_ARCH}.txt
    grep $1 ocean.output | tee -a mesg_10_comments_${CFG_USER}_${CFG_ARCH}.txt
}

get_cpu_time() {
    # Portability !
    real_cpu_time=$( ${JOB_VIEW} ${job_num} | awk 'END{print $9}' )
    printf "Real CPU time\n%s\n" ${real_cpu_time} | tee mesg_08_realcputime_${CFG_USER}_${CFG_ARCH}.txt
}

diff_inputs() {
    mesg='Same'

    for file in inputs_list.txt $( ls namelist_* ) iodef.xml; do
	diff $file ${REFE_DIR}/$file
	[ $? -ne 0 ] && mesg='Different'
    done

    printf "Input files\n$mesg" | tee mesg_09_inputfiles_${CFG_USER}_${CFG_ARCH}.txt
}

diff_restart() {
    last_time_step=$( cat ${REFE_DIR}/time.step | tr -d [:space:] )
    echo 'Last time step of standard run: '${last_time_step}

    if [ $( find ${TEST_DIR} -name "*${last_time_step}_restart*.nc" -print -quit ) ]; then
	base_name=$( find ${TEST_DIR} -name "*${last_time_step}_restart*.nc" -print -quit           \
	             | awk -F/ '{print $NF}' | sed "s/^\(.*\)$last_time_step\_.*/\1$last_time_step/" )

	for comp in restart restart_ice restart_trc; do
	    printf "${base_name}_${comp}.nc: "

	    nb_dom=$( find ${TEST_DIR} -name "${base_name}_${comp}_[0-9]*.nc" | wc -l | awk '{print $1}' )
	    if [ ${nb_dom} -gt 1 ]; then
		${NEMO_TRBD}/rebuild_nemo -t $NPROC ${base_name}_$comp ${nb_dom} > /dev/null
	    fi

	    cmp ${TEST_DIR}/${base_name}_$comp.nc ${REFE_DIR}/${base_name}_$comp.nc
	    if [ $? -ne 0 ]; then
		status='FAILED'
		printf "$CDO diffn\n"
		$CDO diffn ${TEST_DIR}/${base_name}_$comp.nc ${REFE_DIR}/${base_name}_$comp.nc 2> /dev/null
	    else
		printf "identical\n"
	    fi

	    \rm  ${TEST_DIR}/${base_name}_${comp}_[0-9]*.nc
	done

    else
	printf "no restart files found for comparison\n"
    fi
}

get_soft_rel() {
    for rel in $CDO $COMPILER $MPI $NETCDF; do
                                 arch_rel=$( echo $LOADEDMODULES | sed  "s#.*$rel/\([^:]*\).*#\1#" )
	[ -z "${arch_rel}"  ] && arch_rel=$( echo $PATH          | sed  "s#.*$rel/\([^/]*\).*#\1#" )
	[ $rel == $COMPILER ] && arch_rel=$( $rel --version      | grep -m1 -o ' [0-9.]* '         )
	echo $rel ${arch_rel} >> model.log
    done

    sed -n 2p model.log | sed 'i\Compiler'    | tee mesg_05_compiler_${CFG_USER}_${CFG_ARCH}.txt
    sed -n 3p model.log | sed 'i\MPI libs'    | tee mesg_06_mpi_${CFG_USER}_${CFG_ARCH}.txt
    sed -n 4p model.log | sed 'i\NetCDF libs' | tee mesg_07_netcdf_${CFG_USER}_${CFG_ARCH}.txt
}

get_nemo_rev() {
    rev=0

    # If -v|--version option has been set, modify svn command
    if   [ $( echo ${NEMO_VERS} | grep  "up\|update"         ) ]; then SVN_OPT='update'
    elif [ $( echo ${NEMO_VERS} | tr -d '[:alpha:][:punct:]' ) ]; then SVN_OPT='checkout'
	rev=$( echo ${NEMO_VERS} | tr -d '[:alpha:][:punct:]' )
    fi

    for dir in ${DIR_XIOS}  ${NEMO_ARCH} ${NEMO_CONF} ${NEMO_ENGI} ${NEMO_EIOI} \
	       ${NEMO_EFCM} ${NEMO_TRUS} ${NEMO_TCMP} ${NEMO_TRBD}               ; do

	# For time being, just get revision number from XIOS
	if [ $dir == ${DIR_XIOS} ]; then
	    echo 'XIOS '$( svn info $dir | awk '(NR == 9) {print $NF}' ) >> model.log
	    continue
	fi

	# 
	if [ $( echo ${SVN_OPT} | grep 'checkout' ) ]; then
	    cd ../$dir 
	    url=$( svn info $dir | grep URL | awk '{print $NF}' )
	    svn ${SVN_OPT} $url -r $rev $dir && cd -
	else
	    svn ${SVN_OPT} $dir
	fi

	rev_loc=$( svn info $dir | awk '(NR == 5) {print $NF}' )
	(( ${rev_loc} >= $rev )) && rev=${rev_loc}
    done

    echo 'NEMO '$rev >> model.log
    sed -n 6p model.log | sed 'i\NEMOGCM rev.' | tee mesg_02_nemo_${CFG_USER}_${CFG_ARCH}.txt
}

mesg_make() {
    paste -d ';' mesg_*_${CFG_USER}_${CFG_ARCH}.txt > trusting_${DATE_STR}_${CFG_USER}_${CFG_ARCH}.txt
    cat                                               trusting_${DATE_STR}_${CFG_USER}_${CFG_ARCH}.txt
}

mesg_publish() {
  
if [ $PUBLISH -eq 1 ]; then

  if [ -f ${REFE_DIR}/trusting_${CFG_USER}_${CFG_ARCH}.txt ]; then cmd='sed -n 2p'; else cmd='cat'; fi

  $cmd trusting_${DATE_STR}_${CFG_USER}_${CFG_ARCH}.txt >> ${REFE_DIR}/trusting_${CFG_USER}_${CFG_ARCH}.txt

  # Send mail only when FAILED
  if [[ $( grep 'FAILED' mesg_02_status_${CFG_USER}_${CFG_ARCH}.txt ) && ! -z $MAIL ]]; then
      cat << END_MAIL > trusting.mail
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Dear $USER,

The trusting sequence for ${CONF_REF} has failed at step:
`tail -n 1 mesg_03_step_${CFG_USER}_${CFG_ARCH}.txt`
Directory : ${TEST_DIR}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

END_MAIL
      cat mesg_*_${CFG_USER}_${CFG_ARCH}.txt        >> trusting.mail
      mail -s "[trusting ${CONF_REF}] FAILED" $MAIL  < trusting.mail
  fi

fi
}

pstep() {
    printf "Step.....\n$1\n"
}

get_out() {
    step=$1; status=$2
    mesg_make; mesg_publish

    # Save tested configuration if trusting failed
    if [ $status == 'FAILED' ]; then
	touch mesg_08_realcputime_${CFG_USER}_${CFG_ARCH}.txt mesg_10_comments_${CFG_USER}_${CFG_ARCH}.txt
	cd ${REFE_DIR}
	tar -czf trusting_${DATE_STR}_${CFG_USER}_${CFG_ARCH}.tar.gz ${TEST_DIR}/*
	cd -
    fi

    printf "Status\n$status\n"  > mesg_03_status_${CFG_USER}_${CFG_ARCH}.txt
    printf "Step.....\n$step\n" > mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt

    exit 1
}
