#!/bin/bash


# Set defaults
DEBUG=0; PUBLISH=0; status='OK'
NEMO_BRAN=trunk; # dev_r5092_CNRS_SETTE dev_v3_4_STABLE_2012
CONF_REFE=ORCA2_LIM_PISCES
CONF_TEST=${CONF_REFE}_trust
#COMPILER=ifort gfortran f95 f90 pgfortran pgf95 pgf90 mpifort mpif90 mpiifort
#MPI=bullxmpi openmpi mpich intel mpxlf


while [ $# -ne 0 ]; do
    case $1 in
        -b|--branch    ) NEMO_BRAN=$2         ; shift 2;;
        -d|--debug     ) set -vx; DEBUG=1     ; shift  ;;
        -h|--help|-help) cat trusting_help.txt; exit   ;;
        -m|--machine   ) ARCH=$2              ; shift 2;;
        -n|--newconf   ) CONF_TEST=$2         ; shift 2;;
        -r|--refconf   ) CONF_REFE=$2         ; shift 2;;
        -p|--publish   ) PUBLISH=1            ; shift  ;;
	*)                                      break  ;;
    esac
done
export CONF_REFE CONF_TEST DEBUG NEMO_BRAN PUBLISH

[ $# -ne 1 ] && { ./trusting.sh -h && exit; }
# suspend debug mode (too verbose)
#[ $debug -eq 1 ] && set +vx

# Initialization
#=============================================

. trusting.env
echo ${DIR_TEST}
mkdir -p ${DIR_TEST} ${DIR_REFE}
cd ${DIR_TEST}
datestr1=$( LC_ALL=C date -d"$DATESTR" +%Y-%m-%dT%H:%M )
printf "Date\n%s\n"       $datestr1 > mesg_01_date.txt

printf "Status\nFAILED\n"           > mesg_02_status.txt # Only at end status will change to OK

. trusting_func.sh
#=============================================


# 
#=============================================
for rel in $COMPILER $MPI netcdf; do
    printf "Step......................\nGet $rel release\n" > mesg_03_step.txt
    arch_rel=$( echo $LOADEDMODULES | sed "s#.*:$rel/\([^:]*\).*#\1#" )
    [ -z "${arch_rel}" ] && arch_rel=$( echo $PATH | sed "s#.*$rel/\([^/]*\).*#\1#" )
    [[ $rel == $COMPILER && -z "$FC" ]] && arch_rel=$( $FC --version | grep -m1 -o ' [0-9.]* ' )
    echo $rel ${arch_rel} >> model.log
done

sed -n 1p model.log | sed 'i\Fortran compiler' > mesg_04_curiec.txt
sed -n 2p model.log | sed 'i\MPI libraries   ' > mesg_05_mpicurie.txt
sed -n 3p model.log | sed 'i\NetCDF libraries' > mesg_06_netcdf_curie.txt


#---------------------------------------------
printf "Step......................\nCheckout XIOS, FCM-make(/EXTERNAL/fcm), NEMO engines (/NEMO) & configuration (/CONFIG)\n" > mesg_03_step.txt
rev=0
for dir in ${DIR_XIOS} ${NEMO_ARCH} ${NEMO_CONF} ${NEMO_EFCM} ${NEMO_ENGI} ${NEMO_IPSL} ${NEMO_TCMP}; do
    svn up $dir
    rev_loc=$( svn info $dir | awk '(NR == 9) {print $NF}' )
    [ $dir == ${DIR_XIOS} ] && echo 'XIOS '${rev_loc} >> model.log && continue
    (( ${rev_loc} >= $rev )) && rev=${rev_loc}
done
echo 'NEMO '$rev >> model.log


#---------------------------------------------
printf "Step......................\nCompilation NEMO configuration %s\n" ${CONF_REF} > mesg_03_step.txt
cd ${NEMO_CONF}
#[ -d ${CONF_TEST} ] && ./makenemo -n ${CONF_TEST} clean
./makenemo -n ${CONF_TEST} -r ${CONF_REFE} -m $ARCH -j 16 #> /dev/null 2>&1
[ ! -e ${CONF_TEST}/BLD/bin/nemo.exe ] && getout


#---------------------------------------------
printf "Step......................\nPreparing job\n" > mesg_03_step.txt
cd ${DIR_TEST}
find ${NEMO_CONF}/${CONF_TEST}/EXP00 -regex '.*\(cfg\|opa\|ref\|xml\)' -exec cp {} . \;
cp ${NEMO_FORC}/${FORC_TAR} . && tar -tvf ${FORC_TAR} > inputs_list.txt && tar -vxf ${FORC_TAR}
[ $? -ne 0 ] && getout
gunzip *.gz


#---------------------------------------------
printf "Step......................\nRunning\n"       > mesg_03_step.txt
#${JOB_SUB} ${DIR_TEST}/../../batch.sh
job_num=$( ${JOB_SUB} ${DIR_TEST}/../../batch.sh | awk '{print $NF}')
[ $? -ne 0 ] && getout

output_test=ocean.output   ; output_refe=${DIR_REFE}/ocean.output
solver_test=solver.stat    ; solver_refe=${DIR_REFE}/solver.stat
tracer_test=tracer.stat    ; tracer_refe=${DIR_REFE}/tracer.stat
inputs_test=inputs_list.txt; inputs_refe=${DIR_REFE}/inputs_list.txt

#time_elapsed=0; time_limit=10800; time_increment=300
time_elapsed=0; time_limit=1080; time_increment=30
sleep ${time_increment}
while [[ $( ${JOB_LIST} | grep $job_num ) && ${time_elapsed} -lt ${time_limit} ]] ; do
#while [[ ! -f ${solver_test} && ${time_elapsed} -lt ${time_limit} ]] ; do
    printf "\n####################################################" >> ${DIR_TEST}/mpp.log
    ${JOB_VIEW} $job_num >> mpp.log
#   ${JOB_VIEW} $id >> mpp.log
    sleep ${time_increment}
    let time_elapsed+=time_increment
done


#---------------------------------------------
printf "Step......................\nTest if solver exists\n"   > mesg_03_step.txt
if [ ! -f ${solver_test} ]; then
    grep 'E R R O R' ocean.output    >> mesg_11_comments.txt
    sed 's/^/Time step :/' time.step >> mesg_11_comments.txt
    getout
fi


#---------------------------------------------
printf "Step......................\nTest solvers difference\n" > mesg_03_step.txt
diff ${output_refe} ${output_test}
[ $? -ne 0 ] && status='FAILED'
diff ${solver_refe} ${solver_test}
[ $? -ne 0 ] && status='FAILED'
diff ${tracer_refe} ${tracer_test}
[ $? -ne 0 ] && status='FAILED'


#---------------------------------------------
printf "Step......................\nTest outputs difference\n" > mesg_03_step.txt
#diff_trusting 2>/dev/null
#[ $? -ne 0 ] && status='FAILED'


#---------------------------------------------
printf "Comments\n\n"              >  mesg_11_comments.txt
grep 'W A R N I N G:' ocean.output >> mesg_11_comments.txt


#---------------------------------------------
if [ $status != 'FAILED' ]; then
    printf "Step......................\nGet Real CPU time\n" > mesg_03_step.txt
    real_cpu_time=$( ${JOB_VIEW} $job_num | awk 'END{print $9}')
    [ $? -ne 0 ] && getout
    printf "Real CPU time\n%s\n" ${real_cpu_time} > mesg_08_realcputime.txt
else
    printf "Step......................\nTest InputFiles difference\n" > mesg_03_step.txt
    diff ${inputs_refe} ${inputs_test}
    { [ $? -ne 0 ] && printf "InputFiles\n%s\n" "Different" > mesg_09_inputfiles.txt } || printf "InputFiles\n%s\n" "Same" > mesg_09_inputfiles.txt
    getout
fi


#---------------------------------------------
printf "Status\nOK\n" > mesg_02_status.txt
printf "Step......................\nCode is reliable\n" > mesg_03_step.txt

#---------------------------------------------
getout
