#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2008.
# 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 :        config_gip_lfc
#
# DESCRIPTION : This function configures the GIP for LFC
#
# AUTHORS :     Akos.Frohner@cern.ch
#
# YAIM MODULE:  glite-yaim-lfc
#
##############################################################################

function config_gip_lfc_check() {
 requires $1 VOS SITE_NAME LFC_HOST
}

function config_gip_lfc() {

    GLITE_LOCATION_ETC=${GLITE_LOCATION_ETC:-$GLITE_LOCATION/etc}

    # overwrite any existing static produced data
    outfile=${GLITE_LOCATION_VAR}/tmp/gip/glite-info-static-lfc.conf
    if [ -f $outfile ] ; then
        cat /dev/null > $outfile
        echo "# Obsolete - LFC now uses a provider script" >> $outfile
    fi

    if [ "$LFC_LOCAL" ] || [ "$LFC_CENTRAL" ]; then
        lfc_local=$LFC_LOCAL
    else
        # populate lfc_local with the VOS which are not set to be central
        unset lfc_local
        for i in $VOS; do
            if ( ! echo $LFC_CENTRAL | grep -qw $i ); then
                lfc_local="$lfc_local $i"
            fi
        done
    fi

    unset vo_list
    if [ "$lfc_local" ]; then
       vo_list="$vo_list --local \"$lfc_local\""
    fi
    if [ "$LFC_CENTRAL" ]; then
       vo_list="$vo_list --central \"$LFC_CENTRAL\""
    fi
    # Install provider script
    cat << EOF  > ${GLITE_LOCATION_ETC}/gip/provider/glite-lfc-provider
#! /bin/sh
${LCG_LOCATION}/libexec/lcg-info-provider-lfc --site ${SITE_NAME} ${vo_list} ${LFC_HOST_ALIAS:+--alias} ${LFC_HOST_ALIAS}
EOF

    chmod +x ${GLITE_LOCATION_ETC}/gip/provider/glite-lfc-provider

  return 0
}

