#!/bin/bash
##############################################################################
# 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_lcgce_info_service_rtepublish
#
# DESCRIPTION : This function configures the dynamic service publisher
#               for RTEPublisher node.
#
# AUTHORS :     Steve.Traylen@cern.ch massimo.sgaravatto@pd.infn.it
#               
# NOTES :       
#
# YAIM MODULE:  glite-yaim-cream-ce
#
##############################################################################
 
config_cream_info_service_rtepublish_check () {
  if [ "x${CREAM_CLUSTER_MODE}" = "xyes" ]; then
    return 0
  fi
  requires $1 SITE_NAME CLUSTER_HOST 
  ret_cod1=$?
  
}

config_cream_info_service_rtepublish_setenv () {
  yaimlog DEBUG "This function currently doesn't set any environment variables."
}

config_cream_info_service_rtepublish () {

if  [ "x${CREAM_CLUSTER_MODE}" == "xyes" ] ; then
    
    yaimlog DEBUG "Infoprovider wrapper for RTEPublisher is not required"
    return 0
    
fi

GLITE_CREAM_LOCATION_ETC=/etc
GLITE_CREAM_LOCATION_BIN=/usr/bin
GLITE_CREAM_LOCATION_GIP=/var/lib/bdii/gip

if [ "x${LCGCE_CLUSTER_MODE}" = "xyes" ]; then
  return 0
fi

INFO_SERVICE_CONFIG=${INFO_SERVICE_CONFIG:-${GLITE_CREAM_LOCATION_ETC}/glite/info/service}
INFO_SERVICE_SCRIPT=${INFO_SERVICE_SCRIPT:-${GLITE_CREAM_LOCATION_BIN}}
INFO_PROVIDER_PATH=${INFO_PROVIDER_PATH:-${GLITE_CREAM_LOCATION_GIP}/provider}

# ServiceId is yaim variable COMPUTING_SERVICE_ID if defined
# Otherwise it is hostname + "_ComputingElement"
if [ "x${COMPUTING_SERVICE_ID}" != "x" ]; then
  ServiceId=${COMPUTING_SERVICE_ID}
else
  ServiceId=${CE_HOST}_ComputingElement
fi



SERVICE=RTEPublisher
SERVICE_HOST=${CLUSTER_HOST}

SERVICE_LC=`echo ${SERVICE} | tr '[:upper:]' '[:lower:]'`

if [ ! -f ${INFO_SERVICE_CONFIG}/glite-info-service-${SERVICE_LC}.conf.template ]; then
  yaimlog ERROR "The template file for glite-info-service-${SERVICE_LC}.conf.template was not found in ${INFO_SERVICE_CONFIG}."
  exit 1
fi

if [ ! -f ${INFO_SERVICE_SCRIPT}/glite-info-service ]; then
  yaimlog ERROR "The script file for glite-info-service was not found in ${INFO_SERVICE_SCRIPT}."
  exit 1
fi

if [ ! -f ${INFO_SERVICE_SCRIPT}/glite-info-glue2-endpoint ]; then
  yaimlog ERROR "The script file for glite-info-glue2-endpoint was not found in ${INFO_SERVICE_SCRIPT}."
  exit 1
fi


rpmcmd=`which rpm`
  if  [ $? == 0 ]; then
   yaimlog DEBUG "Using rpm to find lcg-info-dynamic-software version"
   get_version_cmd="rpm -q --qf %{V} lcg-info-dynamic-software"
  else
   dpkgcmd=`which dpkg-query`
   if  [ $? == 0 ]; then
      yaimlog DEBUG "Using dpkg-query to find lcg-info-dynamic-software version"
      get_version_cmd="dpkg-query -W -f='${Version}' lcg-info-dynamic-software"
   else
      yaimlog ERROR "This system doen't support rpm nor dpkg-query"
      exit 1
   fi
  fi




yaimlog DEBUG "Delete a previous version of the glite-info-service-${SERVICE_LC}.conf if it exists"
rm -rf ${INFO_SERVICE_CONFIG}/glite-info-service-${SERVICE_LC}.conf

yaimlog DEBUG "Create the glite-info-service-${SERVICE_LC}.conf file out of the template file"
cat ${INFO_SERVICE_CONFIG}/glite-info-service-${SERVICE_LC}.conf.template | sed \
 -e "s/get_version =.*/get_version = ${get_version_cmd}/" \
> ${INFO_SERVICE_CONFIG}/glite-info-service-${SERVICE_LC}.conf


yaimlog DEBUG "Create the glite-info-glue2-${SERVICE_LC}.conf file out of the template file"
cat ${INFO_SERVICE_CONFIG}/glite-info-glue2-${SERVICE_LC}.conf.template | sed \
  -e "s/get_implementationversion =.*/get_implementationversion = ${get_version_cmd}/" \
>  ${INFO_SERVICE_CONFIG}/glite-info-glue2-${SERVICE_LC}.conf



# Note: the configuration file may need to be altered if non standard values have been used. 

yaimlog DEBUG "Delete a previous version of the glite-info-provider-service-${SERVICE_LC}-wrapper if it exists"
rm -rf ${INFO_PROVIDER_PATH}/glite-info-provider-service-${SERVICE_LC}-wrapper

yaimlog DEBUG "Create the ${INFO_PROVIDER_PATH} in case it doesn't exist"
mkdir -p ${INFO_PROVIDER_PATH} 

yaimlog DEBUG "Create the glite-info-provider-service-${SERVICE_LC}-wrapper file"
cat << EOF > ${INFO_PROVIDER_PATH}/glite-info-provider-service-${SERVICE_LC}-wrapper
#!/bin/sh
export PATH=$PATH:${INFO_SERVICE_SCRIPT}
export ${SERVICE}_HOST=${SERVICE_HOST}
export GRIDFTP_PATH=/opt/glite/var/info
${INFO_SERVICE_SCRIPT}/glite-info-service ${INFO_SERVICE_CONFIG}/glite-info-service-${SERVICE_LC}.conf $SITE_NAME
${INFO_SERVICE_SCRIPT}/glite-info-glue2-endpoint ${INFO_SERVICE_CONFIG}/glite-info-glue2-${SERVICE_LC}.conf ${ServiceId}
EOF

chmod +x ${INFO_PROVIDER_PATH}/glite-info-provider-service-${SERVICE_LC}-wrapper 
 
  
return 0

}
