# 
# Copyright 1999-2010 University of Chicago
# 
# 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.
# 

#
# source this file to properly set up your environment for globus applications
# this requires that GLOBUS_LOCATION be set.
# GLOBUS_PATH will be set by this script to save the current location,
# should you decide to change GLOBUS_LOCATION to a different location and
# re source this script, the old GLOBUS_PATH information will be removed from
# your environment before applying the new GLOBUS_LOCATION
#

if ( ! $?GLOBUS_LOCATION ) then
    set prefix="/cvmfs/dirac.egi.eu/dirac/v8.0.45/Linux-x86_64"
else
    set prefix="$GLOBUS_LOCATION"
endif

set old=""
set exec_prefix='${prefix}'
while ( $exec_prefix != $old)
    set old="$exec_prefix"
    eval "set exec_prefix=$exec_prefix"
end

set old=""
set sbindir='${exec_prefix}/sbin'
while ( $sbindir != $old )
    set old="$sbindir"
    eval "set sbindir=$sbindir"
end

set old=""
set bindir='${exec_prefix}/bin'
while ( $bindir != $old )
    set old="$bindir"
    eval "set bindir=$bindir"
end

set old=""
set includedir='/cvmfs/dirac.egi.eu/dirac/v8.0.45/Linux-x86_64/include/globus'
while ( $includedir != $old )
    set old="$includedir"
    eval "set includedir=$includedir"
end

set old=""
set datarootdir='${prefix}/share'
while ( $datarootdir != $old )
    set old="$datarootdir"
    eval "set datarootdir=$datarootdir"
end

set old=""
set datadir='${datarootdir}'
while ( $datadir != $old )
    set old="$datadir"
    eval "set datadir=$datadir"
end

set old=""
set libexecdir='/cvmfs/dirac.egi.eu/dirac/v8.0.45/Linux-x86_64/share/globus'
while ( $libexecdir != $old )
    set old="$libexecdir"
    eval "set libexecdir=$libexecdir"
end

set old=""
set sysconfdir='${prefix}/etc'
while ( $sysconfdir != $old )
    set old="$sysconfdir"
    eval "set sysconfdir=$sysconfdir"
end

set old=""
set sharedstatedir='${prefix}/com'
while ( $sharedstatedir != $old )
    set old="$sharedstatedir"
    eval "set sharedstatedir=$sharedstatedir"
end

set old=""
set localstatedir='${prefix}/var'
while ( $localstatedir != $old )
    set old="$localstatedir"
    eval "set localstatedir=$localstatedir"
end

if (! $?GLOBUS_LOCATION ) then
    set old=""
    setenv GLOBUS_LOCATION '/cvmfs/dirac.egi.eu/dirac/v8.0.45/Linux-x86_64'
    while ( $GLOBUS_LOCATION != $old )
        set old="$GLOBUS_LOCATION"
        eval "setenv GLOBUS_LOCATION $GLOBUS_LOCATION"
    end
endif

if ( $?GLOBUS_PATH ) then
    setenv PATH `echo "${PATH}" | sed -e "s%:${GLOBUS_PATH}[^:]*%%g" -e "s%^${GLOBUS_PATH}[^:]*:\{0,1\}%%"`
    setenv LD_LIBRARY_PATH `echo "${LD_LIBRARY_PATH}" | sed -e "s%:${GLOBUS_PATH}[^:]*%%g" -e "s%^${GLOBUS_PATH}[^:]*:\{0,1\}%%"`
    setenv DYLD_LIBRARY_PATH `echo "${DYLD_LIBRARY_PATH}" | sed -e "s%:${GLOBUS_PATH}[^:]*%%g" -e "s%^${GLOBUS_PATH}[^:]*:\{0,1\}%%"`
    setenv LIBPATH `echo "${LIBPATH}" | sed -e "s%:${GLOBUS_PATH}[^:]*%%g" -e "s%^${GLOBUS_PATH}[^:]*:\{0,1\}%%"`
    setenv SHLIB_PATH `echo "${SHLIB_PATH}" | sed -e "s%:${GLOBUS_PATH}[^:]*%%g" -e "s%^${GLOBUS_PATH}[^:]*:\{0,1\}%%"`
    if ( $?MANPATH ) then
        setenv MANPATH `echo "${MANPATH}" | sed -e "s%:${GLOBUS_PATH}[^:]*%%g" -e "s%^${GLOBUS_PATH}[^:]*:\{0,1\}%%"`
    endif
    if ( $?PERL5LIB ) then
        setenv PERL5LIB `echo "${PERL5LIB}" | sed -e "s%:${GLOBUS_PATH}[^:]*%%g" -e "s%^${GLOBUS_PATH}[^:]*:\{0,1\}%%"`
    endif
endif

setenv PATH `echo "${PATH}" | sed -e "s%:${GLOBUS_LOCATION}[^:]*%%g" -e "s%^${GLOBUS_LOCATION}[^:]*:\{0,1\}%%"`
setenv LD_LIBRARY_PATH `echo "${LD_LIBRARY_PATH}" | sed -e "s%:${GLOBUS_LOCATION}[^:]*%%g" -e "s%^${GLOBUS_LOCATION}[^:]*:\{0,1\}%%"`
setenv DYLD_LIBRARY_PATH `echo "${DYLD_LIBRARY_PATH}" | sed -e "s%:${GLOBUS_LOCATION}[^:]*%%g" -e "s%^${GLOBUS_LOCATION}[^:]*:\{0,1\}%%"`
setenv LIBPATH `echo "${LIBPATH}" | sed -e "s%:${GLOBUS_LOCATION}[^:]*%%g" -e "s%^${GLOBUS_LOCATION}[^:]*:\{0,1\}%%"`
setenv SHLIB_PATH `echo "${SHLIB_PATH}" | sed -e "s%:${GLOBUS_LOCATION}[^:]*%%g" -e "s%^${GLOBUS_LOCATION}[^:]*:\{0,1\}%%"`
if ( $?MANPATH ) then
    setenv MANPATH `echo "${MANPATH}" | sed -e "s%:${GLOBUS_LOCATION}[^:]*%%g" -e "s%^${GLOBUS_LOCATION}[^:]*:\{0,1\}%%"`
endif
if ( $?PERL5LIB ) then
    setenv PERL5LIB `echo "${PERL5LIB}" | sed -e "s%:${GLOBUS_LOCATION}[^:]*%%g" -e "s%^${GLOBUS_LOCATION}[^:]*:\{0,1\}%%"`
endif


setenv GLOBUS_PATH "${GLOBUS_LOCATION}"
setenv PATH "${GLOBUS_LOCATION}/bin:${GLOBUS_LOCATION}/sbin:${PATH}";

if ( $?MANPATH ) then
    set DELIM
    if ( "X${MANPATH}" != "X" ) then
        set DELIM=:
    endif
    setenv MANPATH "${datadir}/man${DELIM}${MANPATH}"
endif

set matches=`ls -1d "$libexecdir/globus-script-initializer."* /dev/null | grep -v /dev/null`
if ($matches != "") then
    foreach arch_specific ($matches)
        if ( -f "$arch_specific" ) then
            eval "set `cat $arch_specific`"
            set old=""
            while ( $libdir != $old )
                set old="$libdir"
                eval "set libdir=$libdir"
            end

            set DELIM=
            if ( "X${LD_LIBRARY_PATH}" != "X" ) then
                set DELIM=:
            endif
            setenv LD_LIBRARY_PATH "${libdir}${DELIM}${LD_LIBRARY_PATH}"

            set DELIM=
            if ( "X${DYLD_LIBRARY_PATH}" != "X" ) then
                set DELIM=:
            endif
            setenv DYLD_LIBRARY_PATH "${libdir}${DELIM}${DYLD_LIBRARY_PATH}"

            set DELIM=
            if ( "X${LIBPATH}" != "X" ) then
                set DELIM=:
            endif
            setenv LIBPATH "${libdir}${DELIM}${LIBPATH}"

            set DELIM=
            if ( "X${SHLIB_PATH}" != "X" ) then
                set DELIM=:
            endif
            setenv SHLIB_PATH "${libdir}${DELIM}${SHLIB_PATH}"
        endif
    end
endif

set old=""
set perlmoduledir='${libdir}/perl'
while ( $perlmoduledir != $old )
    set old="$perlmoduledir"
    eval "set perlmoduledir=$perlmoduledir"
end

if ( ! $?LD_LIBRARY_PATH ) then
    setenv LD_LIBRARY_PATH ''
endif

if ( ! $?DYLD_LIBRARY_PATH ) then
    setenv DYLD_LIBRARY_PATH ''
endif

if ( ! $?LIBPATH ) then
    setenv LIBPATH '/usr/lib:/lib'
endif

if ( ! $?SHLIB_PATH ) then
    setenv SHLIB_PATH ''
endif

if ( -d ${perlmoduledir} ) then
    set DELIM
    if ( $?PERL5LIB ) then
        set DELIM=:
    else
        setenv PERL5LIB ''
        DELIM=:
    endif
    setenv PERL5LIB "${perlmoduledir}${DELIM}${PERL5LIB}"
endif
