##############################################################################
# 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_storm_globus_gridftp
#
# DESCRIPTION : This function configures the GT4 gridftp server.
#
# AUTHORS :     Elisabetta.ronchieri@cnaf.infn.it
#
#
# CREDITS :     Nuno.Orestes.Vaz.da.Silva@cern.ch
#               Di.Qing@cern.ch
#
# NOTES :       Requires also the use of config_globus_clients
#
# YAIM MODULE:  yaim-storm
#
##############################################################################

config_storm_globus_gridftp_check () {

        requires $1 GRIDFTP_WITH_DSI GLOBUS_TCP_PORT_RANGE GRIDFTP_CONNECTIONS_MAX
}                                                                                             

config_storm_globus_gridftp_setenv () {

        # Set environment
        yaimgridenv_set GLOBUS_TCP_PORT_RANGE "${GLOBUS_TCP_PORT_RANGE}"
        yaimgridenv_set GRIDFTP_WITH_DSI "${GRIDFTP_WITH_DSI}"

}

config_storm_globus_gridftp () {

  ####@ Set up maximum number of connections
  yaimlog DEBUG "Set up maximum number of connections"
  #if [ -d "${GLOBUS_LOCATION}" ]; then
   # if [ -f ${GLOBUS_LOCATION}/etc/gridftp.conf ]; then
   #   file=${GLOBUS_LOCATION}/etc/gridftp.conf
   # else
  if [ -f /etc/grid-security/gridftp.conf ]; then
    file="/etc/grid-security/gridftp.conf"
  else
    #echo "connections_max ${GRIDFTP_CONNECTIONS_MAX}" >> ${GLOBUS_LOCATION}/etc/gridftp.conf
    echo "connections_max ${GRIDFTP_CONNECTIONS_MAX}" >> /etc/grid-security/gridftp.conf
    done=0
  fi
    #fi
  #else
  #  file="/etc/grid-security/gridftp.conf"
  #fi

  if [ ! ${done} ]; then
    grep connections_max ${file} > /dev/null
    if [ ! $? -eq 0 ]; then
      yaimlog DEBUG "connections_max is not defined in ${file}."
      yaimlog DEBUG "Adding the value of connections_max with ${GRIDFTP_CONNECTIONS_MAX}..."
      echo "connections_max ${GRIDFTP_CONNECTIONS_MAX}" >> ${file}  
    else
      yaimlog DEBUG "Updating the value of connections_max with ${GRIDFTP_CONNECTIONS_MAX}..."
      mycnftmp=`mktemp /tmp/my.cnf.XXXXXX`
      cat ${file} | sed -e "s/connections_max.*/connections_max ${GRIDFTP_CONNECTIONS_MAX}/" > ${mycnftmp}
      mv -f ${mycnftmp} ${file}
    fi
  fi

  ####@ (Re)start gridftp service
  STORM_GLOBUS_GRIDFTP_INIT="/etc/init.d/storm-globus-gridftp"
  if test -x /etc/init.d/storm-globus-gridftp ; then
    yaimlog INFO "Starting gridftp service :"
    /sbin/chkconfig storm-globus-gridftp on
    /sbin/service storm-globus-gridftp restart
	  if [ ! $? = 0 ] ; then
     	yaimlog ERROR "Service storm-globus-gridftp failed to start!"
     	return 1
    fi
  else
    yaimlog ERROR "Unable to execute ${STORM_GLOBUS_GRIDFTP_INIT}."
    return 1
  fi                                                                                                                             
  ####@ Exit with success
  return 0
}


