#!/bin/bash


cd $( dirname $0 )

## Set defaults
##---------------------------------------------------
NEMO_TRUS=$( pwd )
DEBUG=0; PUBLISH=0
I_MODULE=0      ## No 'module' command by default
ST='FAILED'     ## 'FAILED'            ""   " "  

## Get options for replacing some initials settings
##---------------------------------------------------
while [ $# -ne 0 ]; do
    case $1 in
	-a|--archive) NEMO_TARF=$2         ; shift 2;; -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) NEMO_HPCC=$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   ) NEMO_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 "${NEMO_HPCC}" && ! -z "${NEMO_USER}" ]]; then

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

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

. ./trusting_func.sh

## Summary of trusting test context
##---------------------------------------------------
printf "\
NEMO Trusting test:\n\
-------------------\n\
Testing ${TEST_CONF} based on ${REFE_CONF} from ${NEMO_BRAN}\n\
Configuration files: ${NEMO_USER}.cfg & ${NEMO_HPCC}.cfg\n"

## Making timestamped directory with messenger files
##---------------------------------------------------
print_step 'Time-stamp generation & testing directory creation'
mkdir -p ${TEST_DIR} ${REFE_DIR}
echo ${TEST_DIR} && cd ${TEST_DIR}
init_files && get_date

## SVN action on XIOS & NEMO essentials directories
##---------------------------------------------------
print_step 'SVN command on NEMO directories:'
get_nemo_rev

## Check softwares versions (source arch environment)
##---------------------------------------------------
print_step 'Get softwares releases'
[ -e ${ARCH_ENV} ] && . ${ARCH_ENV} > /dev/null
(( $? == 0  )) && export I_MODULE=1
get_soft_rel
cat model.log

## XIOS compiling
##---------------------------------------------------
print_step 'Compile XIOS'
cd ${DIR_XIOS}
full=''; [ $DEBUG -eq 0 ] && full='--full'
./make_xios $full --arch ${NEMO_HPCC} --job $NPROC >& /dev/null
[ ! -e ${DIR_XIOS}/lib/libxios.a ] && get_out 1

## NEMO config compiled from scratch
##---------------------------------------------------
print_step "Compile ${REFE_CONF} configuration"
cd ${NEMO_CONF}
[[ -d ${TEST_CONF} && $DEBUG -eq 0 ]] && ./makenemo -n ${TEST_CONF} clean >  /dev/null
./makenemo -n ${TEST_CONF} -r ${REFE_CONF} -m ${NEMO_HPCC} -j $NPROC      >& /dev/null
[ ! -e ${TEST_CONF}/BLD/bin/nemo.exe ] && get_out 2

## Get namelists, xml & forcing files for running
##---------------------------------------------------
print_step 'Set job (copying & extracting input files)'
cd ${TEST_DIR}
find ${NEMO_CONF}/${TEST_CONF}/EXP00 -regex '.*\(cfg\|opa\|ref\|xml\)' -exec cp {} . \;
get_inputs
(( $? != 0 )) && get_out 3
[ $( find . -name '*.gz' -print -quit ) ] && gunzip *.gz

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

## Job submission & computing
##---------------------------------------------------
print_step 'Submit job'
JOB_ID=$( eval ${JOB_SUBM} )
(( $? != 0 )) && get_out 4
print_step 'Pending job'
job_pending
print_step 'Job finished'

## Job state
##---------------------------------------------------
print_step 'Test job state'
if   [[ ! -e time.step || $( grep -q 'E R R O R' ocean.output ) ]]; then
    comments 'E R R O R'
    get_out 5
else
    ## Get time computation
    ##---------------------------------------------------
    print_step 'Get real CPU time'
    get_cpu_time
fi

ST='OK' ## 'OK' by default

## Inspect output text files
##---------------------------------------------------
print_step 'Test ASCII output files diff'
diff_results
[ "$ST" == 'FAILED' ] && get_out 8

## Inspect output NetCDF files
##---------------------------------------------------
print_step 'Test last restart NetCDF files diff'
diff_restart
[ "$ST" == 'FAILED' ] && get_out 10

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

## End
##---------------------------------------------------
get_out 'Code is reliable'
