##############################################################################
# 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 :        config_wn_info
#
# DESCRIPTION : This function configures the glite-wn-info utility
#
# AUTHORS :     Maria.Alandes.Pradillo@cern.ch
#
# NOTES :      
#
# YAIM MODULE:  glite-yaim-clients
#                 
##############################################################################

config_wn_info_check () {

  requires $1 WN_LIST WN_INFO_CONFIG_FILE

}

config_wn_info () {
 
  unset my_wn_host
  unset my_wn_info
  unset my_subcluster
  unset config_file
  unset what_node

  config_file=${WN_INFO_CONFIG_FILE}

  if ( ! echo "${NODE_TYPE_LIST}" | grep -qE 'TAR|tar'); then
    my_wn_host=`hostname -f`
    my_wn_info=`grep ${my_wn_host} ${WN_LIST}` 
    if [ "x${my_wn_info}" == "x" ]; then
      yaimlog ERROR "The WN you are configuring is not defined in the WN_LIST file ${WN_LIST}"
      yestr ${YEX_CONFIG}
      yaimlog ERROR "${YERRORSTR}"
      exit ${YEX_CONFIG}
    fi
    my_subcluster=`echo ${my_wn_info} | awk -F: '{print $2}'`
    what_node="WN"
  else
    my_subcluster=`grep tarball-installation ${WN_LIST} | awk -F: '{print $2}'`
    what_node="tarball"
  fi
  
  if [ "x${my_subcluster}" == "x" ]; then
    yaimlog WARNING "No subcluster has been defined for the ${what_node} in the WN_LIST file ${WN_LIST}"
    yaimlog WARNING "YAIM will use the default subcluster id: CE_HOST -> ${CE_HOST}"  
    if  [ "x${CE_HOST}" == "x" ]; then
      yaimlog ERROR "No subcluster has been defined in WN_LIST, and CE_HOST is not defined."
      yaimlog ERROR "Please, define CE_HOST or subcluster in WN_LIST"
      yestr ${YEX_CONFIG}
      yaimlog ERROR "${YERRORSTR}"
      exit ${YEX_CONFIG}
    fi
    my_subcluster=${CE_HOST}
  fi

  if [ -f ${config_file} ]; then
    mv ${config_file} ${config_file}.old
  fi
  echo "[static]" > ${config_file}
  echo "GlueSubClusterUniqueId=${my_subcluster}" >> ${config_file}

  return 0

}
