#! /bin/sh

#    Nov 07/2005 - F.Majaess (Protect "+" when it appears in the individual 
#                             filenames list input to "cmcarc")
#    Jun 04/2004 - F.Majaess

#id arc_rtrv- Transfers an individual file or extracts it from an "_arc" file.

#   AUTHOR  - F.Majaess

#hd PURPOSE - "arc_rtrv" code is used to either transfers an individual 
#hd           file or extracts it from an "_arc" file.
#hd           Note: This script is not intended to be called directly by
#hd                 the user, instead it's to be called by higher level
#hd                 scripts.

IFS=' '
export IFS
# set -x

#  * Set variable 'AWK' to the version of "awk" to be used.

AWK=${AWK:='awk'}

#  * Obtain the list of file(s) to be archived.

arg_list=$@
unset first_file rest_of_files
for arg in $arg_list
do
  case $arg in
       -*) set $arg                    ;;
      *=*) eval "$arg"                 ;;
        *) if [ -z "$first_file" ] ; then
            first_file="$arg"
           else
            rest_of_files="$rest_of_files $arg"
           fi 
  esac
done

arcvmid='cfs2'

Arc_extnsn=`basename $first_file | tr '[A-Z]' '[a-z]' | sed -n -e 's/^.*_arc$/Yes/p'`
if [ "$Arc_extnsn" != 'Yes' ] ; then
 lfil=`basename $first_file | tr '[A-Z]' '[a-z]'` &&
  CnFs='no' && CnFs=`pwd | sed -e 's/^\/cnfs\/.*$/yes/'` &&
  CeTs='no' && CeTs=`pwd | sed -e 's/^\/fs\/cetus\/.*$/yes/'` &&
  if [ "$CnFs" = 'yes' ] ; then
   Cwd=`pwd` &&
   Tfirst_file=`basename $first_file | tr '[A-Z]' '[a-z]'` &&
  #ssh datasvr "cd $Cwd; nrcp -b 4M $arcvmid:$first_file $Tfirst_file" 
   ssh $arcvmid "srcp -b 4M $first_file datasvr:${Cwd}/${Tfirst_file}"
  elif [ "$CeTs" = 'yes' ] ; then
   Cwd=`pwd` &&
   Tfirst_file=`basename $first_file | tr '[A-Z]' '[a-z]'` &&
  #ssh cetus "cd $Cwd; nrcp -b 4M $arcvmid:$first_file $Tfirst_file" 
   ssh $arcvmid "srcp -b 4M $first_file cetus:${Cwd}/${Tfirst_file}"
  else
   scp -o NoneSwitch=yes -p $arcvmid:$first_file `basename $first_file | tr '[A-Z]' '[a-z]'`
  fi
else
 # rsh $arcvmid -n cmcarc -o -f $first_file -x $rest_of_files | cmcarc -i -p -m -c -x
 # rsh $arcvmid -n cmcarc -o -f $first_file -x $rest_of_files | cmcarc -i -p -m -x
 # rsh $arcvmid -n cmcarc.64 -o -f $first_file -x $rest_of_files | cmcarc.64 -i -p -m -x
 # set -x
 
 rest_of_filesp=`echo $rest_of_files | sed -e "s/  *//g"` &&

 if [ -n "$rest_of_filesp" ] ; then

   ## Needed to precede '+' by '\' and protect such occurences (if present)

   rest_of_filesp=`echo $rest_of_files | sed -e 's/[\\]*//g' | sed -e 's/\+/\\\+/g' | sed -e "s/  */' '/g"` &&
   rest_of_filesp="'""$rest_of_filesp""'" &&
   CnFs='no' && CnFs=`pwd | sed -e 's/^\/cnfs\/.*$/yes/'` &&
   CeTs='no' && CeTs=`pwd | sed -e 's/^\/fs\/cetus\/.*$/yes/'` &&
   if [ "$CnFs" = 'yes' -o "$CeTs" = 'yes' ] ; then
     Cwd=`pwd` &&
     ssh $arcvmid -n "cd $Cwd; cmcarc -f $first_file -p -m -x $rest_of_filesp"
   else
     ssh $arcvmid -o NoneSwitch=yes -n "cmcarc -o -f $first_file -x $rest_of_filesp " | cmcarc.64 -i -p -m -x
   fi

 else

   CnFs='no' && CnFs=`pwd | sed -e 's/^\/cnfs\/.*$/yes/'` &&
   CeTs='no' && CeTs=`pwd | sed -e 's/^\/fs\/cetus\/.*$/yes/'` &&
   if [ "$CnFs" = 'yes' -o "$CeTs" = 'yes' ] ; then
     Cwd=`pwd` &&
     ssh $arcvmid -n "cd $Cwd; cmcarc -f $first_file -p -m -x $rest_of_files"
   else
     ssh $arcvmid -o NoneSwitch=yes -n "cmcarc -o -f $first_file -x $rest_of_files " | cmcarc.64 -i -p -m -x
   fi
   
 fi 
fi &&
exit || exit 1
