#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2008.
# 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_lfc_server
#
# DESCRIPTION : This function upgrades the LFC server
#
# AUTHORS :     Akos.Frohner@cern.ch
#
# YAIM MODULE:  glite-yaim-lfc
#
##############################################################################

config_lfc_server_check () {
 requires $1 LFC_HOST VOS GLITE_LOCATION LCG_LOCATION
 retcode=$?
 return ${retcode}
}


function config_lfc_server () {

INSTALL_ROOT=${INSTALL_ROOT:-/opt} 
NSCONFIG=${LCG_LOCATION}/etc/NSCONFIG


####@ Hack to be removed when VOMS or YAIM CORE provides this !
if [ `uname -m` = 'x86_64' ]; then
	if [ `grep -c ^${GLITE_LOCATION}/lib64 /etc/ld.so.conf` = 0 ]; then
		echo "${GLITE_LOCATION}/lib64" >> /etc/ld.so.conf
		/sbin/ldconfig
	fi
else
	if [ `grep -c ^${GLITE_LOCATION}/lib /etc/ld.so.conf` = 0 ]; then
		echo "${GLITE_LOCATION}/lib" >> /etc/ld.so.conf
		/sbin/ldconfig
	fi
fi


if [ ! -d /etc/grid-security/${LFCMGR_USER} ]; then
    mkdir /etc/grid-security/${LFCMGR_USER}
fi

cp -pf /etc/grid-security/hostcert.pem /etc/grid-security/${LFCMGR_USER}/lfccert.pem
cp -pf /etc/grid-security/hostkey.pem /etc/grid-security/${LFCMGR_USER}/lfckey.pem

chown -R ${LFCMGR_USER}:${LFCMGR_GROUP} /etc/grid-security/${LFCMGR_USER}

####@ Create sysconfig file from template if it doesn't exist

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

sed -i -e "/^.*NSCONFIGFILE.*$/ d" /etc/sysconfig/lfcdaemon
echo NSCONFIGFILE=${NSCONFIG} >> /etc/sysconfig/lfcdaemon

# Yaim uses ORACLE_LOCATION, while it is usually 
# ORACLE_HOME, what is set by Oracle packages
ORACLE_HOME=${ORACLE_HOME:-${ORACLE_LOCATION}/client}

sed -i -e "/^[^#]*ORACLE_HOME.*$/ d" /etc/sysconfig/lfcdaemon
echo ORACLE_HOME=${ORACLE_HOME} >> /etc/sysconfig/lfcdaemon

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

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

####@ End of temporary fix


/sbin/chkconfig --add lfcdaemon
/sbin/service lfcdaemon stop 

yaimlog INFO "Waiting for complete wake-up of the name server..."

/sbin/service lfcdaemon start
sleep 10

if ( ! /sbin/service lfcdaemon status > /dev/null 2>&1 ); then 
	yaimlog ERROR "LFC Daemon Startup failed - please check."
	return 1;
fi

# Enable service
/sbin/chkconfig lfcdaemon on

# create directory structure
export LFC_HOST
export LFC_CONRETRY=0

if ( ! ${LCG_LOCATION}/bin/lfc-ls /grid > /dev/null 2>&1 ); then
    if ( ! ${LCG_LOCATION}/bin/lfc-mkdir /grid ); then
      yaimlog ERROR "Can not create /grid"
      return 1;
    fi
fi

unset vo_list
if [ "$LFC_LOCAL" ] || [ "$LFC_CENTRAL" ]; then
    vo_list="$LFC_LOCAL $LFC_CENTRAL"
else
    vo_list=$VOS
fi

yaimlog DEBUG "Creating default paths for the VOs"
    
for vo in `echo $vo_list | tr '[:upper:]' '[:lower:]'`; do
    if ( ! ${LCG_LOCATION}/bin/lfc-ls /grid/$vo > /dev/null 2>&1 ); then
	    ${LCG_LOCATION}/bin/lfc-mkdir /grid/$vo
    fi
    ${LCG_LOCATION}/bin/lfc-chmod 775 /grid/$vo
    ${LCG_LOCATION}/bin/lfc-entergrpmap --group $vo
    ${LCG_LOCATION}/bin/lfc-chown root:$vo /grid/$vo
    ${LCG_LOCATION}/bin/lfc-setacl -m d:u::7,d:g::7,d:o:5 /grid/$vo
done 

return 0

}

