#!/bin/bash


# Set defaults
#---------------------------------------------------
NEMO_TRUS=$( pwd $( dirname $0 ) )
DEBUG=0; PUBLISH=0
STATUS='FAILED'; STEP='' # Only at end status will change to 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  ) EMAIL=$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}
date_str=$( date -ud ${DATESTR} +"%F %R %Z" )
printf "Date\n${date_str}\n" > mesg_01_date_${CFG_USER}_${CFG_ARCH}.txt

# SVN action on XIOS & NEMO essentials directories
#---------------------------------------------------
print_step 'Get NEMO code state / repository'
get_nemo_rev

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

# XIOS compiling
#---------------------------------------------------
print_step 'Compiling XIOS'
cd ${DIR_XIOS}
#./make_xios --full --arch $ARCH -job $NPROC #>& /dev/null
[ ! -e ${DIR_XIOS}/lib/libxios.a ] && get_out $STATUS $STEP

# 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 by copying ${CONF_REF} input files"
cd ${TEST_DIR}
find ${NEMO_CONF}/${TEST_CONF}/EXP00 -regex '.*\(cfg\|opa\|ref\|xml\)' -exec cp {} . \;
get_inputs
[ $? -ne 0 ] && get_out $STATUS $STEP
[ $( find . -name '*.gz' -print -quit ) ] && gunzip *.gz

# Check input files in all cases
#---------------------------------------------------
print_step 'Difference with standard input files list'
diff_inputs

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

# Eventual crash during computing
#---------------------------------------------------
print_step 'Test if job crashed'
if   [[ ! -e ocean.output || $( grep 'E R R O R' ocean.output ) ]]; then
    comments 'E R R O R'
    get_out $STATUS $STEP
fi

# Inspect output text files
#---------------------------------------------------
print_step 'Test ASCII output files diff'
diff_textfiles

# Inspect output NetCDF files
#---------------------------------------------------
print_step 'Test last restart NetCDF files diff'
diff_restart

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

# Get comments (ocean.output & diff model.log)
#---------------------------------------------------
comments 'W A R N I N G'

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