#!/bin/ksh

#-----------------------------------
function MBG_Initialize
{ 
    IGCM_debug_PushStack "MBG_Initialize"

# Local function to find namelists parameters
supergrep () {
    grep "^ *$1 *=" $2 | sed -e "s% *\!.*%%"
}

# Local function to find parameters in LMDZ .def files

lmdzgrep () {
    grep $1 $2 |grep -v "#" | awk "-F=" '{print $2}'
}

    ##--Variables used by MBG --

    # ln_rsttr  boolean term for tracer model restart (true or false)
    # nrsttr    control of the time step (0, 1 or 2)
    
    ##--Variables used by PISCES --
 
    NAMELIST_TOP_CFG=${SUBMIT_DIR}/PARAM/namelist_top_${RESOL_OCE}_cfg
    NAMELIST_PISCES_CFG=${SUBMIT_DIR}/PARAM/namelist_pisces_${RESOL_OCE}_cfg

    PAT_TOP_LRSTTR=$( supergrep ln_rsttr          ${NAMELIST_TOP_CFG} )
    PAT_TOP_NRSTTR=$( supergrep nrsttr            ${NAMELIST_TOP_CFG} )
    PAT_PIS_ATCCO2=$( supergrep atcco2            ${NAMELIST_PISCES_CFG} )
    PAT_PIS_LRIVER=$( supergrep ln_river          ${NAMELIST_PISCES_CFG} )

    IGCM_debug_PopStack "MBG_Initialize"
}

#-----------------------------------
function MBG_Update
{
    IGCM_debug_PushStack "MBG_Update"

    ##-- Restart configuration
    if ( [ "${CumulPeriod}" -eq 1 ] && [ "${config_MBG_Restart}" = "n" ] ) ; then

        #echo "NO RESTART FOR TOP"
        TOP_LRSTTR=.FALSE.
        TOP_NRSTTR=0

        PISCES_LRIVER=.FALSE.

    elif ( [ "${CumulPeriod}" -eq 1 ] && [ "${config_MBG_Restart}" = "y" ] ) ; then

        #echo "RESTART TOP"
        TOP_LRSTTR=.TRUE.
        TOP_NRSTTR=0

        PISCES_LRIVER=.TRUE.

    else

        #echo "RESTART TOP"
        TOP_LRSTTR=.TRUE.
        TOP_NRSTTR=2

        PISCES_LRIVER=.TRUE.

    fi

    ##-- Enable dbio output file
    DBIO_ENABLE=".FALSE." 
    eval pisces_DBIO_ENABLE=\${pisces_UserChoices_DBIO_ENABLE} > /dev/null 2>&1
    if [ "${pisces_DBIO_ENABLE}" = "y" ]; then
        DBIO_ENABLE=".TRUE."
    fi

    # update iodef.xml

    echo 'Informations into iodef.xml : DBIO_ENABLE'
    echo ${DBIO_ENABLE}

    sed -e "s/<DBIO_ENABLE>/${DBIO_ENABLE}/" \
        iodef.xml > iodef.xml.tmp

    IGCM_sys_Mv iodef.xml.tmp iodef.xml

    ##-- CO2 forcing : update atcco2 value in namelist_pisces_cfg
    if [ "${pisces_UserChoices_CARBON_CYCLE}" = "y" ]; then
       	# Special case for carbon cycle
	# Activate this case by putting CARBON_CYCLE=y in pisces.card section UserChoices
	echo 'Update atcco2 in namelist_pisces_cfg with value from file co2.log'
	PathCO2log=${SUBMIT_DIR}
	PreviousLinelog=$( tail -1 ${PathCO2log}/co2.log )
	IPCC_PIS_CO2=$( echo "${PreviousLinelog[*]}" | gawk -F '|' '{gsub(" ",""); print $4}' )
	sed -e "s%${PAT_PIS_ATCCO2}%          atcco2=${IPCC_PIS_CO2}%"             \
	    namelist_pisces_cfg > namelist_pisces_cfg.tmp
	IGCM_sys_Mv namelist_pisces_cfg.tmp namelist_pisces_cfg
    else
	# Take same CO2 as for LMDZ, in parameter file config.def.
	# ATM_Update is done before MBG_Update and therefor config.def file contains the correct value for CO2.
	echo 'Update atcco2 in namelist_pisces_cfg with same value as for LMDZ'
	LMDZ_CO2=$( lmdzgrep co2_ppm config.def )
	sed -e "s%${PAT_PIS_ATCCO2}%          atcco2=${LMDZ_CO2}%"             \
	    namelist_pisces_cfg > namelist_pisces_cfg.tmp
	IGCM_sys_Mv namelist_pisces_cfg.tmp namelist_pisces_cfg
    fi

    ##-- Update namelist_top_cfg and namelist_pisces_cfg

    sed -e "s%${PAT_TOP_LRSTTR}%          ln_rsttr=${TOP_LRSTTR}%"                   \
        -e "s%${PAT_TOP_NRSTTR}%          nrsttr=${TOP_NRSTTR}%"                   \
        namelist_top_cfg > namelist_top_cfg.tmp

    IGCM_sys_Mv namelist_top_cfg.tmp namelist_top_cfg

    sed -e "s%${PAT_PIS_LRIVER}%          ln_river=${PISCES_LRIVER}%"                \
        namelist_pisces_cfg > namelist_pisces_cfg.tmp

    IGCM_sys_Mv namelist_pisces_cfg.tmp namelist_pisces_cfg


    IGCM_debug_PopStack "MBG_Update"
}

#-----------------------------------
function MBG_Finalize
{
    IGCM_debug_PushStack "MBG_Finalize"

    echo FINALIZE MBG !!!

    IGCM_debug_PopStack "MBG_Finalize"
}

