##############################################################################
# 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_lcgenv
#
# DESCRIPTION : This function removes old environment files and declares 
#               env variables affecting most node types.
#
# AUTHORS :     Andreas.Unterkicher@cern.ch
#               Gergely.Debreczeni@cern.ch
#               Maria.Alandes@cern.ch
#
# NOTES :       This function used to create environment files for different services.
#               This is now done in each relevant node type. Normally in the main
#               configuration function (_check or _setenv).
#    
#               3.1 glite UI  - config_glite_ui_setenv    - X509_USER_PROXY
#               3.1 VOBOX     - config_vobox_check        - SITE_NAME
#                             - config_vobox_setenv       - SITE_GIIS_URL   
#               3.1 WN        - config_wn_setenv          - SITE_GIIS_URL, EDG_WL_SCRATCH 
#
# YAIM MODULE:  glite-yaim-core
#                 
##############################################################################

config_lcgenv_check () {
ret=0
if ( echo "${NODE_TYPE_LIST}" | egrep -q '(CE|CREAM|WN|VOBOX)' ); then
  requires $1 VOS SE_LIST VO__SW_DIR
  let "ret |= $?"
fi

if [ ! "$BDII_LIST" ]; then
  requires $1 BDII_HOST
  let "ret |= $?"
fi

if [ "$VOBOX_HOST" ]; then
   requires $1 GSSKLOG
   let "ret |= $?"
   if  [ "${GSSKLOG}x" == "yesx" ]; then
      requires $1 GSSKLOG_SERVER
      let "ret |= $?"
   fi
fi

if [ $ret -ne 0 ]; then
   return 1
fi

return 0
}

config_lcgenv() {

rm -f ${GLITE_EXTERNAL_ROOT:-}/etc/profile.d/lcgenv.sh
rm -f ${GLITE_EXTERNAL_ROOT:-}/etc/profile.d/lcgenv.csh

TEMP_SE=`eval echo \$SE_LIST | sed s'/^[ ]*//'`;
default_se="${TEMP_SE%% *}"

if [ "$default_se" ]; then
	for VO in `echo $VOS | tr '[:lower:]' '[:upper:]'`; do
	    if [ "x`get_vo_param ${VO} DEFAULT_SE`" = "x" ]; then
	        set_vo_param ${VO} DEFAULT_SE $default_se
	    fi
	done
fi

####@ LCG_GFAL_INFOSYS
if [ "$BDII_LIST" ]; then
  yaimgridenv_set LCG_GFAL_INFOSYS $BDII_LIST
else
  yaimgridenv_set LCG_GFAL_INFOSYS $BDII_HOST:2170
fi

####@ PX_HOST
if [ "$PX_HOST" ]; then
yaimgridenv_set MYPROXY_SERVER $PX_HOST
fi

####@ Add PERL5LIB paths

#fix for EMI, no need to add GLITE_LOCATION perl paths
#for dir in ${LCG_LOCATION} ${GLITE_LOCATION}; do

#  for perl_dir in perl perl5; do

    # If this is a 64bit machine...
#    if [ "x${OS_ARCH}" = "x64BIT" ]; then
      # and there's 64bit middleware installed, add the 64bit paths ONLY.
#      if [ -d ${dir}/lib64/${perl_dir} ]; then
#        yaimgridpath_prepend PERL5LIB ${dir}/lib64/${perl_dir}
#      else
        # and there's 32bit middleware installed, add the 32bit paths ONLY.
#        if [ -d ${dir}/lib/${perl_dir} ]; then
#          yaimgridpath_prepend PERL5LIB ${dir}/lib/${perl_dir}
#        fi
#      fi
#    else
    # If this is a 32bit machine and there's 32bit middleware installed, add the 32bit paths ONLY.
#      if [ -d ${dir}/lib/${perl_dir} ]; then
#        yaimgridpath_prepend PERL5LIB ${dir}/lib/${perl_dir}
#      fi
#    fi
#  done
#done

####@ Add PYTHONPATH paths

YAIM_PYTHON_BASE=9.9
YAIM_PYTHON_VERSIONS=

#fix for EMI, no need to add GLITE_LOCATION python paths
#for dir in ${LCG_LOCATION} ${GLITE_LOCATION}; do
#  for i in 0 2.{3,4,5,6,7,8,9} 3.{0,1,2,3,4,5,6,7,8,9}; do

#    if [ $i = 0 ]; then
#      python_dir=python
#      final_dir=python
#    else
#      python_dir=python$i/site-packages

#    fi

    # If this is a 64bit machine...
#    if [ "x${OS_ARCH}" = "x64BIT" ]; then
      # and there's 64bit middleware installed, add the 64bit paths ONLY.
#      if [ -d ${dir}/lib64/${python_dir} ]; then
#        if [ $i != 0 ]; then
#          final_dir=\$${python_dir//./_}
#          [[ $YAIM_PYTHON_BASE > $i ]] && YAIM_PYTHON_BASE=$i
#          [[ " $YAIM_PYTHON_VERSIONS " =~ " $i " ]] || YAIM_PYTHON_VERSIONS="$YAIM_PYTHON_VERSIONS $i"
#        fi
#        yaimgridpath_prepend PYTHONPATH ${dir}/\$arch_dir/${final_dir}
#      else
        # and there's 32bit middleware installed, add the 32bit paths ONLY.
#        if [ -d ${dir}/lib/${python_dir} ]; then
#          if [ $i != 0 ]; then
#            final_dir=\$${python_dir//./_}
#            [[ $YAIM_PYTHON_BASE > $i ]] && YAIM_PYTHON_BASE=$i
#            [[ " $YAIM_PYTHON_VERSIONS " =~ " $i " ]] || YAIM_PYTHON_VERSIONS="$YAIM_PYTHON_VERSIONS $i"
#          fi
#          yaimgridpath_prepend PYTHONPATH ${dir}/lib/${final_dir}
#        fi
#      fi
#    else
      # If this is a 32bit machine and there's 32bit middleware installed,
      # add the 32bit paths ONLY.
#      if [ -d ${dir}/lib/${python_dir} ]; then
#        if [ $i != 0 ]; then
#          final_dir=\$${python_dir//./_}
#          [[ $YAIM_PYTHON_BASE > $i ]] && YAIM_PYTHON_BASE=$i
#          [[ " $YAIM_PYTHON_VERSIONS " =~ " $i " ]] || YAIM_PYTHON_VERSIONS="$YAIM_PYTHON_VERSIONS $i"
#        fi
#        yaimgridpath_prepend PYTHONPATH ${dir}/lib/${final_dir}
#      fi
#    fi
#  done
#done

#echo $YAIM_PYTHON_BASE > ${yaimb_temp}
#echo $YAIM_PYTHON_VERSIONS > ${yaimv_temp}

####@ Add dcache paths
#temporary until dCache becomes FHS compliant
#for UI_TAR,WN_TAR -  INSTALL_ROOT is defined in site-info.def
if ( ! echo "${NODE_TYPE_LIST}" | egrep -qi 'TAR' ); then
  INSTALL_ROOT=/opt
fi

if [ -d ${INSTALL_ROOT}/d-cache/srm/bin ]; then
    yaimgridpath_prepend PATH ${INSTALL_ROOT}/d-cache/srm/bin:${INSTALL_ROOT}/d-cache/dcap/bin
fi

if [ "x${OS_ARCH}" = "x64BIT" ]; then
 if [ -d ${INSTALL_ROOT}/d-cache/dcap/lib64 ]; then
  yaimgridpath_prepend LD_LIBRARY_PATH ${INSTALL_ROOT}/d-cache/dcap/lib64
 fi
 if [ -d ${INSTALL_ROOT}/d-cache/dcap/lib ]; then
  yaimgridpath_prepend LD_LIBRARY_PATH ${INSTALL_ROOT}/d-cache/dcap/lib
 fi
else
if [ -d ${INSTALL_ROOT}/d-cache/dcap/lib ]; then
    yaimgridpath_prepend LD_LIBRARY_PATH ${INSTALL_ROOT}/d-cache/dcap/lib
fi
fi

if [ -d ${INSTALL_ROOT}/d-cache/srm ]; then
    yaimgridenv_set SRM_PATH ${INSTALL_ROOT}/d-cache/srm 
fi

# UI/WN tarballs still need INSTALL_ROOT
if ( ! echo "${NODE_TYPE_LIST}" | egrep -qi 'TAR' ); then
 unset INSTALL_ROOT
fi

####@ Define default SE and SW_DIR
for VO in `echo $VOS | tr '[:lower:]' '[:upper:]'`; do
    vo=`echo $VO | sed -e 's/-/_/g' -e 's/\./_/g'`
    default_se=`get_vo_param ${VO} DEFAULT_SE`
    if [ "$default_se" ]; then
      yaimgridenv_set "VO_${vo}_DEFAULT_SE" $default_se
    fi
    mys=`echo "${NODE_TYPE_LIST}" | sed -e 's/[^ ]*UI//g' | sed -e 's/ //g'`
    if [ ! -z $mys ]  ; then
      sw_dir=`get_vo_param ${VO} SW_DIR`
      if [ "$sw_dir" ]; then
        yaimgridenv_set "VO_${vo}_SW_DIR" $sw_dir	    	
      fi
    fi
done

####@ VOBOX related variables
if [ "$VOBOX_HOST" ]; then
   if  [ "${GSSKLOG}x" == "yesx" ]; then	
      yaimgridenv_set GSSKLOG_SERVER $GSSKLOG_SERVER
   fi	
fi

####@ DPM_HOST
if [ "${DPM_HOST}" ]; then
    yaimgridenv_set DPNS_HOST $DPM_HOST
    yaimgridenv_set DPM_HOST $DPM_HOST
fi

####@ RFIO_PORT_RANGE
if [ "$RFIO_PORT_RANGE" ]; then
    yaimgridenv_set RFIO_PORT_RANGE \"${RFIO_PORT_RANGE/ /,}\"
fi

####@ Add lcg externals
# only UI_TAR or WN_TAR
# not needed in EMI
if ( echo "${NODE_TYPE_LIST}" | egrep -qi 'TAR' ); then
 if [ -d ${INSTALL_ROOT}/usr/bin ]; then
    yaimgridpath_prepend PATH ${INSTALL_ROOT}/usr/bin
 fi
fi
return 0

}
