##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2004.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
# NAME:      gridenv
#
# AUTHOR:    Gergely.Debreczeni@cern.ch
#
# DESCRIPTION: These functions have to be used by different YAIM functions.
# They are writing a file (${GRID_ENV_LOCATION}/grid-env.sh) which is to be 
# sourced by the users or by different service daemons to set up the grid environment.
# The file uses the functions defined in grid-env-funcs.sh which are safe againt
# double definitions ,i.e. no infintely growing PATH variables if an item is
# added several time.
#
# YAIM MODULE: glite-yaim-core
##############################################################################

function yaimgridenv_set () {
    if [ $# -ne 2 ]; then
       yaimlog DEBUG "Calling gridenv_set with the incorrect  number of parameters: $*"
       return 1
    fi
    yaimlog DEBUG "Setting environment variable $1, to value \"$2\"."
    echo "gridenv_set        " "\"$1\"" "\"$2\"" >> ${GRID_ENV_LOCATION}/grid-env.sh 
    eval export $1="\"$2\""
    yaimgridenv_unset $1
}

function yaimgridenv_setind () {
    if [ $# -ne 2 ]; then
       yaimlog DEBUG "Calling gridenv_setind with the incorrect  number of parameters: $*"
       return 1
    fi
    yaimlog DEBUG "Setting environment variable $1, to value \"$2\"."
    echo "gridenv_setind     " "\"$1\"" "\"$2\"" >> ${GRID_ENV_LOCATION}/grid-env.sh 
    eval export $1=\"\${$1:-$2}\"
}

function yaimgridenv_unset () {
    if [ $# -ne 1 ]; then
       yaimlog DEBUG "Calling gridenv_unset with the incorrect  number of parameters: $*"
       return 1
    fi
    yaimlog DEBUG "Unset environment variable $1."
    echo "gridenv_unset     " "\"$1\"" >> ${GRID_ENV_LOCATION}/grid-clean-env.sh 
}

function yaimgridpath_delete () {
    if [ $# -ne 2 ]; then
      yaimlog DEBUG "Calling gridpath_delete with the incorrect  number of parameters: $*"
      return 1
    fi
    yaimlog DEBUG "Deleting value \"$2\" from environment variable $1."
    echo "gridpath_delete     " "\"$1\"" "\"$2\"" >> ${GRID_ENV_LOCATION}/grid-clean-env.sh 
 }


function yaimgridpath_append () {
    if [ $# -ne 2 ]; then
       yaimlog DEBUG "Calling gridpath_append with the incorrect  number of parameters: $*"
       return 1
    fi
    yaimlog DEBUG "Appending value \"$2\" to environment variable $1."
    echo "gridpath_append     " "\"$1\"" "\"$2\"" >> ${GRID_ENV_LOCATION}/grid-env.sh 
    eval export $1=\${$1}":"$2
    yaimgridpath_delete $1 $2
}

function yaimgridpath_prepend () {
    if [ $# -ne 2 ]; then
       yaimlog DEBUG "Calling gridpath_prepend with the incorrect  number of parameters: $*"
       return 1
    fi
    yaimlog DEBUG "Appending value \"$2\" to environment variable $1."
    echo "gridpath_prepend    " "\"$1\"" "\"$2\"" >> ${GRID_ENV_LOCATION}/grid-env.sh 
    eval export $1=$2":"\${$1}
    yaimgridpath_delete $1 $2
}
