#!/bin/bash


## Defaults
##--------------------------------------------------------------------------------

[ -z "${TRUST_SVN_BRANCH}" ] && TRUST_SVN_BRANCH='trunk'

## Ref. cfg. unset or empty (mandatory)
if [[ -z "${TRUST_CFG_REF}" ]]; then

    ## Default cfg from trunk excepting last stable
    TRUST_CFG_REF='ORCA2_LIM3_PISCES'

    if [[ ${TRUST_SVN_BRANCH} && ${TRUST_SVN_BRANCH} == 'nemo_v3_6_STABLE' ]]; then
	TRUST_CFG_REF='ORCA2_LIM_PISCES'
    fi

fi


## Source user & (super)computer configurations
##--------------------------------------------------------------------------------
. ./cfg/${TRUST_MAIN_USER}.cfg
. ./cfg/${TRUST_MAIN_HPCC}.cfg


## Checking and export environment variables
##--------------------------------------------------------------------------------

## Trusting installation
## ---------------------

[ -z "${TRUST_DIR_WORK}"    ] &&    TRUST_DIR_WORK=${TRUST_MAIN_DIR}/../CONFIG
[ -z "${TRUST_DIR_SCRATCH}" ] && TRUST_DIR_SCRATCH=${TRUST_MAIN_DIR}
export TRUST_DIR_WORK TRUST_DIR_SCRATCH TRUST_DIR_STORE


## Branch checkout (./install_new_branch.sh)
##------------------------------------------

export TRUST_SVN_REPO='https://forge.ipsl.jussieu.fr/nemo/svn'
export TRUST_SVN_CO=( 'ARCH           CONFIG          NEMO              
                       EXTERNAL/AGRIF EXTERNAL/fcm    EXTERNAL/IOIPSL   
                       TOOLS/COMPILE  TOOLS/maketools TOOLS/REBUILD_NEMO
                       TRUST                                            ' )
export TRUST_SVN_UP=( 'ARCH           CONFIG          NEMO           
                       EXTERNAL/AGRIF EXTERNAL/fcm    EXTERNAL/IOIPSL
                       TOOLS/COMPILE                                 ' )

export TRUST_SVN_BRANCH
export TRUST_SVN_NEMOGCM=${TRUST_DIR_WORK}/${TRUST_SVN_BRANCH}/NEMOGCM


## NEMO Configuration
##-------------------

## Forcing archive unset
if [[ ! ${TRUST_IO_FORC_TAR} && ${TRUST_IO_FORC_TAR-_} ]]; then

    ## Forcing directory set
    if [ ${TRUST_IO_FORC_PATH} ]; then
	## Default forcing archive from trunk
	TRUST_IO_FORC_TAR='ORCA2_LIM_nemo_v3.7.tar INPUTS_PISCES_v3.6st.tar'
	[ ${TRUST_CFG_REF} == 'AMM12' ] && TRUST_IO_FORC_TAR='AMM12_v3.7.tar'

	if [ ${TRUST_SVN_BRANCH} == 'nemo_v3_6_STABLE' ]; then
	    TRUST_IO_FORC_TAR='ORCA2_LIM_nemo_v3.6.tar'

	    if   [ ${TRUST_CFG_REF} == 'ORCA1_LIM3_PISCES' ]; then
		TRUST_IO_FORC_TAR='INPUTS_ORCA1_LIM3_PISCES_V10.tar'
	    elif [ ${TRUST_CFG_REF} == 'AMM12'             ]; then
		TRUST_IO_FORC_TAR='AMM12_v3.6.tar'
	    fi

	fi

    fi

fi

[ -z "${TRUST_CFG_NEW}" ] && TRUST_CFG_NEW=${TRUST_CFG_REF}'_trust'
export TRUST_CFG_NEW TRUST_CFG_REF
export TRUST_CFG_KEY_ADD TRUST_CFG_KEY_DEL


## Inputs/Outputs
## --------------

[ -z "${TRUST_IO_XIOS}" ] && get_out A
export TRUST_IO_XIOS
export TRUST_IO_FORC_PATH TRUST_IO_FORC_TAR
export TRUST_IO_CDO


## Compilation & Running
##----------------------

if [ -z "${TRUST_JOB_ENV}" ]; then
    TRUST_JOB_ENV=${TRUST_IO_XIOS}/arch/arch-${TRUST_MAIN_HPCC}.env
fi

export TRUST_JOB_ENV

[ -z "${TRUST_COMPILE_NPROC}" ] && TRUST_COMPILE_NPROC='4'
export TRUST_COMPILE_NPROC

export TRUST_COMPILE_FORTRAN TRUST_COMPILE_MPI TRUST_COMPILE_NETCDF

if [ -z "${TRUST_JOB_SCRIPT}" ]; then
    TRUST_JOB_SCRIPT=${TRUST_MAIN_DIR}/batch/${TRUST_MAIN_HPCC}.sh || get_out A
fi

export TRUST_JOB_SCRIPT 


if [[ -z "${TRUST_JOB_SUBMIT}" || -z "${TRUST_JOB_STATE}" \
                               || -z "${TRUST_JOB_KILL}"    ]]; then
    get_out A
fi

export TRUST_JOB_SUBMIT TRUST_JOB_STATE TRUST_JOB_KILL

[ -z "${TRUST_JOB_TIMEOUT}" ] && TRUST_JOB_TIMEOUT='10800'
export TRUST_JOB_TIMEOUT

export TRUST_JOB_INFO TRUST_JOB_TIME TRUST_JOB_RAM_P TRUST_JOB_RAM_V


## Trusting test
##--------------

export TRUST_TEST_NAME=${TRUST_MAIN_USER}'_'${TRUST_MAIN_HPCC}
export TRUST_TEST_DATE=$( date -u +"%Y%m%d%Z%H%M" )

export TRUST_TEST_SUMMARY=trusting_${TRUST_TEST_DATE}_${TRUST_TEST_NAME}.txt
export  TRUST_TEST_BACKUP=trusting_${TRUST_TEST_DATE}_${TRUST_TEST_NAME}.tgz

##       TRUST_TEST_DIR unset
if [[ ! ${TRUST_TEST_DIR}       && ${TRUST_TEST_DIR-_}       ]]; then
        TRUST_TEST_DIR=${TRUST_DIR_SCRATCH}/${TRUST_CFG_REF}/${TRUST_SVN_BRANCH}
        TRUST_TEST_DIR+=/${TRUST_TEST_DATE}
    export TRUST_TEST_DIR
fi

## TRUST_TEST_BENCHMARK unset
if [[ ! ${TRUST_TEST_BENCHMARK} && ${TRUST_TEST_BENCHMARK-_} ]]; then
    TRUST_TEST_BENCHMARK=${TRUST_DIR_STORE}/${TRUST_CFG_REF}/${TRUST_SVN_BRANCH}
    export TRUST_TEST_BENCHMARK
fi

export TRUST_TEST_LOG=${TRUST_DIR_BENCHMARK}/trusting_${TRUST_TEST_NAME}.txt

export TRUST_TEST_MAILING
