#!/bin/ksh

function XIOS_sed_xml
{
# Usage : XIOS_sed_xml xml_file attribute value 
#         In file xml_file modify at the line containing id="attribute" the attribute value ">value<" 
    IGCM_debug_PushStack "XIOS_sed_xml"
    # Test if the fichier exist
    if [ ! -f ${1} ] ; then
	echo "WARNING : ${1} file does not exist. Following will not be done : XIOS_sed_xml : ${1} ${2} ${3}"
        IGCM_debug_PopStack "XIOS_sed_xml"
	return
    fi
    sed -e "s/\(<[^\"]*\"${2}\".*>\)\([^<]*\)\(<[^>]*\)/\1${3}\3/" ${1} > ${1}.tmp
    RET=$?
    echo "XIOS_sed_xml" : ${1} ${2} ${3}
    \mv ${1}.tmp ${1}
    IGCM_debug_PopStack "XIOS_sed_xml"
    return $RET
}

#-----------------------------------
function IOS_Initialize
{ 
    IGCM_debug_PushStack "IOS_Initialize"

    echo INITIALIZE IOS !!!

    IGCM_debug_PopStack "IOS_Initialize"
}

#-----------------------------------
function IOS_Update
{
    IGCM_debug_PushStack "IOS_Update"

    echo UPDATE IOS !!!
# Activate server mode
    XIOS_sed_xml iodef.xml using_server true
# If lmdz_UserChoices_XIOS=y add LMDZ as component id :
    if [ X${lmdz_UserChoices_XIOS}  = Xy ] ; then
	XIOS_sed_xml iodef.xml oasis_codes_id LMDZ,oceanx
    fi 
    if [ -f namcouple ] ; then
	sed -e "s/2  LMDZ oceanx/3 LMDZ oceanx ${config_Executable_IOS[1]}/" namcouple > namcouple.tmp 
	IGCM_sys_Mv namcouple.tmp namcouple
    fi

    IGCM_debug_PopStack "IOS_Update"
}

#-----------------------------------
function IOS_Finalize
{
    IGCM_debug_PushStack "IOS_Finalize"

    echo FINALIZE IOS !!!

    IGCM_debug_PopStack "IOS_Finalize"
}
