##############################################################################
# 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 :        convert_fqan
#
# DESCRIPTION : 
# This utility converts old style FQANs, to new style ones.
# The result is returned in the exported 'newfqan' variable and not in stdin,
# which makes possible the usage of DEBUG level yaimlog messages inside th utility.
#
# The exported 'newfqanvo' variable will contain the name of the VO the FQANs belongs to.
# If the input is a VO name (without leading '/') then this utility only converts 
# it to lowercase, and the 'newfqan' and 'newfqanvo' will be the same, i.e the VO name.
#
# AUTHORS :     Gergely.Debreczeni@cern.ch
#               
# YAIM MODULE:  glite-yaim-core
#
##############################################################################


convert_fqan() {
if [ ! -z "$1" ]; then

#If it is an fqan
 if ( echo "$1" | egrep -q '/'); then
  newstyle=`echo "$1" | sed -e 's|^/VO=[^/]*/GROUP=||' | sed -e 's/ROLE/Role/g'`
  newstyle=${newstyle//\/Capability=NULL/}
  newstyle=${newstyle//\/Role=NULL/}
  newstyle="/$newstyle"
  newstyle=${newstyle//\/\//\/}
  export newfqan="$newstyle"
  export newfqanvo=`echo "${newfqan}" | cut -d "/" -f 2`

# if it is a simple VO name
 else
  export newfqan=`echo "$1" | tr '[:upper:]' '[:lower:]'`
  export newfqanvo=${newfqan}
 fi
 yaimlog DEBUG "Converted: $1 --> $newfqan"
fi
}
