#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2011.
# 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_lfc_semsg
#
# DESCRIPTION : This function configures SEMSG
#
# AUTHORS :     aalvarez@cern.ch fabrizio.furano@cern.ch
#
# YAIM MODULE:  glite-yaim-lfc
#
##############################################################################

function config_lfc_semsg_check () {
 requires $1 LFCMGR_USER LFCMGR_GROUP LFC_SEMSG LFC_SEMSG_CONFIG LFC_SEMSG_PFXDIR
}


function config_lfc_semsg () {
  if [ "$LFC_SEMSG" == "no" ]; then
    yaimlog INFO "LFC_SEMSG set to no. SEMsg will not be configured."
    return 0
  fi

  # Modify main daemon startup file
  yaimlog INFO "Setting the SEMsgdaemon startup parameters..."

  sed -i "s%RUN_SEMSGDAEMON=.*%RUN_SEMSGDAEMON=${LFC_SEMSG}%g"         "/etc/sysconfig/SEMsgdaemon"

  yaimlog INFO "LFC_SEMSG_CONFIG=${LFC_SEMSG_CONFIG}"
  sed -i "s%SEMSGCONFIGFILE=.*%SEMSGCONFIGFILE=${LFC_SEMSG_CONFIG}%g"  "/etc/sysconfig/SEMsgdaemon"

  yaimlog INFO "LFC_SEMSG_PFXDIR=${LFC_SEMSG_PFXDIR}"
  sed -i "s%SEMSGPFXDIR=.*%SEMSGPFXDIR=${LFC_SEMSG_PFXDIR}%g"          "/etc/sysconfig/SEMsgdaemon"

  yaimlog INFO "LFCMGR_USER=${LFCMGR_USER}"
  sed -i "s/SEMSGUSER=.*/SEMSGUSER=${LFCMGR_USER}/g"                   "/etc/sysconfig/SEMsgdaemon"

  yaimlog INFO "LFCMGR_GROUP=${LFCMGR_GROUP}"
  sed -i "s/SEMSGGROUP=.*/SEMSGGROUP=${LFCMGR_GROUP}/g"                "/etc/sysconfig/SEMsgdaemon"

  yaimlog INFO "Enabling the message sending in lfcdaemon"
  sed -i "s%.*export SEMSGCONFIGFILE=.*%export SEMSGCONFIGFILE=${LFC_SEMSG_CONFIG}%g"  "/etc/sysconfig/lfcdaemon"

  # Set /etc/sysconfig/SEMsgdaemon
  sed -i "s%RUN_SEMSGDAEMON=.*%RUN_SEMSGDAEMON=\"yes\"%g" /etc/sysconfig/SEMsgdaemon


  # Restart service
  # standard location under /etc/init.d
  service SEMsgdaemon restart



  return 0
}

