#!/bin/bash
######################################################
# Author : Simona Flavoni for NEMO
# Contact : sflod@locean-ipsl.upmc.fr
#
# Some scripts called by sette.sh
# all_functions.sh   : all functions used by sette.sh  
######################################################
#set -x
set -o posix
#set -u
#set -e
#+
#
# ================
# all_functions.sh
# ================
#
# ----------------------------------------------
# Set of functions used by sette.sh (NEMO tests) 
# ----------------------------------------------
#
# SYNOPSIS
# ========
#
# ::
#
#  $ ./set_namelist.sh INPUT_NAMELIST VARIABLE VALUE 
#
#
# DESCRIPTION
# ===========
#
# function superegrep
# input variable value
#
# function set_namelist
# input namelist_name variable value
# output namelist
#
# EXAMPLES
# ========
#
# ::
#
#  $ ./set_namelist.sh namelist          nn_itend        75
#  $ ./set_namelist.sh namelist_ice_lim2 cn_icerst_in  \"00101231_restart_icemod\"
#
#
# TODO
# ====
#
# option debug
#
#
# EVOLUTIONS
# ==========
#
# $Id$
#
#   * creation
#-
#


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

usage=" Usage : set_namelist input_namelist variable_name value"
usage=" if value is a string ths is neede syntax : ./set_namelist namelist_name var_name \"new_value\" "

# function to set namelists parameters
set_namelist () {
	minargcount=3
		if [ ${#} -lt ${minargcount} ]
		then
		echo "not enought arguments"
		echo "${usage}"
		exit 1
		fi
	unset minargcount
	VAR_NAME=$( supergrep $2 ${INPUT_DIR}/$1 )
     	sed -e "s/${VAR_NAME}/$2=$3/"  ${INPUT_DIR}/$1 > ${INPUT_DIR}/$1.tmp
     	mv ${INPUT_DIR}/$1.tmp ${INPUT_DIR}/$1
}
