##############################################################################
# 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_globus_gridftp
#
# DESCRIPTION : This function configures the GT4 gridftp server.
#
# AUTHORS :     Shu-Ting.Liao@cern.ch
#               Nuno.Orestes.Vaz.da.Silva@cern.ch
#               Di.Qing@cern.ch
#
# NOTES :       Requires also the use of config_globus_clients
#
# YAIM MODULE:  glite-yaim-core
#
##############################################################################

config_globus_gridftp_check () {                                                                                                                             
        requires $1 INSTALL_ROOT GLOBUS_TCP_PORT_RANGE GRIDFTP_CONNECTIONS_MAX
}                                                                                             
config_globus_gridftp_setenv () {

	# Auxiliary parameters
#        GLOBUS_LOCATION="${GLOBUS_LOCATION:-${INSTALL_ROOT}/globus}"                    
        yaimgridenv_set GLOBUS_TCP_PORT_RANGE "${GLOBUS_TCP_PORT_RANGE}"

# not needed for EMI
#if [ -d ${GLOBUS_LOCATION} ]; then	
	# Set environment
#        yaimgridenv_set GLOBUS_LOCATION "${GLOBUS_LOCATION}"
#        yaimgridpath_prepend LD_LIBRARY_PATH "${GLOBUS_LOCATION}/lib"
#        yaimgridpath_prepend PATH "${GLOBUS_LOCATION}/bin"
#fi

}

config_globus_gridftp () {

  ####@ Set up maximum number of connections
  yaimlog DEBUG "Set up maximum number of connections"

if [ -f /etc/gridftp.conf ]; then
  file=/etc/gridftp.conf
else
  if [ -f /etc/gridftp.conf.default]; then
  # old globus-gridftp-server-progs-3.33-2
     cp /etc/gridftp.conf.default /etc/gridftp.conf
     file=/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}" >> /etc/gridftp.conf
      done=0
    fi
  fi
fi
  
  if [ ! ${done} ]; then
    grep connections_max ${file} > /dev/null
    if [ ! $? = 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 ${mycnftmp} ${file}
      rm -rf ${mycnftmp}
    fi
  fi
  
# needed by glite-initscript-globus-gridftp
if [ ! -f /etc/grid-security/gridftp.conf ]; then
   ln -s /etc/gridftp.conf /etc/grid-security/gridftp.conf
fi

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


