#!/bin/sh

if [ "$SRM_PATH" = "" ]
then
   command=`which $0`
   commanddir=`dirname $command`
   SRM_PATH=`dirname $commanddir`
   if [ -x $SRM_PATH/sbin/srm ]
   then
      export SRM_PATH
   else
      echo "cannot determine path to srm product, please define and export SRM_PATH enviroment variable" 1>&1
      exit 1
   fi
fi


args=$* 

function error() {
    echo "Can't copy from \"${1}\" protocol to \"${2}\"  protocol"
    exit 1
}

function check_protocols() {
    case "$1" in 
	gsiftp)
	    ;;
	enstore)
	    ;;
	dcap)
	    ;;
	file)
	    ;;
	*)
	    echo "Unsupported protocol: \"$1\""
	    exit 1
    esac
}

skip=0
i=0
fargs=()

for arg in $args;
  do
  case "${arg}" in
      -help|-version|--help|-copyjobfile*)
	  skip=1
	  continue
	  ;; 
      srm:*)
	  skip=1
	  continue
	  ;;
      *=*|-*)
	  continue
	  ;;
  esac
  fargs[i]=${arg}
  let i=i+1
done


#
# if any of the input args contain srm or help - fall through to standard java call
#

length=${#fargs[*]}
let last=length-1
first=${#fargs[0]}

cmd=""
if [[ "${skip}" -eq 0 ]] 
    then
    if [ ${length} -lt 2 ]
	then
	echo "Please specify source and destination"
	exit 1
    else
	i=0
	src_protocols=()
	src_files=()
	while [ $i -lt ${last} ] 
	do
	  src_protocols[$i]=`echo ${fargs[$i]} | grep ":" | cut -d":" -f1`
	  let i=i+1
	done
	n_src_protocols=`echo  ${src_protocols[@]} | tr A-Z a-z | tr ' ' '\012' | sort | uniq | wc -l`
	if [  ${n_src_protocols} -ne 1 ] 
	    then
	    echo "Wrong number of input protocols:  ${n_src_protocols}"
	    echo "    0 - check validity of URL spelling"
	    echo "   >1 - mixture of protocols is not supported "
	    exit 1
	else
	    src_protocol=${src_protocols[0]}
	    dst_protocol=`echo ${fargs[${last}]} | grep ":" | cut -d":" -f1`
	    check_protocols ${src_protocol}
	    check_protocols ${dst_protocol}
	    if [ "${src_protocol}" = "${dst_protocol}" ] 
		then
		case "${src_protocol}" in 
		    gsiftp)
			cmd="globus-url-copy ${fargs[*]}"
			;;
		    file)
			cmd="cp `echo ${fargs[*]} | sed -e "s/${src_protocol}://g"`"
			;;
		    *)
			error ${src_protocol}  ${dst_protocol} 
		esac
	    else

		if [[ "${src_protocol}" = "gsiftp" || "${dst_protocol}" = "gsiftp" ]] 
		    then
		    cmd="globus-url-copy ${fargs[*]}"
		fi

		if [[ "${src_protocol}" = "enstore" || "${dst_protocol}" = "enstore" ]] 
		    then
		    cmd="encp `echo ${fargs[*]} | sed -e "s/${src_protocol}://g" | sed -e "s/${dst_protocol}://g"`"
		fi


		if [[ "${src_protocol}" = "dcap"  ||  "${dst_protocol}" = "dcap" ]]
		    then
		    if [ "${src_protocol}" = "file" ] 
			then
			cmd="dccp `echo ${fargs[*]:0:${last}} | sed -e "s/${src_protocol}://g"` ${fargs[*]:${last}}"
		    else
			cmd="dccp `echo ${fargs[*]:0:${last}}` `echo ${fargs[*]:${last}} | sed -e "s/${dst_protocol}://g"`"
		    fi
		fi

		#
		# all bad cases go here
		#

		if [[ "${src_protocol}" = "gsiftp" && "${dst_protocol}" = "enstore" ]] 
		    then
		    error ${src_protocol} ${dst_protocol}
		fi

		if [[ "${dst_protocol}" = "gsiftp" && "${src_protocol}" = "enstore" ]] 
		    then
		    error ${src_protocol} ${dst_protocol}
		fi
		

		if [[ "${src_protocol}" = "dcap" && "${dst_protocol}" = "gsiftp" ]] 
		    then
		    error ${src_protocol} ${dst_protocol}
		fi

		if [[ "${dst_protocol}" = "dcap" && "${src_protocol}" = "gsiftp" ]] 
		    then
		    error ${src_protocol} ${dst_protocol}
		fi

		if [[ "${src_protocol}" = "dcap" && "${dst_protocol}" = "enstore" ]] 
		    then
		    error ${src_protocol} ${dst_protocol}
		fi

		if [[ "${dst_protocol}" = "dcap" && "${src_protocol}" = "enstore" ]] 
		    then
		    error ${src_protocol} ${dst_protocol}
		fi
	    fi
	    $cmd
	    rc=$?
	    exit $rc
	fi
    fi
else 
    $SRM_PATH/sbin/srm -copy $*
fi
