##############################################################################
# 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 :        requires
#
# DESCRIPTION : This function checks that the list of variables passed as parameters
#               are defined in site-info.def or the services/ files.
#
# AUTHORS :     yaim-contact@cern.ch
#
# NOTES : 
#
# YAIM MODULE:  glite-yaim-core
#                 
##############################################################################


function requires () {

myquitonerror="yes"
if [ "x$1" = "xdonotquitonerror" ]; then
 shift
 myquitonerror="no"
fi

unset must_set

for var in "$@"; do
if [ ${var/__//} != ${var} ]; then

    is_ce=1
    is_sub=1
    # We have a loop variable
    case $var in
    VO__*) looplist=VOS;;
    BDII__*) looplist=BDII_REGIONS;;
    *__GROUP_ENABLE) looplist=QUEUES;;
    CLUSTER__*) looplist=CLUSTERS;;
    SUBCLUSTER__*) looplist=CLUSTERS; is_sub=0;;
    CE_HOST__*) looplist=CLUSTERS; is_ce=0;;
    CE_HOST_*_QUEUE__*) prefix=${var/_QUEUE__*/}; ce=${prefix/CE_HOST_/}; looplist=CE_HOST_${ce}_QUEUES;;
    *) return 1;;
    esac

    prefix=${var/__*/}
    suffix=${var/*__/}

    if [ "x`eval echo '$'$looplist`" = "x" ]; then
        yaimlog ERROR "$looplist is not set" ;
        must_set="${must_set} $looplist"
    fi

#yaimlog INFO "var is $var; looplist is $looplist; is_ce is $is_ce; is_sub is $is_sub"
#yaimlog INFO "prefix is $prefix, suffix is $suffix"
    for loop in `eval echo '$'$looplist`; do
        if [ $looplist == VOS ]; then
          VO=`echo ${loop} | sed -e 's/-/_/g' -e 's/\./_/g' | tr '[:lower:]' '[:upper:]'`
          if [ "x`get_vo_param ${loop} ${suffix}`" = "x" ]; then
               yaimlog ERROR "${suffix} for ${loop} is not set !"
               must_set="${must_set} \$${prefix}_${VO}_${suffix}"
          fi
        else
          if [ $looplist == QUEUES ]; then
            loop=`echo ${loop} | sed -e 's/-/_/g' -e 's/\./_/g' | tr '[:lower:]' '[:upper:]'`
            if [ "x`eval echo '$'${loop}_${suffix}`" = "x" ]; then
              yaimlog ERROR "${loop}_${suffix} is not set";
              must_set="${must_set} \$${loop}_${suffix}"
            fi
          else
            if [ $looplist == CE_HOST_*_QUEUES ]; then
              loop=`echo ${loop} | tr '[:lower:]' '[:upper:]'`
              if [ "x`eval echo '$'${prefix}_${loop}_${suffix}`" = "x" ]; then
                yaimlog ERROR "${prefix}_${loop}_${suffix} is not set";
                must_set="${must_set} \$${prefix}_${loop}_${suffix}"
              fi
            else
            if [ $looplist == CLUSTERS ]; then
              loop=`echo ${loop} | tr '[:lower:]' '[:upper:]'`
              if [ $is_ce -eq 0 ]; then                                           # CE_HOST variable
                for ce in `eval echo "\\$CLUSTER_${loop}_CE_HOSTS"`; do
                  ce=`echo ${ce} | sed -e 's/-/_/g' -e 's/\./_/g'`
                  if [ "x`eval echo '$'${prefix}_${ce}_${suffix}`" = "x" ]; then
                    yaimlog ERROR "${prefix}_${ce}_${suffix} is not set";
                    must_set="${must_set} \$${prefix}_${ce}_${suffix}"
                  fi
                done
              else
                if [ $is_sub -eq 0 ]; then
                  for sub in `eval echo "\\$CLUSTER_${loop}_SUBCLUSTERS"`; do
                    sub=`echo ${sub} | tr '[:lower:]' '[:upper:]'`
                    if [ "x`eval echo '$'${prefix}_${sub}_${suffix}`" = "x" ]; then
                      yaimlog ERROR "${prefix}_${sub}_${suffix} is not set";
                      must_set="${must_set} \$${prefix}_${sub}_${suffix}"
                    fi
                  done
                else
                  loop=`echo ${loop} | tr '[:lower:]' '[:upper:]'`
                  if [ "x`eval echo '$'${prefix}_${loop}_${suffix}`" = "x" ]; then
                    yaimlog ERROR "${prefix}_${loop}_${suffix} is not set";
                    must_set="${must_set} \$${prefix}_${loop}_${suffix}"
                  fi
                fi
              fi
            else
              loop=`echo ${loop} | tr '[:lower:]' '[:upper:]'`
              if [ "x`eval echo '$'${prefix}_${loop}_${suffix}`" = "x" ]; then
                yaimlog ERROR "${prefix}_${loop}_${suffix} is not set";
                must_set="${must_set} \$${prefix}_${loop}_${suffix}"
              fi # any other
            fi # CLUSTERS
          fi # CE_HOST_*_QUEUES
          fi # QUEUES
       fi # VOS
    done

else
    if [ "x`eval echo '$'$var`" = "x" ]; then
        yaimlog ERROR "$var is not set";
        must_set="${must_set} $var"
    fi
fi

done

if [ -z "$must_set" ]; then
    return 0
elif [ "x${myquitonerror}" = "xno" ]; then
    return 1
else
    exit 1
fi

}

