##############################################################################
# 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 :        vo_param functions
#
# DESCRIPTION : These functions allow to set/get/initialize VO related parameters
#
# MAINTAINER :  yaim-contact@cern.ch
#
# NOTES :       VO definition:
#               saved in vo.d directory,
#               1 file per VO. File name is the VO name in lowercase,
#               parameter names are in UPPER case
#
# YAIM MODULE:  glite-yaim-node-type
#                 
##############################################################################

######################################################
# vo_param_init <SITEINFO_DIR>
#	Initialize temporary VO information storage
######################################################

function vo_param_init() {
    if [ -d /tmp/.vo.d ] ; then
    	rm -rf /tmp/.vo.d
    fi
        # create directory to store transitive VO values
	mkdir -p /tmp/.vo.d
	# copy VO definitions if they exist
	if [ -d $1/vo.d ] ; then
		cp -a $1/vo.d/* /tmp/.vo.d
	fi
}

#################################################
# Close VO manipulation
#	Remove temporary VO information storage
#################################################

function vo_param_close() {
	rm -rf /tmp/.vo.d
}

################################################################
# get_vo_param <VO> <param>
#	<VO> is the vo name as defined in VOS 
#	<param> is the parameter we are looking for
################################################################

function get_vo_param() {
    VO=`echo $1 | tr '[:upper:]' '[:lower:]'`           # make sure the VO name is in lower case, as it should be in VOS variable.
    unset return_val
    if [ -f /tmp/.vo.d/${VO} ] ; then
        return_val=$(
            source /tmp/.vo.d/${VO}
            eval echo '$'`echo $2`
        )
    else
        VO=`echo $VO | sed -e 's/-/_/g' -e 's/\./_/g'`  # Transform DNS-like VO names into "_" in case they are defined in site-info.def
        return_val=$(
	    eval echo '$'VO_`echo $VO | tr '[:lower:]' '[:upper:]'`_$2
	)
    fi
    echo $return_val
}

################################################################
# set_vo_param <VO> <param>
#       <VO> is the vo name as defined in VOS
#       <param> is the parameter we are looking for
################################################################

function set_vo_param(){
    VO=`echo $1 | tr '[:upper:]' '[:lower:]'`           # make sure the VO name is in lower case, as it should be in VOS variable.
    if [ -f /tmp/.vo.d/${VO} ] ; then
        grep -v ^$2= /tmp/.vo.d/${VO} > /tmp/.vo.d/${VO}~
        echo $2=$3 >> /tmp/.vo.d/${VO}~
        mv /tmp/.vo.d/${VO}~ /tmp/.vo.d/${VO}
    else
        VO=`echo $VO | sed -e 's/-/_/g' -e 's/\./_/g'`  # Transform DNS-like VO names into "_" in case they are defined in site-info.def
        eval VO_`echo $VO | tr '[:lower:]' '[:upper:]'`_$2=$3
    fi
}

