#!/bin/bash
#
# Author: Patrick Brockmann
# Contact: Patrick.Brockmann@cea.fr
# History:
# Modification: Nicolas-thierry.Martin@locean-ipsl.upmc.fr
#


# Set defaults
#---------------------------------------------------
NEMO_TRUS=$( pwd $( dirname $0 ) )
SVN_CMD='svn status'
DEBUG=0; PUBLISH=0; status='OK'

# Get options for replacing some initials settings
#---------------------------------------------------
while [ $# -ne 0 ]; do
    case $1 in
        -b|--branch ) NEMO_BRAN=$2         ; shift 2;; -d|--debug  ) set -vx; DEBUG=1     ; shift  ;;
	-e|--email  ) MAIL=$2              ; shift 2;; -j|--job    ) NPROC=$2             ; shift 2;;
	-h|--help   ) cat trusting_help.txt; exit  1;; -m|--machine) CFG_ARCH=$2          ; shift 2;;
	-n|--newconf) TEST_CONF=$2         ; shift 2;; -r|--refconf) REFE_CONF=$2         ; shift 2;;
	-t|--time   ) TIME_LIMI=$2         ; shift 2;; -p|--publish) PUBLISH=1            ; shift  ;;
        -u|--user   ) CFG_USER=$2          ; shift 2;; -v|--version) NEMO_VERS=$2         ; shift 2;;
                   *) cat trusting_help.txt; exit  1;;
    esac
done

#export DEBUG PUBLISH


# Initialization (super-computer & user environment)
#---------------------------------------------------
if [[ ! -z "${CFG_ARCH}" && ! -z "${CFG_USER}" ]]; then

    if   [ ! $( find ${NEMO_TRUS}/cfg -name ${CFG_USER}.cfg ) ]; then
	echo 'No configuration file in '${NEMO_TRUS}'/cfg for '${CFG_USER}; exit 1
    elif [ ! $( find ${NEMO_TRUS}/cfg -name ${CFG_ARCH}.cfg ) ]; then
	echo 'No configuration file in '${NEMO_TRUS}'/cfg for '${CFG_ARCH}; exit 1
    else
	. ${NEMO_TRUS}/trusting.env
    fi

else
    cat ${NEMO_TRUS}/trusting_help.txt; exit 1
fi

# If -v|--version option has been set, modify svn command
[ $( echo ${NEMO_VERS} | grep  "up\|update"         ) ] && SVN_CMD='svn update'
[ $( echo ${NEMO_VERS} | tr -d '[:alpha:][:punct:]' ) ] && SVN_CMD='svn update -r '$( echo ${NEMO_VERS} | tr -d '[:alpha:][:punct:]' )

datestr1=$( LC_ALL=C date -d"${DATE_STR}" +%Y-%m-%dT%H:%M )
. ./trusting_func.sh

mkdir -p ${TEST_DIR} ${REFE_DIR}
echo ${TEST_DIR} && cd ${TEST_DIR}
printf "Date\n%s\n"       $datestr1 > mesg_01_date_${CFG_USER}_${CFG_ARCH}.txt
# Only at end status will change to OK
printf "Status\nFAILED\n"           > mesg_03_status_${CFG_USER}_${CFG_ARCH}.txt


# Check softwares versions
#---------------------------------------------------
printf "Step......................\nGet softwares release\n"                     | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt
. ${ARCH_ENV} > /dev/null    # Sourcing architecture environment
get_rel
sed -n 2p model.log | sed 'i\Fortran compiler' | tee mesg_05_compiler_${CFG_USER}_${CFG_ARCH}.txt
sed -n 3p model.log | sed 'i\MPI libraries   ' | tee mesg_06_mpi_${CFG_USER}_${CFG_ARCH}.txt
sed -n 4p model.log | sed 'i\NetCDF libraries' | tee mesg_07_netcdf_${CFG_USER}_${CFG_ARCH}.txt


# SVN action on XIOS & NEMO essentials directories
#---------------------------------------------------
printf "Step......................\nGet NEMO SVN revision with: ${SVN_CMD}\n"    | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt
get_rev
sed -n 6p model.log | sed 'i\NEMO revision' > mesg_02_nemo_${CFG_USER}_${CFG_ARCH}.txt


# NEMO config compiled from scratch
#---------------------------------------------------
printf "Step......................\nCompiling ${CONF_REF} configuration\n"       | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt
cd ${NEMO_CONF}
#[ -d ${TEST_CONF} ] && ./makenemo -n ${TEST_CONF} clean
./makenemo -n ${TEST_CONF} -r ${REFE_CONF} -m $ARCH -j $NPROC > /dev/null 2>&1
[ ! -e ${TEST_CONF}/BLD/bin/nemo.exe ] && get_out


# Get namelists, xml & forced files for running
#---------------------------------------------------
cd ${TEST_DIR}
printf "Step......................\nPreparing job\n"                             | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt
find ${NEMO_CONF}/${TEST_CONF}/EXP00 -regex '.*\(cfg\|opa\|ref\|xml\)' -exec cp {} . \;

if [ ! -z "${FORC_TAR}" ]; then
    cmd_iol="tar -tvf ${NEMO_FORC}/${FORC_TAR}"; cmd_iof="tar -vxf ${NEMO_FORC}/${FORC_TAR}"
else
    cmd_iol="ls ${NEMO_FORC}/*"                ; cmd_iof="\cp ${NEMO_FORC}/* ."
fi

${cmd_iol} > inputs_list.txt && ${cmd_iof} > /dev/null
[ $? -ne 0 ] && get_out
[ $( find . -name '*.gz' -print -quit ) ] && gunzip *.gz


# Job submission & computing
#---------------------------------------------------
printf "Step......................\nRunning\n"                                   | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt; echo ${JOB_SUBM}
job_num=$( ${JOB_SUBM} | awk '{print $NF}' )
[ $? -ne 0 ] && get_out

time_elapsed=0; time_increment=30
sleep ${time_increment}
while [[ $( ${JOB_LIST} | grep ${job_num} ) && ${time_elapsed} -lt ${TIME_LIMI} ]]; do
    printf "\n####################################################" >> computation.log
    ${JOB_VIEW} ${job_num}                                          >> computation.log
    sleep ${time_increment}
    let time_elapsed+=${time_increment}
done
sleep ${time_increment}

# Eventual crash during computing
#---------------------------------------------------
printf "Step......................\nTest if ASCII output files exist\n"          | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt
if   [ ! -f ocean.output                  ]; then
    printf "Step......................\nOpa crashed at initialization\n"         | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt
    ${JOB_DELE} ${job_num} >/dev/null 2>&1
    status='FAILED'
elif [ $( grep 'E R R O R' ocean.output ) ]; then
    printf "Step......................\nOpa crashed\n"                           | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt
    grep 'E R R O R'       ocean.output | tee    mesg_10_comments_${CFG_USER}_${CFG_ARCH}.txt
    sed 's/^/Time step :/' time.step    | tee -a mesg_10_comments_${CFG_USER}_${CFG_ARCH}.txt
    ${JOB_DELE} ${job_num} >/dev/null 2>&1
    status='FAILED'
fi


# Inspect output text files
#---------------------------------------------------
printf "Step......................\nTest ASCII output files difference\n"        | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt
for file in ocean.output solver.stat tracer.stat; do
    diff $file ${REFE_DIR}/$file
    [ $? -ne 0 ] && status='FAILED'
done


# Inspect output NetCDF files
#---------------------------------------------------
printf "Step......................\nTest last restart NetCDF files difference\n" | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt
diff_restart 2>/dev/null
[ $? -ne 0 ] && status='FAILED'


# Finals tests
#---------------------------------------------------
if [ $status != 'FAILED' ]; then

    # Comments
    #---------------------------------------------------
    echo 'Comments'                    | tee    mesg_10_comments_${CFG_USER}_${CFG_ARCH}.txt
    grep 'W A R N I N G:' ocean.output | tee -a mesg_10_comments_${CFG_USER}_${CFG_ARCH}.txt

    # Portability !
    printf "Step......................\nGet real CPU time\n"                     | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt
    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
else
    printf "Step......................\nTest input files difference\n"           | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt
    diff_inputs
    get_out
fi


# So far, everything is good
#---------------------------------------------------
printf "Status\nOK\n" > mesg_03_status_${CFG_USER}_${CFG_ARCH}.txt
printf "Step......................\nCode is reliable\n"                          | tee mesg_04_step_${CFG_USER}_${CFG_ARCH}.txt


# End
#---------------------------------------------------
get_out
