#!/bin/bash
###########################################################
# Version 1.0                                             #
# 11/2016 M.Ebert, mebert@uvic.ca mec6975@gmail.com       #
###########################################################
# Usage:                                                  #
# 1) copy script into directory                           #
#    that is in path and make it executable               #
#                                                         #
# 2) make sure the user that runs                         #
#    xrootd can reach DPM headnode                        #
#    through ssh                                          #
#                                                         #
# 3) change PREFIX in script                              #
#                                                         #
# 4) add following line to                                #
#    /etc/xrootd/xrootd-dpmdisk.cfg with correct Path:    #
#    xrootd.chksum adler32 /usr/bin/xrdcpadler32chksum.sh #
#                                                         #
# 5) restart xrootd                                       #
#                                                         #
# 6) try xrdcp with "adler32:print" checksum option       #
###########################################################

# Define local prefix used with DPM
PREFIX="/dpm/ecdf.ed.ac.uk/home"

# LFN to PFN conversion, needs dpm-dpns-to-disk tool
# If dpm-dpns-to-disk not installed locally, connect to one that has (here srm.glite which is DPM headnode)

prefixpresent=$(echo $1|grep -c "$PREFIX")
if [ "$prefixpresent" == "1" ];
then
 localfile=$(ssh root@srm.glite "dpm-dpns-to-disk $1|cut -d: -f2") #change hostname/user
else
 localfile=$(ssh root@srm.glite "dpm-dpns-to-disk ${PREFIX}$1|cut -d: -f2")  #change hostname/user
fi

#calculate checksum and store in array
# make sure xrdadler32 is installed and in path or use absolute path
chksumoutput=($(xrdadler32 $localfile))

#give out checksum which is in first field of array
echo ${chksumoutput[0]}

