#!/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 ) )
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

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

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

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

. ./trusting_func.sh

# 
#---------------------------------------------------
mkdir -p ${TEST_DIR} ${REFE_DIR}
echo ${TEST_DIR} && cd ${TEST_DIR}
datestr1=$( LC_ALL=C date -d"${DATE_STR}" +%Y-%m-%dT%H:%M )
printf "Date\n$datestr1\n" > mesg_01_date_${CFG_USER}_${CFG_ARCH}.txt
# Only at end status will change to OK
STATUS='FAILED'; STEP=''

# SVN action on XIOS & NEMO essentials directories
#---------------------------------------------------
print_step "Get NEMO SVN state with: ${SVN_OPT}"
get_nemo_rev

# Check softwares versions (source arch environment)
#---------------------------------------------------
print_step 'Get softwares release'
. ${ARCH_ENV} > /dev/null
get_soft_rel
cat env.log

# XIOS compiling
#---------------------------------------------------
print_step 'Compiling XIOS'
cd ${DIR_XIOS}
#./make_xios --arch $ARCH -job $NPROC >& /dev/null

# NEMO config compiled from scratch
#---------------------------------------------------
print_step "Compiling ${CONF_REF} configuration"
cd ${NEMO_CONF}
#[ -d ${TEST_CONF} ] && ./makenemo -n ${TEST_CONF} clean
./makenemo -n ${TEST_CONF} -r ${REFE_CONF} -m $ARCH -j $NPROC >& /dev/null
[ ! -e ${TEST_CONF}/BLD/bin/nemo.exe ] && get_out $STATUS $STEP

# Get namelists, xml & forced files for running
#---------------------------------------------------
print_step 'Preparing job'
cd ${TEST_DIR}
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 $STATUS $STEP
[ $( find . -name '*.gz' -print -quit ) ] && gunzip *.gz

# Check input files in all cases
#---------------------------------------------------
print_step 'Test input files difference'
diff_inputs

# Job submission & computing
#---------------------------------------------------
print_step 'Running'
JOB_ID=$( ${JOB_SUBM} | awk '{print $NF}' )
export JOB_ID
[ $? -ne 0 ] && get_out $STATUS $STEP

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

# Eventual crash during computing
#---------------------------------------------------
print_step 'Test if ASCII output files exist'
if   [[ ! -e ocean.output || $( grep 'E R R O R' ocean.output ) ]]; then
    ${JOB_DELE} ${JOB_ID} &> /dev/null
    [ ! -e ocean.output                  ] && STEP='nemo.exe crashed at initialization'
    [ $( grep 'E R R O R' ocean.output ) ] && STEP='nemo.exe crashed'
    comments 'E R R O R'
    ${JOB_DELE} ${JOB_ID} >/dev/null 2>&1
    get_out $STATUS $STEP
else
    STATUS='OK'

    # Inspect output text files
    #---------------------------------------------------
    print_step 'Test ASCII output files diff'
    for file in ocean.output solver.stat tracer.stat; do
	diff $file ${REFE_DIR}/$file
	[ $? -ne 0 ] && STATUS='FAILED' && STEP='ASCII output file differ'
    done

    # Inspect output NetCDF files
    #---------------------------------------------------
    print_step 'Test last restart NetCDF files diff'
    diff_restart #2> /dev/null
    [ $? -ne 0 ] && STATUS='FAILED'

    # Get computation duration
    #---------------------------------------------------
    print_step 'Get real CPU time'
    get_cpu_time

    comments 'W A R N I N G'
fi

# End
#---------------------------------------------------
[ $STATUS == 'OK' ] && STEP='Code is reliable'
get_out $STATUS $STEP
