#!/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_xrootd
#
# DESCRIPTION : This function configures the xroot daemon for DPM
#
# AUTHORS :     David.Smith@cern.ch
#
# YAIM MODULE:  glite-yaim-dpm
#
##############################################################################

config_DPM_xrootd_check () {
  if [ "$DPM_XROOTD_NO_YAIM" = "yes" ]; then
	return 0
  fi
  if [ -r /usr/lib64/libXrdDPMOss.so.3 -o -r /usr/lib/libXrdDPMOss.so.3 ]; then
	requires $1 MY_DOMAIN DPNS_BASEDIR DPMMGR_USER DPMMGR_GROUP DPM_XROOTD_SHAREDKEY
	ret=$?
	if [ $ret -ne 0 ]; then
		return $ret
	fi
	if [ ${#DPM_XROOTD_SHAREDKEY} -lt 32 ]; then
		yaimlog ERROR "The shared secret DPM_XROOTD_SHAREDKEY is too short"
		return 1
	fi
	if [ ${#DPM_XROOTD_SHAREDKEY} -gt 64 ]; then
		yaimlog ERROR "The shared secret DPM_XROOTD_SHAREDKEY is too long"
		return 1
	fi
  fi
  return 0
}

write_alice() {
	local file
	file=$1
	cat >> $file <<EOF
setenv TTOKENAUTHZ_AUTHORIZATIONFILE=$ALICETOK_CONF
ofs.authlib libXrdDPMRedirAcc.so.3 $ALICETOK_LIBNAME
dpm.fixedidrestrict /dpm/${MY_DOMAIN}/${DPNS_BASEDIR}/alice
dpm.principal $ALICETOK_PRINCIPAL
EOF
	if [ "$ALICETOK_PRINCIPAL" != "root" ]; then
		echo "dpm.fqan $ALICETOK_FQANS" >> $file
	fi
	return
}

config_DPM_xrootd () {
cfname="CONFIG_DPM_XROOTD"

INSTALL_ROOT=${INSTALL_ROOT:-/opt}

####@ Check if this component has to be configured
if [ ! -r /usr/lib64/libXrdDPMOss.so.3 -a ! -r /usr/lib/libXrdDPMOss.so.3 ]; then
	yaimlog INFO " [$cfname] dpm-xrootd is not installed, skipping its configuration"
	return 0
fi

if [ "$DPM_XROOTD_NO_YAIM" = "yes" ]; then
	yaimlog INFO " [$cfname] configured to skip dpm-xrootd setup"
	return 0
fi

/sbin/service cmsd stop
/sbin/service xrootd stop

####@ Find out if we are a disk server, disk server + head node or plain head node 

yaimlog INFO " [$cfname] Checking the configuration of this DPM node ...."

isdiskserver="false"
isheadnode="false"

if [ -e /etc/sysconfig/rfiod ]; then
    isdiskserver="true"
fi

if [ -e /etc/sysconfig/dpm ]; then
    isheadnode="true"
fi

if [ $isdiskserver = "true" ]; then
	if [ $isheadnode = "true" ]; then
		 nodetype="disk-and-head-node"
	else
		 nodetype="disk-node";
	fi
else
	if [ $isheadnode = "true" ]; then
		 nodetype="head-node"
	else
		 nodetype="none";
	fi
fi


####@ Notify about the configuration found on this host

if [ $nodetype = "none" ]; then
	yaimlog ERROR "[$cfname] This node is neither configured as a DPM head node nor a DPM disk server"
	return 1
fi

yaimlog INFO " [$cfname] This node has been detected to be of type <$nodetype>"
yaimlog INFO " [$cfname] Writing your xrootd configuration ..."

nfedredir=0
unset redir_host_xrd redir_host_cmsd redir_vo redir_vo_uc redir_vo_lc redir_paths
for redir_path in $DPM_XROOTD_FEDREDIRS; do
	nv=-2
	OLDIFS="$IFS"
	IFS=","
	unset pths rhostports tvo
	for v in $redir_path; do
		if [ $nv -eq -2 ]; then
			rhostports=$v
		elif [ $nv -eq -1 ]; then
			tvo="$v"
		else
			if [ "${v:0:1}" != "/" ]; then
				yaimlog ERROR " [$cfname] Federation path $v does not start with slash"
				return 1
			fi
			if [ "$v" != "/" -a "${v: -1}" != "/" ]; then
				v="${v}/"
			fi
			pths[nv]=$v
		fi
		let nv++
	done
	IFS=":"
	nv=0
	unset rhost rxrdport rcmsdport
	for v in $rhostports; do
		if [ $nv == 0 ]; then
			rhost=$v
		elif [ $nv == 1 ]; then
			rxrdport=$v
		elif [ $nv == 2 ]; then
			rcmsdport=$v
		fi
		let nv++
	done
	IFS="$OLDIFS"
	unset rhostxrd rhostcmsd
	if [ -n "$rxrdport" ]; then
		rhostxrd="$rhost:$rxrdport"
	fi
	if [ -n "$rcmsdport" ]; then
		rhostcmsd="${rhost}+:$rcmsdport"
	fi
	if [ -n "$rhostxrd" -a -n "$rhostcmsd" -a -n "$tvo" ]; then
		redir_host_xrd[nfedredir]=$rhostxrd
		redir_host_cmsd[nfedredir]=$rhostcmsd
		redir_vo[nfedredir]="$tvo"
		redir_vo_uc[nfedredir]="`echo $tvo | sed -e 's/[\.-]/_/g' |  tr '[:lower:]' '[:upper:]'`"
		redir_vo_lc[nfedredir]="`echo $tvo | sed -e 's/[\.-]/_/g' |  tr '[:upper:]' '[:lower:]'`"
		redir_paths[nfedredir]=${pths[@]}
		let nfedredir++
	else
		yaimlog ERROR " [$cfname] Syntax error in federation information in DPM_XROOTD_FEDREDIRS"
		return 1
	fi
done

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

if [ $isheadnode = "true" ]; then
	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" "xroot"
fi

## writing /etc/sysconfig/xrootd

cat > /etc/sysconfig/xrootd <<EOF
XROOTD_USER=$DPMMGR_USER
XROOTD_GROUP=$DPMMGR_GROUP
export DPM_HOST="${DPM_HOST}"
export DPNS_HOST="${DPNS_HOST}"
export DPM_CONRETRY=0
export DPNS_CONRETRY=0
if [ "\`uname -m\`" = "x86_64" ]; then
	XRDLIBDIR="lib64"
else
	XRDLIBDIR="lib"
fi
export XRDLIBDIR
export XRD_MAXREDIRECTCOUNT=1
EOF

if [ "$DPM_XROOTD_DEBUG" = "yes" ]; then
	cat >> /etc/sysconfig/xrootd <<EOF
DAEMON_COREFILE_LIMIT=unlimited
EOF
fi

myhostname="$(hostname -f)"
althosts=""
for hostmap in $DPM_XROOTD_ALTHOSTNAMES; do
	OLDIFS="$IFS"
	IFS=":"
	cmpname=""
	for v in $hostmap; do
		if [ -n "$cmpname" ]; then
			if [ "$cmpname" = "$myhostname" ]; then
				if [ -n "$althosts" ]; then
					althosts="$althosts "
				fi
				althosts="${althosts}${v}"
			fi
		else
			cmpname="$v"
		fi
	done
	IFS="$OLDIFS"
done
if [ -n "$althosts" ]; then
	echo "export DPMXRD_ALTERNATE_HOSTNAMES=\"$althosts\"" >> /etc/sysconfig/xrootd
fi

if [ $isheadnode = "true" ]; then
	echo "XROOTD_REDIR_OPTIONS=\"-l /var/log/xrootd/xrootd.log -c /etc/xrootd/xrootd-dpmredir.cfg\"" >> /etc/sysconfig/xrootd
fi
if [ $isdiskserver = "true" ]; then
	echo "XROOTD_DISK_OPTIONS=\"-l /var/log/xrootd/xrootd.log -c /etc/xrootd/xrootd-dpmdisk.cfg\"" >> /etc/sysconfig/xrootd
fi

if [ $isheadnode = "true" ]; then
	nv=0
	while [ $nv -lt $nfedredir ]; do
		echo "XROOTD_FEDREDIR_${redir_vo_uc[nv]}_OPTIONS=\"-l /var/log/xrootd/xrootd.log -c /etc/xrootd/xrootd-dpmfedredir_${redir_vo_lc[nv]}.cfg\"" >> /etc/sysconfig/xrootd
		echo "CMSD_FEDREDIR_${redir_vo_uc[nv]}_OPTIONS=\"-l /var/log/xrootd/cmsd.log -c /etc/xrootd/xrootd-dpmfedredir_${redir_vo_lc[nv]}.cfg\"" >> /etc/sysconfig/xrootd
		let nv++
	done
	if [ -n "$DPM_XROOTD_BASEFEDPORT" ]; then
		baseport=$DPM_XROOTD_BASEFEDPORT
	else
		baseport=11000
	fi
	nv=0
	while [ $nv -lt $nfedredir ]; do
		echo "export XROOT_FED_REMOTE_CMSD_MANAGER_${redir_vo_uc[nv]}=${redir_host_cmsd[nv]}" >> /etc/sysconfig/xrootd
		echo "export XROOT_FED_REMOTE_XRD_MANAGER_${redir_vo_uc[nv]}=${redir_host_xrd[nv]}" >> /etc/sysconfig/xrootd
		echo "export XROOT_FED_LOCAL_PORT_${redir_vo_uc[nv]}=$baseport" >> /etc/sysconfig/xrootd
		let nv++
		let baseport++
	done
	instances=""
	nv=0
	while [ $nv -lt $nfedredir ]; do
		if [ -n "$instances" ]; then
			instances="$instances "
		fi
		instances="${instances}fedredir_${redir_vo_lc[nv]}"
		let nv++
	done
	if [ -n "$instances" ]; then
		echo "CMSD_INSTANCES=\"$instances\"" >> /etc/sysconfig/xrootd
	fi
	if [ $isdiskserver = "true" ]; then
		if [ -n "$instances" ]; then
			instances=" $instances"
		fi
		instances="disk${instances}"
	fi
	if [ -n "$instances" ]; then
		instances=" $instances"
	fi
	instances="redir${instances}"
	echo "XROOTD_INSTANCES=\"$instances\"" >> /etc/sysconfig/xrootd

	needalicetok=0
	for vo in $VOS; do
		if [ "$vo" = "alice" ]; then
			needalicetok=1
		fi
	done

	alicetok=0
	if [ $needalicetok -ne 0 ]; then
		if [ -r "/usr/lib/$ALICETOK_LIBNAME" -o -r "/usr/lib64/$ALICETOK_LIBNAME" ]; then
			alicetok=1
			if [ ! -r "$ALICETOK_PRIVKEY_FILE" -o ! -r "$ALICETOK_PUBKEY_FILE" -o ! -r "$ALICETOK_CONF" ]; then
				yaimlog ERROR " [$cfname] ALICE token keys or configuration are missing"
				return 1
			fi
			chmod 640 $ALICETOK_PRIVKEY_FILE $ALICETOK_PUBKEY_FILE $ALICETOK_CONF
			chown $DPMMGR_USER:$DPMMGR_GROUP $ALICETOK_PRIVKEY_FILE $ALICETOK_PUBKEY_FILE $ALICETOK_CONF
			unset mf
			if [ -r /etc/lcgdm-mapfile ]; then
				mf="/etc/lcgdm-mapfile"
			elif [ -r /opt/lcg/etc/lcgdm-mapfile ]; then
				mf="/opt/lcg/etc/lcgdm-mapfile"
			fi
			if [ "$ALICETOK_PRINCIPAL" != "root" -a -n "$mf" ]; then
				grep -E -q '^"?'"$ALICETOK_PRINCIPAL"'"?[[:space:]]' $mf ${mf}-local
				if [ $? != 0 ]; then
					echo "\"$ALICETOK_PRINCIPAL\" alice" >> ${mf}
					echo "\"$ALICETOK_PRINCIPAL\" alice" >> ${mf}-local
				fi
			fi
		else
			yaimlog ERROR " [$cfname] To configure dpm-xrootd with alice support you need the alice token library"
			return 1
		fi
	fi

elif [ $isdiskserver = "true" ]; then
	echo "XROOTD_INSTANCES=\"disk\"" >> /etc/sysconfig/xrootd
fi

echo -n "$DPM_XROOTD_SHAREDKEY" > /etc/xrootd/dpmxrd-sharedkey.dat
chmod 640 /etc/xrootd/dpmxrd-sharedkey.dat
chown $DPMMGR_USER:$DPMMGR_GROUP /etc/xrootd/dpmxrd-sharedkey.dat

## writing /etc/xrootd/xrootd-dpm* files
##

usevoms=0
if [ "$DPM_XROOTD_USE_VOMS" = "yes" ]; then
	usevoms=1
fi

if [ $isdiskserver = "true" ]; then
	if [ ! -x /etc/xrootd/xrootd-dpmdisk.cfg.yaimsave -a -x /etc/xrootd/xrootd-dpmdisk.cfg ]; then
		mv /etc/xrootd/xrootd-dpmdisk.cfg /etc/xrootd/xrootd-dpmdisk.cfg.yaimsave
	fi
	cat > /etc/xrootd/xrootd-dpmdisk.cfg <<EOF
set xrdlibdir   = \$XRDLIBDIR
all.adminpath /var/spool/xrootd
all.pidpath /var/run/xrootd
xrootd.seclib libXrdSec.so
EOF
	if [ $usevoms -ne 0 ]; then
		echo "sec.protocol /usr/\$(xrdlibdir) gsi -crl:3 -key:/etc/grid-security/${DPMMGR_USER}/dpmkey.pem -cert:/etc/grid-security/${DPMMGR_USER}/dpmcert.pem -md:sha256:sha1 -ca:2 -gmapopt:10" >> /etc/xrootd/xrootd-dpmdisk.cfg
	else
		echo "sec.protocol /usr/\$(xrdlibdir) gsi -crl:3 -key:/etc/grid-security/${DPMMGR_USER}/dpmkey.pem -cert:/etc/grid-security/${DPMMGR_USER}/dpmcert.pem -md:sha256:sha1 -ca:2 -gmapopt:10 -vomsat:0" >> /etc/xrootd/xrootd-dpmdisk.cfg
	fi
	cat >> /etc/xrootd/xrootd-dpmdisk.cfg <<EOF
sec.protocol /usr/\$(xrdlibdir) unix
ofs.authlib libXrdDPMDiskAcc.so.3
ofs.authorize
ofs.osslib libXrdDPMOss.so.3
ofs.persist auto hold 0
xrootd.export /
xrd.port 1095
xrd.network nodnr
all.role server
EOF

	if [ "$DPM_XROOTD_DEBUG" = "yes" ]; then
		cat >> /etc/xrootd/xrootd-dpmdisk.cfg << EOF
ofs.trace all
xrd.trace all
oss.trace all
EOF
	fi
	if [ -n "$DPM_XROOTD_DISK_MISC" ]; then
		echo "$DPM_XROOTD_DISK_MISC" >> /etc/xrootd/xrootd-dpmdisk.cfg
	fi

fi


if [ $isheadnode = "true" ]; then
	if [ ! -x /etc/xrootd/xrootd-dpmredir.cfg.yaimsave -a -x /etc/xrootd/xrootd-dpmredir.cfg ]; then
		mv /etc/xrootd/xrootd-dpmredir.cfg /etc/xrootd/xrootd-dpmredir.cfg.yaimsave
	fi
	rm /etc/xrootd/xrootd-dpmredir.cfg
	if [ $nfedredir -gt 0 ]; then
		cat >> /etc/xrootd/xrootd-dpmredir.cfg <<EOF
set dpmhost = \$DPM_HOST
EOF
		nv=0
		while [ $nv -lt $nfedredir ]; do
			echo "set xrootfedlport${nv} = \$XROOT_FED_LOCAL_PORT_${redir_vo_uc[nv]}" >> /etc/xrootd/xrootd-dpmredir.cfg
			let nv++
		done
	fi
	cat >> /etc/xrootd/xrootd-dpmredir.cfg <<EOF
set xrdlibdir = \$XRDLIBDIR
all.adminpath /var/spool/xrootd
all.pidpath /var/run/xrootd
xrd.network nodnr
xrootd.seclib libXrdSec.so
EOF
	if [ $usevoms -ne 0 ]; then
		echo "sec.protocol /usr/\$(xrdlibdir) gsi -crl:3 -key:/etc/grid-security/${DPMMGR_USER}/dpmkey.pem -cert:/etc/grid-security/${DPMMGR_USER}/dpmcert.pem -md:sha256:sha1 -ca:2 -gmapopt:10" >> /etc/xrootd/xrootd-dpmredir.cfg
	else
		echo "sec.protocol /usr/\$(xrdlibdir) gsi -crl:3 -key:/etc/grid-security/${DPMMGR_USER}/dpmkey.pem -cert:/etc/grid-security/${DPMMGR_USER}/dpmcert.pem -md:sha256:sha1 -ca:2 -gmapopt:10 -vomsat:0" >> /etc/xrootd/xrootd-dpmredir.cfg
	fi
	cat >> /etc/xrootd/xrootd-dpmredir.cfg <<EOF
sec.protocol /usr/\$(xrdlibdir) unix
ofs.cmslib libXrdDPMFinder.so.3
ofs.osslib libXrdDPMOss.so.3
dpm.defaultprefix /dpm/${MY_DOMAIN}/${DPNS_BASEDIR}
dpm.xrdserverport 1095
xrootd.export /
all.role manager
ofs.forward all
ofs.authorize
EOF
	if [ $alicetok -eq 0 ]; then
		cat >> /etc/xrootd/xrootd-dpmredir.cfg <<EOF
ofs.authlib libXrdDPMRedirAcc.so.3
EOF
	else
		write_alice /etc/xrootd/xrootd-dpmredir.cfg
	fi

	nv=0
	while [ $nv -lt $nfedredir ]; do
		pths=${redir_paths[nv]}
		for pth in $pths; do
			echo "xrootd.redirect \$(dpmhost):\$(xrootfedlport${nv}) ${pth}" >> /etc/xrootd/xrootd-dpmredir.cfg
		done
		let nv++
	done

	if [ "$DPM_XROOTD_DEBUG" = "yes" ]; then
		cat >> /etc/xrootd/xrootd-dpmredir.cfg << EOF
ofs.trace all
xrd.trace all
cms.trace all
oss.trace all
EOF
	fi
	if [ -n "$DPM_XROOTD_REDIR_MISC" ]; then
		echo "$DPM_XROOTD_REDIR_MISC" >> /etc/xrootd/xrootd-dpmredir.cfg
	fi

	nv=0
	while [ $nv -lt $nfedredir ]; do
		fname="/etc/xrootd/xrootd-dpmfedredir_${redir_vo_lc[nv]}.cfg"
		if [ "${redir_vo[nv]}" = "atlas" ]; then
			if [ ! -x $fname.yaimsave -a -x $fname ]; then
				mv $fname $fname.yaimsave
			fi
		fi
		cat > $fname <<EOF
set xrootfedcmsdmanager = \$XROOT_FED_REMOTE_CMSD_MANAGER_${redir_vo_uc[nv]}
set xrootfedxrdmanager = \$XROOT_FED_REMOTE_XRD_MANAGER_${redir_vo_uc[nv]}
set xrootfedlport = \$XROOT_FED_LOCAL_PORT_${redir_vo_uc[nv]}
set xrdlibdir = \$XRDLIBDIR

all.adminpath /var/spool/xrootd
all.pidpath /var/run/xrootd
EOF

		if [ "$DPM_XROOTD_DEBUG" = "yes" ]; then
			cat >> $fname <<EOF
pss.trace all
ofs.trace all
xrd.trace all
cms.trace all
oss.trace all

xrd.network nodnr
EOF
		fi

		pths=${redir_paths[nv]}
		for pth in $pths; do
			echo "all.export ${pth}" >> $fname
		done

		cat >> $fname <<EOF

if exec cmsd
all.role server
ofs.osslib libXrdPss.so
pss.origin localhost:\$(xrootfedlport)
all.manager \$(xrootfedcmsdmanager)
fi

if exec xrootd
xrootd.seclib libXrdSec.so
EOF
		if [ $usevoms -ne 0 ]; then
			echo "sec.protocol /usr/\$(xrdlibdir) gsi -crl:3 -key:/etc/grid-security/${DPMMGR_USER}/dpmkey.pem -cert:/etc/grid-security/${DPMMGR_USER}/dpmcert.pem -md:sha256:sha1 -ca:2 -gmapopt:10" >> $fname
			echo "dpm.allowvo ${redir_vo[nv]}" >> $fname
		else
			echo "sec.protocol /usr/\$(xrdlibdir) gsi -crl:3 -key:/etc/grid-security/${DPMMGR_USER}/dpmkey.pem -cert:/etc/grid-security/${DPMMGR_USER}/dpmcert.pem -md:sha256:sha1 -ca:2 -gmapopt:10 -vomsat:0" >> $fname
		fi
		cat >> $fname <<EOF
sec.protocol /usr/\$(xrdlibdir) unix

ofs.cmslib libXrdDPMFinder.so.3
ofs.osslib libXrdDPMOss.so.3

dpm.xrdserverport 1095

xrd.port \$(xrootfedlport)
all.role manager
ofs.forward all
dpm.enablecmsclient

ofs.authorize
EOF

		eval "skipup=\$DPM_XROOTD_FED_${redir_vo_uc[nv]}_NO_REDIR_UP"
		if [ "$skipup" != "yes" ]; then
			pths=${redir_paths[nv]}
			for pth in $pths; do
				echo "xrootd.redirect \$(xrootfedxrdmanager) ? ${pth}" >> $fname
			done
		fi

		if [ ${redir_vo[nv]} != "alice" -o $alicetok -eq 0 ]; then
			cat >> $fname <<EOF
ofs.authlib libXrdDPMRedirAcc.so.3
EOF
		else
			write_alice $fname
		fi

		eval "fnlib=\$DPM_XROOTD_FED_${redir_vo_uc[nv]}_NAMELIB"
		eval "fnlibpfx=\$DPM_XROOTD_FED_${redir_vo_uc[nv]}_NAMELIBPFX"
		eval "fenv=\$DPM_XROOTD_FED_${redir_vo_uc[nv]}_SETENV"
		eval "frpfx=\$DPM_XROOTD_FED_${redir_vo_uc[nv]}_REPLACEPFX"
		eval "fdefpfx=\$DPM_XROOTD_FED_${redir_vo_uc[nv]}_DEFPFX"
		eval "fmisc=\$DPM_XROOTD_FED_${redir_vo_uc[nv]}_MISC"

		if [ -n "$fnlib" -a -z "$fnlibpfx" -o -z "$fnlib" -a -n "$fnlibpfx" ]; then
			yaimlog ERROR " [$cfname] If either NAMELIB or NAMELIBPFX is set then both must be (vo=${redir_vo[nv]})"
			return 1
		fi

		if [ -n "$fnlib" ]; then
			if [ -n "$fenv" ]; then
				for v in $fenv; do
					echo "setenv $v" >> $fname
				done
			fi
			echo "dpm.namelib $fnlib" >> $fname
			echo "dpm.namecheck $fnlibpfx" >> $fname
		fi

		if [ -n "$frpfx" ]; then
			echo "dpm.replacementprefix $frpfx" >> $fname
		fi
		if [ -n "$fdefpfx" ]; then
			echo "dpm.defaultprefix $fdefpfx" >> $fname
		fi
		echo "fi" >> $fname
		if [ -n "$fmisc" ]; then
			echo "$fmisc" >> $fname
		fi
			
		let nv++
	done
fi

chown ${DPMMGR_USER}:${DPMMGR_GROUP} /var/log/xrootd
/sbin/chkconfig xrootd on
/sbin/service xrootd start

if [ $isheadnode = "true" ]; then
	grep -E -q '^[^#].*DPM_USE_SYNCGET.*yes' /etc/sysconfig/dpm
	if [ $? != 0 ]; then
		yaimlog INFO " [$cfname] dpm-xrootd may benefit from enabling DPM_USE_SYNCGET in /etc/sysconfig/dpm on the headnode"
	fi
fi

if [ $isheadnode = "true" -a $nfedredir -gt 0 ]; then
	/sbin/chkconfig cmsd on
	/sbin/service cmsd start
else
	/sbin/chkconfig cmsd off
	/sbin/service cmsd stop
fi

yaimlog INFO " [$cfname] Started successfully dpm-xrootd!"
yaimlog INFO " [$cfname] Configuration of DPM-xrootd successfull!"

return 0
}
