#!/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_DPM_rfio
#
# DESCRIPTION : This function configures the RFIO service
#
# AUTHORS :     David.Smith@cern.ch, Gergely.Debreczeni@cern.ch
#
# YAIM MODULE:  glite-yaim-dpm
#
##############################################################################

config_DPM_rfio_check() { 
  requires $1 DPM_HOST DPM_FILESYSTEMS
  retcode=$?
  return ${retcode}
}

config_DPM_rfio() {

INSTALL_ROOT=${INSTALL_ROOT:-/opt}

if [ "x`grep rfio /etc/services | grep tcp`" = "x"  ]; then
    echo "rfio     5001/tcp" >> /etc/services
fi

if [ "x`grep rfio /etc/services | grep udp`" = "x"  ]; then
    echo "rfio     5001/udp" >> /etc/services
fi

touch /etc/shift.conf

if [ -f ${FUNCTIONS_DIR}/config_add_to_shift ]; then
	source ${FUNCTIONS_DIR}/config_add_to_shift
else
	yaimlog WARNING "${FUNCTIONS_DIR}/config_add_to_shift not found. Exiting."
        return 1
fi

for i in 'RFIOD TRUST' 'RFIOD WTRUST' 'RFIOD RTRUST' 'RFIOD XTRUST' 'RFIOD FTRUST'; do
    for j in $DPM_FILESYSTEMS; do
        host=`echo $j | awk -F: '{if ($3) print $2; else print $1}'`
        add_to_shift "$i" "${host}"
    done
    add_to_shift "$i" `hostname -f`
    add_to_shift "$i" "${DPM_HOST}"
done


if ( echo $NODE_TYPE_LIST | grep -qi dpm_mysql ); then
    for i in $DPM_FILESYSTEMS; do
        host=`echo $i | awk -F: '{if ($3) print $2; else print $1}'`
        add_to_shift 'DPM TRUST'  "${host}"
        add_to_shift 'DPNS TRUST' "${host}"
    done
fi

add_to_shift "DPM PROTOCOLS" "rfio"

####@ If /etc/sysconfig/dpm-gsiftp already exists, do not overwrite it with the template.
####@ Otherwise, only substitute the needed values

if [ ! -f /etc/sysconfig/rfiod ]; then
        cp /etc/sysconfig/rfiod.templ /etc/sysconfig/rfiod
fi

grep -v -E 'DPNS_HOST|DPM_HOST' /etc/sysconfig/rfiod > /etc/sysconfig/rfiod+
mv -f /etc/sysconfig/rfiod+ /etc/sysconfig/rfiod
echo "export DPNS_HOST=${DPM_HOST}" >> /etc/sysconfig/rfiod
echo "export DPM_HOST=${DPM_HOST}" >> /etc/sysconfig/rfiod

if [ "$RFIO_PORT_RANGE" ]; then
	grep -v RFIO_PORT_RANGE /etc/sysconfig/rfiod > /etc/sysconfig/rfiod+
	mv -f /etc/sysconfig/rfiod+ /etc/sysconfig/rfiod
	echo "export RFIO_PORT_RANGE=\"${RFIO_PORT_RANGE}\"" >> /etc/sysconfig/rfiod
fi 

# Enable V3 and set buffer size
yaimlog INFO "Setting up rfio V3"
grep "RFIO_READOPT=" /etc/sysconfig/rfiod -q
if [ $? -ne 0 ]; then
  echo -e "export RFIO_READOPT=16" >> /etc/sysconfig/rfiod
else
  sed -i "s/RFIO_READOPT=.*/RFIO_READOPT=16/g" /etc/sysconfig/rfiod
fi

add_to_shift 'RFIO DAEMONV3_RDMT_BUFSIZE' 524288
add_to_shift 'RFIO DAEMONV3_RDSIZE'       524288

###### Temporary fix in case RUN_RFIOD="yes" is missing from the existing sysconfig file

runmissing=`grep RUN_RFIOD /etc/sysconfig/rfiod`
if [ "x${runmissing}x" == "xx" ]; then
	echo "RUN_RFIOD=\"yes\"" >> /etc/sysconfig/rfiod
fi

###### End of temporary fix


/sbin/service rfiod stop
sleep 1s
/sbin/service rfiod start
if [ $? -ne 0 ]; then
  yaimlog ERROR "Could not start the rfio daemon"
  return 1
fi
/sbin/chkconfig rfiod on

return 0
}

