#!/bin/sh


printCanonicalPath() # in $1 = path                                                    
{                                                                                      
local link                                                                         
local ret                                                                          
link="$1"                                                                          
if readlink -f / > /dev/null 2>&1; then                                            
readlink -f "$link"                                                            
else                                                                               
ret="$(cd $(dirname "$link"); pwd -P)/$(basename "$link")"                     
while [ -h "$ret" ]; do                                                        
link="$(readlink "$ret")"                                                  
if [ -z "${link##/*}" ]; then                                              
ret="${link}"                                                          
else                                                                       
link="$(dirname $ret)/${link}"                                         
ret="$(cd $(dirname "$link"); pwd -P)/$(basename "$link")"             
fi                                                                         
done                                                                           
echo "$ret"                                                                    
fi                                                                                 
}                                                                                      

if [ -z "$SRM_PATH" ]; then                                                            
SRM_PATH="$(cd "$(dirname "$(printCanonicalPath "$0")")/../share/srm"; pwd -P)"    
fi                                                                                     

if [ ! -d "$SRM_PATH" ]; then                                                          
echo "${SRM_PATH} is not a directory"                                              
exit 2                                                                             
fi                                                                                     

export SRM_PATH                                                                        


conf_dir=$SRM_PATH/conf

if [ "$1" = "-debug" ]; then
    logbackDefn="-Dlogback.configurationFile=$conf_dir/logback-all.xml"
    shift
else
    logbackDefn="-Dlogback.configurationFile=$conf_dir/logback.xml"
fi

if [ -n "$X509_USER_PROXY" ]; then
    x509_user_proxy="$X509_USER_PROXY"
elif [ -r /tmp/x509up_u$(id -u) ]; then
    x509_user_proxy=/tmp/x509up_u$(id -u)
else
   (echo "Could not find X.509 proxy credential."
    echo
    echo "Either create a proxy credential if one does not already exist or"
    echo "use the X509_USER_PROXY environment variable to specify the path"
    echo "if the proxy is in a non-standard location.") >&2
    exit 1
fi

if [ -n "$X509_CERT_DIR" ]; then
   x509_user_trusted_certs=$X509_CERT_DIR
elif [ -d "$HOME/.globus/certificates" ]; then
   x509_user_trusted_certs=$HOME/.globus/certificates
else
   x509_user_trusted_certs=/etc/grid-security/certificates
fi

CLASSPATH="$SRM_PATH/lib/*" exec java -Dlog=${DELEGATION_LOG:-warn} \
    -client \
    -Djava.awt.headless=true \
    -DwantLog4jSetup=n \
    "$logbackDefn" \
    -XX:+TieredCompilation \
    -XX:TieredStopAtLevel=1 \
    org.dcache.srm.shell.SrmShell -x509_user_proxy="$x509_user_proxy" -x509_user_trusted_certificates="$x509_user_trusted_certs" "$@"
