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

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

function config_DPM_gsiftp () {

####@ Update /etc/shift.conf with "gsiftp" protocol

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

add_to_shift "DPM PROTOCOLS" "gsiftp"

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

if [ ! -f /etc/sysconfig/dpm-gsiftp ]; then
	if [ -f /etc/sysconfig/dpm-gsiftp.templ ]; then
		cp /etc/sysconfig/dpm-gsiftp.templ /etc/sysconfig/dpm-gsiftp
        elif [ -f /usr/share/dpm-dsi/dpm-gsiftp.sysconfig ]; then
		cp /usr/share/dpm-dsi/dpm-gsiftp.sysconfig /etc/sysconfig/dpm-gsiftp
	else
		yaimlog WARNING "dpm-gsiftp sysconfig template not found!"
		return 0
	fi
fi

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


####@ Stops globus mds if it is still running. We are not using that any more.

service globus-mds stop >& /dev/null
chkconfig globus-mds off >& /dev/null

####@ Writes a basic globus configuration

# Basic globus configuration, which would be only used by the
# GT2 based dpm-gsiftp server. The GT4 based gridftp server 
# (which may run on x86_64) doesn't need this configuration, 
# so having 'gcc32dbg' hardcoded is "OK".
cat << EOF > /etc/globus.conf
########################################################################
#
# Globus configuraton.
#
########################################################################
[common]
GLOBUS_LOCATION=/opt/globus
globus_flavor_name=gcc32dbg
x509_user_cert=/etc/grid-security/hostcert.pem
x509_user_key=/etc/grid-security/hostkey.pem
gridmap=/etc/grid-security/grid-mapfile
gridmapdir=/etc/grid-security/gridmapdir/

[gridftp]
EOF

####@ Starts dpm-gsiftp service
/sbin/service dpm-gsiftp restart
if [ $? -ne 0 ]; then
  yaimlog ERROR "Could not start the dpm-gsiftp daemon"
  return 1
fi
/sbin/chkconfig dpm-gsiftp on

return 0

}
