#!/bin/bash


cd $( dirname $0 )

## Set defaults
##---------------------------------------------------
NEMO_TRUS=$PWD
DEBUG=0; PUBLISH=0
IMOD=0; ST='FAILED'  ## No modules system & 'FAILED' status
SVN_CMD='svn status' ## No update on directories
trus_help='trusting_help.txt'


## Get options for replacing 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 ${help_file}; 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 ${help_file}; exit  1;;
    esac

done

## DEBUG options to speed up & expand verbosity of compilation
xios_full='--full'; stderr_redir='>&'
[ $DEBUG -eq 1 ] && xios_full='' && stderr_redir='>'

## If -v|--version option has been set, modify default SVN action on directories
if   [ $( echo ${NEMO_VERS} | grep  "HEAD\|up\|update"                     ) ]; then
    SVN_CMD='svn update -r HEAD'
elif [ $( echo ${NEMO_VERS} | grep -o '{[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}}' ) ]; then
    SVN_CMD='svn update -r '$( echo ${NEMO_VERS} | grep -o '{[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}}' )
elif [ $( echo ${NEMO_VERS} | grep -o '[0-9]*'                             ) ]; then
    SVN_CMD='svn update -r '$( echo ${NEMO_VERS} | grep -o '[0-9]*'                             )
fi


## 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}/${help_file}
    exit 1
fi

. ./trusting_func.sh


## Display contextual summary of trusting test
##---------------------------------------------------
echo 'NEMO Trusting test:'
echo '-------------------'
echo 'Testing '${TEST_CONF}' based on '${REFE_CONF}' from '${NEMO_BRAN}
echo 'NEMO installation: '${NEMO_USER}
echo 'Computing architecture: '${NEMO_HPCC}
echo 'Trusting SVN version: '$( svn info | awk '(NR == 9) {print $NF}' )


## Make timestamped directory with messenger files
##---------------------------------------------------
print_step 'Timestamped testing directory'
mkdir -p ${TEST_DIR} ${REFE_DIR}
cd ${TEST_DIR} && echo ${TEST_DIR}
init_files
get_date


## Get SVN revision on XIOS & NEMO essentials directories
##---------------------------------------------------
print_step "SVN action on NEMO directories: ${SVN_CMD}"
get_nemo_rev


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


## XIOS compilation from scratch
##---------------------------------------------------
print_step 'Compile XIOS'
cd ${DIR_XIOS}
eval ./make_xios ${xios_full} --arch ${NEMO_HPCC} --job $NPROC \
    ${stderr_redir} /dev/null
[ ! -e lib/libxios.a ] && get_out 1 || echo 'Success'


## NEMO compilation from scratch
##---------------------------------------------------
print_step "Compile ${REFE_CONF} configuration"
cd ${NEMO_CONF}
[[ -d ${TEST_CONF} && $DEBUG -eq 0 ]] && ./makenemo -n ${TEST_CONF} clean_config \
    > /dev/null <<EOF
y
EOF

eval ./makenemo -n ${TEST_CONF} -r ${REFE_CONF} -m ${NEMO_HPCC} -j $NPROC \
                   ${KEYS_ADD} ${KEYS_DEL}                                \
    ${stderr_redir} /dev/null
[ ! -e ${TEST_CONF}/BLD/bin/nemo.exe ] && get_out 2 || echo 'Success'


## Get all inputs for running
##---------------------------------------------------
print_step 'Set job (copying or extracting inputs)'
cd ${TEST_DIR}
cp ${NEMO_CONF}/${TEST_CONF}/cpp_* .
find ${NEMO_CONF}/${TEST_CONF}/EXP00 -regex '.*\(_cfg\|.in\|opa\|_ref\|.xml\)' \
                                     -exec  cp {} . \;
get_inputs
[ $? -ne 0 ] && get_out 3 || echo 'Success'
[ $( find . -name '*.gz' -print -quit ) ] && find . -name '*.gz' -exec gunzip {} \;


## Check inputs
##---------------------------------------------------
print_step 'Compare inputs to benchmark files'
diff_inputs


## Job submission & computation
##---------------------------------------------------
print_step 'Submit job'
JOB_ID=$( eval ${JOB_SUBM} )
[ $? -ne 0 ] && get_out 4 || echo 'Success (job ID '${JOB_ID}')'
print_step 'Pending job'
job_pending
print_step 'Job finished'


## Check job state & get computing time if succeeded
##---------------------------------------------------
print_step 'Test job state'
[[ ! -e time.step || $( grep 'E R R O R' ocean.output ) ]] && get_out 5 || echo 'Success'
print_step 'Get real CPU time'
get_cpu_time


## Check outputs
##---------------------------------------------------
ST='OK' ## 'OK' by default
print_step 'Compare new outputs to benchmark files'
diff_results
print_step 'Compare new restarts to benchmark files'
diff_restart
[ $ST == 'FAILED' ] && get_out 8


## End, at least nothing has changed ;-)
##---------------------------------------------------
get_out 0
