#!/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"
else
    x509_user_proxy=/tmp/x509up_u$(id -u)
fi

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