#!/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                                                                        


##  haveSrm is a boolean variable that describes whether either of the
##  two transfer URLs involved in the transfer uses the 'srm' schema
##  (i.e., starts 'srm:').  A value of '0' indicates that neither URL
##  has the 'srm' schema; a value of '1' indicates that at least one
##  URL has the 'srm' schema.
haveSrm=0

##  delegationMakesSense is a boolean variable that describes whether
##  the user-supplied URLs suggest that delegation would be useful.  A
##  value of '0' indicates that delegation is not useful; a value of
##  '1' indicates that the transfer will likely require delegation.
delegationMakesSense=0

##  haveDelegate is a boolean variable that describes whether the user
##  has supplied the '-delegate' option.  A value of '0' indicates the
##  user is accepting the default value by not specifying the option;
##  a value of '1' indicates that the user specified the '-delegate'
##  option.
haveDelegate=0

for arg in "$@"; do
    case "${arg}" in
        -copyjobfile*)
            hasThirdPartyTransfer=0
            copyJobFile="${arg##-copyjobfile=}"
            if [ -r "$copyJobFile" ]; then
                hasThirdPartyTransfer=1
                grep -E -e '^ *(srm|gsiftp|gridftp|https):[^ ]* (srm|gsiftp|gridftp|https):' "$copyJobFile" >/dev/null || hasThirdPartyTransfer=0
            fi
            if [ $hasThirdPartyTransfer -eq 1 ]; then
                delegationMakesSense=1
            fi
            ;;

        srm:*)
            if [ "$haveSrm" = 0 ]; then
                haveSrm=1
            else
                delegationMakesSense=1
            fi
            ;;

        gsiftp:*|gridftp:*|https:*)
            delegationMakesSense=1
            ;;

        -delegate|-delegate=*)
            haveDelegate=1
            ;;
    esac
done


if [ "$delegationMakesSense" = 1 ] && [ "$haveDelegate" = 0 ]; then
    delegate=-delegate=true
fi

exec "${SRM_PATH}/lib/srm" -copy $delegate "$@"
