##############################################################################
# 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_tar_user_env
#
# DESCRIPTION : This function configures 3.0 TAR user environment.
#
# AUTHORS :     Robert.Harakaly@cern.ch
#
# NOTES :
#
# YAIM MODULE:  glite-yaim-clients
#
##############################################################################


function config_tar_user_env () {

INSTALL_ROOT=${INSTALL_ROOT:-/opt}

if ( ! central_certs ); then
    x509_cert_dir=${INSTALL_ROOT}/etc/grid-security/certificates
else
    x509_cert_dir=${X509_CERT_DIR:-/etc/grid-security/certificates}
fi

if [ ! -d /etc/grid-security/vomsdir -a -d $INSTALL_ROOT/etc/grid-security/vomsdir ]; then
    x509_voms_dir=${INSTALL_ROOT}/etc/grid-security/vomsdir
else
    x509_voms_dir=${X509_VOMS_DIR:-/etc/grid-security/vomsdir}
fi

# If GLOBUS_TCP_PORT_RANGE is unset, give it a good default
GLOBUS_TCP_PORT_RANGE=${GLOBUS_TCP_PORT_RANGE:-"20000 25000"}

####################################
# sh
####################################

cat > $INSTALL_ROOT/etc/profile.d/grid_env.sh <<EOF

# Edit this line to reflect the mountpoint of your middleware
INSTALL_ROOT="$INSTALL_ROOT"
EOF

cat >> $INSTALL_ROOT/etc/profile.d/grid_env.sh <<'EOF'

# Leave the rest...

if [ "${LCG_ENV_SET}" ]; then return 0; fi

# Root directory for EDG software. (mandatory)
# Usual value: /opt/edg
EDG_LOCATION=${INSTALL_ROOT}/edg
# Directory for machine-specific files.
# Usual value: $EDG_LOCATION/var
EDG_LOCATION_VAR=$EDG_LOCATION/var
# World writable directory for temporary files. (mandatory)
# Usual value: /tmp
EDG_TMP=/tmp
EDG_WL_LOCATION=$EDG_LOCATION


# Usual value: /opt/lcg
LCG_LOCATION=${INSTALL_ROOT}/lcg
# Directory for machine-specific files.
# Usual value: $LCG_LOCATION/var
LCG_LOCATION_VAR=$LCG_LOCATION/var
# World writable directory for temporary files. (mandatory)
# Usual value: /tmp
LCG_TMP=/tmp

GLOBUS_LOCATION=${INSTALL_ROOT}/globus
#GLOBUS_CONFIG=/etc/globus.conf
GLOBUS_CONFIG=/dev/null

GPT_LOCATION=${INSTALL_ROOT}/gpt

GLITE_LOCATION=${INSTALL_ROOT}/glite
GLITE_LOCATION_VAR=${GLITE_LOCATION}/var

for i in ${INSTALL_ROOT}/etc/env.d/*.sh; do
       if [ -x $i ]; then
           . $i
       fi
done

LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${INSTALL_ROOT}/edg/externals/lib

if [ -d ${INSTALL_ROOT}/gcc-3.2.2/lib ]; then
        LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${INSTALL_ROOT}/gcc-3.2.2/lib
fi

RGMA_HOME="${INSTALL_ROOT}/glite"

export EDG_LOCATION EDG_LOCATION_VAR EDG_TMP EDG_WL_LOCATION LCG_LOCATION LCG_LOCATION_VAR LCG_TMP GLOBUS_LOCATION GLOBUS_CONFIG GPT_LOCATION GLITE_LOCATION GLITE_LOCATION_VAR LD_LIBRARY_PATH RGMA_HOME

EOF

cat >> $INSTALL_ROOT/etc/profile.d/grid_env.sh <<EOF

GLOBUS_TCP_PORT_RANGE="${GLOBUS_TCP_PORT_RANGE}"
export GLOBUS_TCP_PORT_RANGE

X509_CERT_DIR=$x509_cert_dir
X509_VOMS_DIR=$x509_voms_dir
export X509_CERT_DIR X509_VOMS_DIR
EOF

####################################
# csh
####################################

cat > $INSTALL_ROOT/etc/profile.d/grid_env.csh <<EOF

# Edit this line to reflect the mountpoint of your middleware
setenv INSTALL_ROOT "$INSTALL_ROOT"
EOF

cat >> $INSTALL_ROOT/etc/profile.d/grid_env.csh <<'EOF'

# Leave the rest...

if ( $?LCG_ENV_SET ) then
    return 0
endif

# Root directory for EDG software. (mandatory)
# Usual value: /opt/edg
setenv EDG_LOCATION ${INSTALL_ROOT}/edg
# Directory for machine-specific files.
# Usual value: $EDG_LOCATION/var
setenv EDG_LOCATION_VAR $EDG_LOCATION/var
# World writable directory for temporary files. (mandatory)
# Usual value: /tmp
setenv EDG_TMP /tmp
setenv EDG_WL_LOCATION $EDG_LOCATION


# Usual value: /opt/lcg
setenv LCG_LOCATION ${INSTALL_ROOT}/lcg
# Directory for machine-specific files.
# Usual value: $LCG_LOCATION/var
setenv LCG_LOCATION_VAR $LCG_LOCATION/var
# World writable directory for temporary files. (mandatory)
# Usual value: /tmp
setenv LCG_TMP /tmp

setenv GLOBUS_LOCATION ${INSTALL_ROOT}/globus
#setenv GLOBUS_CONFIG /etc/globus.conf
setenv GLOBUS_CONFIG /dev/null

setenv GPT_LOCATION ${INSTALL_ROOT}/gpt

setenv GLITE_LOCATION ${INSTALL_ROOT}/glite
setenv GLITE_LOCATION_VAR ${GLITE_LOCATION}/var

foreach i (${INSTALL_ROOT}/etc/env.d/*.csh)
       if ( -x $i ) then
           source $i
       endif
end

setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${INSTALL_ROOT}/edg/externals/lib

if ( -d ${INSTALL_ROOT}/gcc-3.2.2/lib ) then
        setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${INSTALL_ROOT}/gcc-3.2.2/lib
endif

setenv RGMA_HOME "${INSTALL_ROOT}/glite"

EOF

cat >>$INSTALL_ROOT/etc/profile.d/grid_env.csh <<EOF
setenv GLOBUS_TCP_PORT_RANGE "$GLOBUS_TCP_PORT_RANGE"

setenv X509_CERT_DIR $x509_cert_dir
setenv X509_VOMS_DIR $x509_voms_dir
EOF

if [ $UID -eq 0 ]; then
	echo "NOTE: If you want this to be your default middleware installation"
	echo "      please run the following command"
	echo "      ln -s $INSTALL_ROOT/etc/profile.d/grid_env.sh $INSTALL_ROOT/etc/profile.d/grid_env.csh /etc/profile.d"
else
	echo "IMPORTANT: You need to make sure that the correct grid_env.*sh file in $INSTALL_ROOT/etc/profile.d/,"
	echo "           is sourced when you log in so that your environment is set up correctly"
fi


}

