#!/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_DPM_semsg
#
# DESCRIPTION : This function configures the SEMsg daemon+plugins
#
# AUTHORS :     fabrizio.furano@cern.ch
#
# YAIM MODULE:  glite-yaim-dpm
#
##############################################################################

config_DPM_semsg_check () {
  requires $1 DPM_HOST DPM_SEMSG DPM_SEMSG_CONFIG DPM_SEMSG_PFXDIR
}


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

  DPNS_HOST=${DPNS_HOST:-$DPM_HOST}
  
  # Modify main daemon startup file
  yaimlog INFO "Setting the SEMsgdaemon startup parameters"
  sed -i "s/RUN_SEMSGDAEMON=.*/RUN_SEMSGDAEMON=${DPM_SEMSG}/g"         "/etc/sysconfig/SEMsgdaemon"
  sed -i "s:SEMSGCONFIGFILE=.*:SEMSGCONFIGFILE=${DPM_SEMSG_CONFIG}:g"  "/etc/sysconfig/SEMsgdaemon"
  sed -i "s:SEMSGPFXDIR=.*:SEMSGPFXDIR=${DPM_SEMSG_PFXDIR}:g"          "/etc/sysconfig/SEMsgdaemon"
  sed -i "s/SEMSGUSER=.*/SEMSGUSER=${DPMMGR_USER}/g"                   "/etc/sysconfig/SEMsgdaemon"
  sed -i "s/SEMSGGROUP=.*/SEMSGGROUP=${DPMMGR_GROUP}/g"                "/etc/sysconfig/SEMsgdaemon"
  yaimlog INFO "Enabling the message sending in dpnsdaemon"
  sed -i "s:#.*export SEMSGCONFIGFILE=.*:export SEMSGCONFIGFILE=${DPM_SEMSG_CONFIG}:g"  "/etc/sysconfig/dpnsdaemon"

  # If SELinux is running, connections should to be allowed
  which setsebool &> /dev/null
  if [ $? -eq 0 ]; then
    yaimlog INFO "Remember to configure SELinux properly!"
  fi

  # Restart service
  # standard location under /etc/init.d
  service SEMsgdaemon restart
  if [ $? -ne 0 ]; then
    yaimlog ERROR "Could not start the SEMsgdaemon daemon"
    return 1
  fi

  
  # Done
  return 0
}

