 
#    Feb 01/18 - F.Majaess (Revised for Science)
#    Jan 07/09 - F.Majaess (Revised for af/alef)
#    May 14/08 - F.Majaess (Revised for sa/saiph, ib/dorval-ib)
#    Oct 16/06 - F.Majaess (Revised for ma/maia)
#    Mar 01/06 - F.Majaess (Revised for rg/rigel)
#    Jun 04/04 - F.Majaess (Revised "cmcarc" mode)
#    Jun 20/03 - F.Majaess (Revised for az/azur)
#    Feb 25/02 - F.Majaess (Revised for ya/yata)
#    Jan 05/00 - F.Majaess (Added kz/kaze)
#    Jan 21/99 - F.Majaess (Added yo/yonaka)
#    May 01/98 - F.Majaess (Disable crd/svcrd/repfile switches use)
#    Aug 13/97 - F.Majaess (Revise for sx/hiru)
#    Mar 25/96 - F.Majaess (Revise for sx4)
#    Jan 08/95 - F.Majaess (Enforce aborting on unspecified filename 
#                           parameter)
#    Jun 22/95 - F.Majaess (Revise for Orion)
#    JAN 31/95 - F.Majaess (Add 'cfs' option)
#    DEC 05/94 - F.Majaess (Added dvmode to set subdirectory permission)
#    Nov 14/94 - F.Majaess (Revise for sx3r)
#    MAY 11/94 - F.Majaess (Modified to accomodate Victoria node)
#    JAN 24/94 - F.Majaess (change "RUNPATH" to "CCRNTMP")
#    OCT 15/93 - F.Majaess (Modified for long list of files)
#    OCT 12/93 - F.Majaess (change "DATAPATH" to "RUNPATH")
#    APR 23/93 - F.Majaess (fix tape duplication)
#    JAN 19/93 - F.Majaess (revise to stripp off padded data)
#    NOV 09/92 - F.Majaess (adjust to accomodate tape writing)
#    OCT 27/92 - F.Majaess
 
#id taprsrv - Used for tape reservation and function definitions.
 
#   AUTHOR  - F.Majaess
 
#hd PURPOSE - "taprsrv" script is used to reserve a tape, and define 
#hd           functions for tape utilities as well as creating a
#hd           temporary subdiretory to hold loaded files from tape
#hd           (if necessary). 
 
#pr  PARAMETERS:
#pr
 
#ex  EXAMPLE: 
#ex  

#  diagnostic codes...

#  echo $@  
set +x

#  * Set variable 'AWK' to the version of "awk" to be used.
 
AWK=${AWK:='awk'}

# Usr=`whoami |  sed 's/^....//'`
 Lgname=`whoami`
 LgnameEC=`accmap $Lgname`
 usr=`echo $LgnameEC |  sed 's/^....//'`
 grp=`echo $LgnameEC |  sed 's/...$//'`
 Usr=`echo $LgnameEC |  sed 's/^....//'`

Diagdir=`pwd` ; export Diagdir
if samefs "$Diagdir" "$CCRNTMP" ; then  
 :
else
 echo "" ; echo "" ; echo " taprsrv: Sorry, current working subdirectory of ${Diagdir} is not what is expected! ;  CCRNTMP=${CCRNTMP} " ; echo "" 
 if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
   echo "TAPRSRV;`whoami`,`date`,`hostname`,SEVERE-$$; wrong working subdirectory : Diagdir=$Diagdir, CCRNTMP=$CCRNTMP !" >> $CCRNSRC/info/.cfs_problems_log
 fi 
 abort='abort' 
  exit 1
fi
sdfltifs () { IFS=' '   ; export IFS ; } 
sdiagdir () { cd $Diagdir ; } 
sdiagifs () { IFS='[,]' ; export IFS ; }
Tstamp=`date +%Y%j%H%M%S`
Tstamp="${host}_${nqsprfx}${jobname}${nqsext}_${Tstamp}"
# Tpid="$$_$Tstamp"
Tpid="$$"
## Disable "RL" part and ensure always it's the case
## by setting "cfs=yes"
## cfs=${cfs='no'}
cfs='yes'
arcvmid='hpnls'
ArcRootPath='/archive/eccc/crd'
ArcSrvr_prfx='hpnl'

if [ -n "$mscan" ] ; then
 mscan=`echo $mscan | tr '[A-Z]' '[a-z]'` ;
 if [ "$mscan" = 'p1' ] ; then
  mscan='eccc_ppp1'
 elif [ "$mscan" = 'p2' ] ; then
  mscan='eccc_ppp2'
 elif [ "$mscan" = 'p3' ] ; then
  mscan='eccc_ppp3'
 elif [ "$mscan" = 'p4' ] ; then
  mscan='eccc_ppp4'
 elif [ "$mscan" = 'mz' ] ; then
  mscan='mez'
 fi
fi

tapexit () {
##   if test "$cfs" != 'yes'  -o "$mode" = 'convert'
##    then
##    (rlvrelease t$Tpid || : ) ;
##    (rlfree t$Tpid | sed -n -e 's/^/ /p' || : ) ;
##    fi ;
  case $1 in 
   0) : ;;
   *) abort='abort' ;
      if test -z "$Taperr_msg"  
       then 
        echo "" ; echo "" ; echo "  tapexit: abnormal termination $1 " ; echo ""
       else 
        echo "" ; echo "" ; echo "  tapexit: $Taperr_msg " ; echo ""  ;
        if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
         echo "TAPEXIT;...:`whoami`,`date`,`hostname`,......-$$; $Tmppath/$Tmpdir ; $Taperr_msg !" >> $CCRNSRC/info/.hpnls_problems_log
        fi ;
       fi ;
 #     set -x ;
       if test -d "$Tmppath/$Tmpdir/."
       then
 #      if test \( "$mscan" != 'vic' -a "$mscan" != 'svvic' \) -a \( "$invoker_deck" = 'othr2fe' -o "$invoker_deck" = 'fe2othr' -o "$invoker_deck" = 'becdata' \)
        if test "$invoker_deck" = 'fe2othr' -o \( "$mscan" != 'vic' -a "$mscan" != 'svvic' -a \( "$mode" != 'read' -o \( "$mode" = 'read' -a \( "$invoker_deck" = 'othr2fe' -o "$invoker_deck" = 'becdata' \) \) \)  -a "$mode" != 'convert' -a "$mode" != 'copy' \) 
         then
          set -x ;
          cd $Tmppath ;
          df -k $Tmppath ;
          ls -alt $Tmppath ;
          ( \rm -rf $Tmpdir || : ) ;
         else
          echo "" ; echo "" ; echo "  tapexit: DO NOT FORGET to delete $Tmppath/$Tmpdir before resubmitting " ; echo "" 
          set -x ;
          df -k $Tmppath ;
          ls -alt $Tmppath ;
         fi ;
       else
         set -x ;
         df -k $Tmppath ;
         ls -alt $Tmppath ;
       fi ;
       sdiagdir ;
 #     : ;;
       (exit $1) ;;
  esac ;
     } 
 
#  *  check if switches/parameters specified on script call
 
if test  $# -gt 0  ; then
  
#  * put argument list in "prmtrl" ...
   
  prmtrl=$@
  prmtrl=`echo $prmtrl | tr '[A-Z]' '[a-z]'`
  
#  * for each argument specified do...
    
  for prmtr in $prmtrl
  do
    case $prmtr in
 
#   * check for standard shell switches and set them accordingly
 
     --|-|[-+][aefhkntuvx]) set `echo $prmtr ` ;;
 
#   * case where a parameter is to be assigned a value
 
    *=*   ) eval $prmtr ;;
 
#   * anything else case skip...
 
    *  )  tprmtr="$tprmtr $prmtr";;
 
    esac
  done
  prmtrl=$tprmtr
  tprmtr=
 
fi
prmtrl=${prmtrl=}

#   * Ignore unused parameters... 
 
if test  -n "$prmtrl"  ; then
  
  echo "" ; echo "" ; echo " taprsrv: ignored $prmtrl !" ; echo ""

fi
 
# Tmppath=${Tmppath=$CCRNTMP}
Tmppath=${Tmppath="$SITESTORE_ROOT/../ccrn_tmp"}

# ensure cfs option is not set to on in tapcopy or tapconv job.
 
if test \( "$mode" = 'copy' -o "$mode" = 'convert' \) -a \( "$cfs" = 'yes' -o "$hpnls" = 'yes' \)  ; then
 echo "" ; echo "" ; echo " taprsrv: Sorry, you can not set cfs switch in tapcopy or tapconv job " ; echo "" 
 abort='abort' 
  exit 1
fi
 
# Disable the use of "crd/svcrd/repfile"...
 
if test "$crd" = 'on' -o "$svcrd" = 'on' -o "$mscan" = 'crd' -o "$mscan" = 'svcrd'  ; then
 echo "" ; echo "" ; echo " taprsrv: Sorry, svcrd/crd switches are no longer valid, please modify and resubmit " ; echo "" 
 abort='abort' 
  exit 1
fi

if test "$vic" = 'on' -o "$svvic" = 'on' -o "$mscan" = 'vic' -o "$mscan" = 'svvic'  ; then
 echo "" ; echo "" ; echo " taprsrv: Sorry, svvic/vic switches are not supported, please modify and resubmit " ; echo "" 
 abort='abort' 
  exit 1
fi

if test "$repfile" = 'on' -o "$rep" = 'rep' ; then
 echo "" ; echo "" ; echo " taprsrv: Sorry, repfile=on in not valid anymore, please modify and resubmit " ; echo "" 
 abort='abort' 
  exit 1
fi
HOSTIDf=`echo $HOSTID | sed -e 's/eccc.*-ppp/cs/g' -e 's/^ppp/cs/g' | cut -c 1-3`
if test \( "$besc" = 'on' -o "$mscan" = 'besc' \) ; then
 echo "" ; echo "" ; echo " taprsrv: Sorry, besc=on mode is not supported, please modify and resubmit " ; echo "" 
 abort='abort' 
  exit 1
fi
# # if test \( "$besc" = 'on' -o "$mscan" = 'besc' \) -a \( "$OS" != 'AIX' -o "$mode" != 'write' \) ; then
# if test \( "$besc" = 'on' -o "$mscan" = 'besc' \) -a \( \( "$HOSTIDf" != 'xc1' -a "$HOSTIDf" != 'xc2' \)  -o "$mode" != 'write' \) ; then
#  echo "" ; echo "" ; echo " taprsrv: Sorry, besc=on mode is only allowed for tdumper job running on one of the back-end clusters, please modify and resubmit " ; echo "" 
#  abort='abort' 
#   exit 1
# fi

if test \( "$besc" = 'on' -o "$mscan" = 'besc' \) -a \( "$svsave" = 'on' -o "$svsave" = 'yes' \) ; then
 echo "" ; echo "" ; echo " taprsrv: Sorry, not allowed to use svsave=on with besc=on targeting the job to run on one of the back-end clusters, please modify and resubmit " ; echo ""
 Taperr_msg="Sorry, not allowed to use svsave=on with besc=on targeting the job to run on one of the back-end clusters, please modify and resubmit "
 abort='abort' 
  exit 1
fi

# ensure all used parameters without a value are set to the appropriate
# default value...
 
 
if test "$cfs" = 'yes' -o "$mode" = 'convert' ; then
 if test "$mode" != 'convert' ; then
  tape=".cfs"
 else
  tape=${tape='000000'}
 fi
 tapback=".cfs"
else
 tape=${tape='000000'}
 tapback=${tapback='000000'}
fi
if [ "$mscan" = 'cidsc03' ] ; then
  unset mscan
fi
if [ "$besc" = 'on' -o "$mscan" = 'besc' ] ; then
 
   HOSTIDf=`echo $HOSTID | sed -e 's/eccc.*-ppp/cs/g' -e 's/^ppp/cs/g' | cut -c 1-3`
   case $HOSTIDf in
    xc1) bemchn='hare' ;;
    xc2) bemchn='brooks' ;;
    xc3) bemchn='banting' ;;
    xc4) bemchn='daley' ;;
      *) echo " taprsrv: unsupported platform `hostname` !" && echo "" && abort='abort' && ( exit 2) ;;
   esac
   mscan='besc'
   # Intentionally block jobs.
   # BloCk='yes'
   # if [ "$BloCk" = 'yes' ] ; then
   #  echo "" ; echo "" ; echo " taprsrv: Sorry, hpnls is not fully functional. Please retry later on." ; echo ""
   #  Taperr_msg="Sorry, hpnls is not fully functional. Please retry later on."
   #  abort='abort' 
   #  exit 1
   # fi
   # Ensure CFS functionality
   unset hstnm
   Ntimes=5
   hstnm=`ssh $arcvmid hostname 2>/dev/null | cut -c 1-4 `
   hststrgtdirck=`ssh $arcvmid hsmls -1d ${ArcRootPath}/ccrn`
   while [ \( "$hstnm" != "${ArcSrvr_prfx}" -o "$hststrgtdirck" != '${ArcRootPath}/ccrn' \) -a "$Ntimes" -gt 0 ]
    do
     Tmstmp=`date`
     echo "" ; echo "" ; echo "  archive: archive server is not fully functional ~ $Tmstmp ! " ; echo ""
     sleep 90
     Ntimes=`expr $Ntimes - 1`
     unset hstnm hststrgtdirck
     hstnm=`ssh $arcvmid hostname 2>/dev/null | cut -c 1-4 `
     hststrgtdirck=`ssh $arcvmid hsmls -1d ${ArcRootPath}/ccrn`
    done
   if [ "$hstnm" != "${ArcSrvr_prfx}" -o "$hststrgtdirck" != '${ArcRootPath}/ccrn' ] ; then
    echo "" ; echo "" ; echo " taprsrv: Sorry, hpnls is not fully functional. Please retry later on." ; echo ""
    Taperr_msg="Sorry, hpnls is not fully functional. Please retry later on."
    abort='abort' 
    exit 1
   fi
else
  if [ "$HDNODE" = 'eccc-ppp1' ] ; then
    bemchn='hare'
  elif [ "$HDNODE" = 'eccc-ppp2' ] ; then
    bemchn='brooks'
  elif [ "$HDNODE" = 'eccc-ppp3' ] ; then
    bemchn='banting'
  elif [ "$HDNODE" = 'eccc-ppp4' ] ; then
    bemchn='daley'
  else
   bemchn=${bemchn:-'cidsc03'}
  fi
  
  # . massage_be_destination
  mscan=${mscan=${bemchn}}
  mscan=`echo $mscan | tr '[A-Z]' '[a-z]'` ;
  # if [ "$mscan" = 'zeta' ] ; then
  #  ssh zeta hostname || mscan='saiph'
  #  bemchn="$mscan"
  # fi
  if [ "$mscan" = 'hare' ] ; then
   ssh hare hostname >> /dev/null || echo 'Hare seems unaccessible'
   bemchn="$mscan"
  elif [ "$mscan" = 'brooks' ] ; then
   ssh brooks hostname >> /dev/null || echo 'Brooks seems unaccessible'
   bemchn="$mscan"
  elif [ "$mscan" = 'banting' ] ; then
   ssh banting hostname >> /dev/null || echo 'XC3 seems unaccessible'
   bemchn="$mscan"
  elif [ "$mscan" = 'daley' ] ; then
   ssh daley hostname >> /dev/null || echo 'Daley seems unaccessible'
   bemchn="$mscan"
  fi
  case $mscan in
    #hare|brooks|eccc-ppp1|eccc-ppp2|both|svvic|vic) : ;;
    #hare|brooks|eccc-ppp1|eccc-ppp2|both) : ;;
    #hare|brooks|eccc-ppp1|eccc-ppp2|both) : ;;
     hare|brooks|daley|banting|eccc-ppp1|eccc-ppp2|eccc-ppp3|eccc-ppp4|both) : ;;
     *        ) echo "" && echo "" && echo " taprsrv: illegal destination $mscan setting!" && echo "" && abort='abort' && ( exit 2) ;;
  esac
fi
#
mode=${mode='read'}
if [ "$mode" = 'read' ] ; then
 if [ -n "$arcfile" -a "$arcfile" = 'none' ] ; then
  noarcmode='yes'
 fi
fi
vmode=${vmode='640'}
dvmode=${dvmode='750'}
 
if test "$tape" = "000000" -o "$tape" = "$tapback"  ; then
 inv_tap='yes'
else
 inv_tap='no'
fi
if test \( "$cfs" != 'yes' -o "$mode" = 'convert' \) -a "$inv_tap" = 'yes' ; then
 echo "" && echo "" && echo " taprsrv: a valid tape number must be specified ? " && echo "" &&
 abort='abort' &&
 (exit 1)
else
#
echo "" && eval "echo '0 taprsrv: PID = $Tpid '" &&
# 
# Tmppath=$CCRNTMP &&
Tmppath=${Tmppath="$SITESTORE_ROOT/../ccrn_tmp"} &&
CnFs='no' &&
CeTs='no' &&
byFreeSpace=${byFreeSpace='on'} &&
if [ "$byFreeSpace" = 'on' ] ; then
 # Force the job to sleep if the treshold exceeded for $CCRNTMP.
 unset FreeSpace &&
 FreeSpace=`df -Pm $Tmppath | tail -1 | awk '{ print $4 ; }' ` &&
 # echo "FreeSpace=$FreeSpace" &&
 FreeSpace=${FreeSpace=0} &&
 while test $FreeSpace -lt 20000
 do
  echo "" && echo "" && echo " taprsrv: waiting, free disk space usage is $FreeSpace MB (< 20GB) on $Tmppath ~ `date` " && echo "" &&
  sleep 180 &&
  unset FreeSpace &&
  FreeSpace=`df -Pm $Tmppath | tail -1 | awk '{ print $4 ; }' ` 
  done 
else
 # Force the job to sleep if the treshold exceeded for $CCRNTMP.
 unset DskSpace &&
 # DskSpace=`df $Tmppath | grep '%' | tail -1 | awk 'BEGIN {} { print $6 ; } END {} '| sed -e 's/%//g' ` &&
 DskSpace=`df -P $Tmppath | grep '%' | tail -1 | awk 'BEGIN {} { print $5 ; } END {} '| sed -e 's/%//g' ` &&
 # echo "DskSpace=$DskSpace" &&
 DskSpace=${DskSpace=100} &&
 while test $DskSpace -gt 90  
 do
  echo "" && echo "" && echo " taprsrv: waiting, disk space usage is $DskSpace % on $Tmppath ~ `date` " && echo "" &&
  sleep 180 &&
  unset DskSpace &&
  # DskSpace=`df $Tmppath | grep '%' | tail -1 | awk 'BEGIN {} { print $6 ; } END {} '| sed -e 's/%//g' `
  DskSpace=`df -P $Tmppath | grep '%' | tail -1 | awk 'BEGIN {} { print $5 ; } END {} '| sed -e 's/%//g' `
  done 
fi &&
#!!!!!
if [ "$SITE_ID" = 'Dorval' ] ; then
  dpalist=${dpalist:='on'} &&
  export dpalist
fi &&
if test  "$mode" = "write" -o "$mode" = "copy" -o "$mode" = 'convert'  ; then
#if test "$mode" = "write" -a "$masterdir" = 'on'  ; then
# trnsfrchksum='yes'
#fi &&
 # Forced safer settings: 
 mscan="$HDNODE" &&
 if test "$mscan" = 'eccc-ppp1' -o "$mscan" = 'eccc-ppp2' -o "$mscan" = 'eccc-ppp3' -o "$mscan" = 'eccc-ppp4' ; then
  :
 else
  echo " taprsrv: Error, invalid mscan=$mscan setting!" &&
  abort=abort &&
  (exit 1)
 fi &&
 tape='hpnls' &&
 #------

 if test "$mode" = "write" ; then
  notrnsfrchksum=${notrnsfrchksum:-'no'}
 fi &&
 if [ "$notrnsfrchksum" = 'on' ] ; then
  notrnsfrchksum='yes'
 elif [ "$notrnsfrchksum" = 'off' ] ; then
  notrnsfrchksum='no'
 fi &&
 export notrnsfrchksum && 
 
 getfile () { 
  unset vol pdn id save && 
  eval "$@" &&
  if [ -n "$pdn" ] ; then
   Updn=`echo $pdn | tr '[A-Z]' '[a-z]'` &&
   touch $Updn 
  else
   echo " taprsrv,getfile: Error, filename must be passed via pdn parameter " &&
   abort=abort &&
   (exit 1) 
  fi || (exit $?) ;
     } &&
 if test "$mscan" = 'vic' -o "$mscan" = 'svvic' ; then
 #VICTmppath=${RMTRUNPATH:=`rsh $VICSV01 'echo $RUNPATH'`}
  VICTmppath=${RMTRUNPATH:=`ssh $VICSV01 'echo $RUNPATH' 2>>/dev/null`} &&
  if [ -z "$VICTmppath" ] ; then
   echo " taprsrv: Sorry, unable to set VICTmppath!" &&
   abort='abort' &&
   (exit 1)
  fi
 fi &&
 BETmppath=$BERUNPATH &&
 ORTmppath=$ORRUNPATH &&
 sdfltifs  &&
 if test  "$mode" = "write"  ; then
  if test "$invoker_deck" = 'fe2othr' ; then
   Tmpdir="tmp.t.fe2othr_${Usr}_${Tpid}_${Tstamp}"
  elif test "$invoker_deck" = 'othr2fe' ; then
   Tmpdir="tmp.t.othr2fe_${Usr}_${Tpid}_${Tstamp}"
  elif test "$invoker_deck" = 'becdata' ; then
   Tmpdir="tmp.t.becdata_${Usr}_${Tpid}_${Tstamp}"
  else
   Tmpdir="tmp.w.${tape}_${Usr}_${Tpid}_${Tstamp}" &&
   (\cp `which writape` $Diagdir && chmod u+w $Diagdir/writape || : ) &&
   (\cp `which archive` $Diagdir && chmod u+w $Diagdir/archive || : ) &&
   # (\cp `which arc_fnm` $Diagdir && chmod u+w $Diagdir/arc_fnm || : ) &&
   # (\cp `which arc_fmt` $Diagdir && chmod u+w $Diagdir/arc_fmt || : ) &&
   (\cp `which arc_shrd` $Diagdir && chmod u+w $Diagdir/arc_shrd || : ) &&
   # (\cp `which arc_lst` $Diagdir && chmod u+w $Diagdir/arc_lst || : ) &&
   (\cp `which taprlse` $Diagdir && chmod u+w $Diagdir/taprlse || : ) &&
   # (\cp `which cmcarc.64` $Diagdir && chmod u+w $Diagdir/cmcarc.64 || : ) &&
   # ls -al $Diagdir &&
   PATH=`clnstrng ".:$Diagdir:$PATH" ':'` &&
   hash -r 
  fi &&
## 
 Cache_sync_file="./.taprsrv_cache_sync_""`date -u '+%Y%j%H%M%S%s'`""_$$" &&
 export Cache_sync_file &&
 if [ "$HDNODE" = 'hare' -o "$HDNODE" = 'brooks' -o "$HDNODE" = 'banting' -o "$HDNODE" = 'daley' ] ; then 
    HOSTIDf=`echo $HOSTID | sed -e 's/eccc.*-ppp/cs/g' -e 's/^ppp/cs/g' | cut -c 1-3` &&
    case $HOSTIDf in
     xc1) bemchn2='hare' ;;
     xc2) bemchn2='brooks' ;;
     xc3) bemchn2='banting' ;;
     xc4) bemchn2='daley' ;;
       *) echo " taprsrv: unsupported platform `hostname` !" && echo "" && abort='abort' && ( exit 2) ;;
    esac
 fi &&
 obtnfl () {
  echo "" && echo "  obtnfl : $@" &&
  set +x &&
  : &&
  if test  "$2" != "A" ; then
   Ufln=`echo $1 | tr '[a-z]' '[A-Z]'` &&
   lfln=`echo $1 | tr '[A-Z]' '[a-z]'` &&
   set -x &&
  #if test "$4" = "saiph" -o "$4" = "maia" -o "$4" = "ftp1" ; then
  # if test "$4" = "saiph" -o "$4" = "maia" ; then
   if test "$4" = "hare" -o "$4" = "brooks" -o "$4" = "daley" -o "$4" = "banting" -o "$4" = "ftp1" ; then
    if test "$4" = "hare" -o "$4" = "brooks" -o "$4" = "daley" -o "$4" = "banting" ; then
     Xpctsz=`fgrep "$2" .tbeflist | $AWK 'BEGIN { } { print \$5 ; } END {} '` &&
     Cwd=`pwd` &&
     scp -p -o NoneSwitch=yes $4:$3/$2 $Ufln &&
     chmod ug+r $Ufln && chmod u+w $Ufln &&
     if test "$data_transfer" = 'yes' -a "$delet_files" = 'yes' ; then
      Fln=`echo $2 | $AWK -F '.' 'BEGIN { } { print \$1 ; } END {} '` &&
      Edn=`echo $2 | $AWK -F '.' 'BEGIN { } { print \$2 ; } END {} ' | sed -e 's/^0*//'` &&
      touch .obtnfl_be_list &&
      if test -n "$DestNtn" ; then
       unset DestNtn
      fi &&
      if test -s ".obtnfl_be_list"  ; then
       DestNtn=`cat .obtnfl_be_list | egrep '^ mdest=' | head -1` 
      fi &&
      if test \( ! -s ".obtnfl_be_list" \) -o \( -s ".obtnfl_be_list" -a "$DestNtn" = " mdest=$4" \) ; then
       echo " mdest=$4" >> .obtnfl_be_list &&
       echo " access delet_file $Fln ed=$Edn nocp na" >> .obtnfl_be_list &&
       echo ' delete delet_file na' >> .obtnfl_be_list &&
       echo ' release delet_file' >> .obtnfl_be_list
      else
       echo " delet $Fln ed=$Edn mdest=$4 " >> .obtnfl_be_list
      fi
     fi
    else
     if [ "$HDNODE" = 'hare' -o "$HDNODE" = 'brooks' -o "$HDNODE" = 'banting' -o "$HDNODE" = 'daley' ] ; then
      CmdLine="ssh ftp1.cccma.ec.gc.ca '""cd $3; ls -ldL $2""'" &&
      Xpctsz=`ssh $bemchn2 "$CmdLine" | tail -1 | $AWK 'BEGIN { } { print \$5 ; } END {} '` &&
      CwdLnx=`pwd` &&
      ssh $bemchn2 "cd $CwdLnx ; scp -p ftp1.cccma.ec.gc.ca:$3/$2 $Ufln"
     else
      CmdLine="ssh ftp1.cccma.ec.gc.ca '""cd $3; ls -ldL $2""'" &&
      Xpctsz=`ssh $CMCFEDEST "$CmdLine" | tail -1 | $AWK 'BEGIN { } { print \$5 ; } END {} '` &&
      CwdLnx=`pwd` &&
      ssh $CMCFEDEST "cd $CwdLnx ; scp -p -o NoneSwitch=yes ftp1.cccma.ec.gc.ca:$3/$2 $Ufln"
     fi
    fi &&
    Actlsz=`\ls -al $Ufln | $AWK 'BEGIN { } { print \$5 ; } END {} '` &&
    if test "$Xpctsz" != "$Actlsz" ; then
     echo "" && echo "  obtnfl : Incomplete dataset transfer "  &&
     sdiagdir && ( exit 6 )
    fi 
   else
    access $Ufln $lfln nocp 
   fi &&
   if test ! -s $Ufln  ; then
    echo "" && echo "  obtnfl : empty, non existing file or timeout"  &&
    sdiagdir && ( exit 7 )
   fi &&
   set +x  &&
   :
  else
   echo "" && echo "  obtnfl : non existing file "  &&
    sdiagdir && ( exit 1 ) 
# fi || ( exit 9 );
  fi || ( exit $?) ;
  } &&
 xtrctx () { 
  set +x &&
  : &&
  eval "unset $3" &&
  eval "$3=`(echo 'A' ; grep \"$1.[0-9][0-9][0-9]\" $2 ) | tail -1 `" || (exit $?) ;
  } &&
 slctx () {
  set +x &&
  : &&
  if test "$2" = "$1" -a "$5" = "$1" ; then
    eval "rfln=null" &&
    eval "rdir=null" &&
    eval "rmachine=null"
  else
   winner=`(echo $2 ; echo $5 ) | sort | tail -1` &&  
   if test "$2" = "$winner" -a "$2" != "$1" ; then
    eval "rfln=$2" &&
    eval "rdir=$3" &&
    eval "rmachine=$4"
   else
    eval "rfln=$5" &&
    eval "rdir=$6" &&
    eval "rmachine=$7"
   fi
  fi || ( exit $?) ;
  } &&
#
 getfeflist () {
     (\rm $3/.feflist || : ) &&
  (cd $2; \ls -ldL $1.[0-9][0-9][0-9] ) | ($AWK '{ print $9 ; } ') > $3/.feflist || (exit $?) ;
  } &&
#
 getbeflist () {
  (\rm $3/.tbeflist || : ) &&
  (\rm $3/.beflist || : ) &&
  ssh $4 "cd $2 ; ls -ldL $1.[0-9][0-9][0-9]" > $3/.tbeflist &&
  if test ! -s $3/.tbeflist ; then
  (\rm $3/.tbeflist || : ) &&
  sleep 2 &&
  ssh $4 "cd $2 ; ls -ldL $1.[0-9][0-9][0-9]" > $3/.tbeflist &&
  sleep 2 
  fi &&
  if test -s "$3/.tbeflist" ; then
  cat $3/.tbeflist | ($AWK '{ print $9 ; } ') > $3/.beflist 
  fi || (exit $?) ;
  } &&
#
 getvicflist () {
  (\rm -f $3/.tvicflist $3/.vicflist || : ) &&
  if [ "$HDNODE" = 'hare' -o "$HDNODE" = 'brooks' -o "$HDNODE" = 'banting' -o "$HDNODE" = 'daley' ] ; then
   CmdLine="ssh ftp1.cccma.ec.gc.ca '""cd $2 ; \ls -ldL $1"'.[0-9][0-9][0-9]'"'" &&
   ssh $bemchn2 "$CmdLine" > $3/.tvicflist 
  else
   CmdLine="ssh ftp1.cccma.ec.gc.ca '""cd $2 ; \ls -ldL $1"'.[0-9][0-9][0-9]'"'" &&
   ssh $CMCFEDEST "$CmdLine" > $3/.tvicflist 
  fi &&
  cat $3/.tvicflist | ($AWK '{ print $9 ; } ') > $3/.vicflist || (exit $?) ;
  } &&
  putfile () {
   Ists=9 &&
   if test "$abort" != 'abort' ; then
   set +x &&
   : &&
   unset vol pdn id &&
   eval "$@" &&
   if [ -z "$pdn" ] ; then
    echo " taprsrv,putfile: Error, missing or invalid filename for pdn parameter " &&
    abort=abort &&
    (exit 1) 
   fi &&
   pdn=`echo $pdn | tr '\*' '_'` &&
   Updn=`echo $pdn | tr '[a-z]' '[A-Z]'` &&
   lpdn=`echo $pdn | tr '[A-Z]' '[a-z]'` &&
   echo "" && echo "  putfile: processing $Updn" &&
   Taperr_msg=" ===> Problem in accessing/transferring $lpdn data from disk!" &&
   set -x &&
   HOSTIDf=`echo $HOSTID | sed -e 's/eccc.*-ppp/cs/g' -e 's/^ppp/cs/g' | cut -c 1-3` &&
   if [ "$OS" = 'Linux' ] ; then 
    # if [ "$HOSTIDf" = 'cs1' ] ; then FeTrgt='eccc-ppp1' ; else FeTrgt='eccc-ppp2' ; fi ; 
    case $HOSTIDf in
     cs1) FeTrgt='eccc-ppp1' ;;
     cs2) FeTrgt='eccc-ppp2' ;;
     cs3) FeTrgt='eccc-ppp3' ;;
     cs4) FeTrgt='eccc-ppp4' ;;
       *) : ;;
    esac 
   fi &&

   case $mscan in
    eccc-ppp1|eccc-ppp2|eccc-ppp3|eccc-ppp4|besc ) access $Updn $lpdn nocp || ( exit 1) ;;
    both    ) getbeflist $pdn $BETmppath $Tmppath/$Tmpdir $bemchn && 
              getfeflist $pdn $DATAPATH $Tmppath/$Tmpdir && 
              xtrctx $pdn .feflist fepdn &&
              xtrctx $pdn .beflist bepdn &&
              slctx A $fepdn $DATAPATH $FeTrgt $bepdn $BETmppath $bemchn &&
              obtnfl $Updn $rfln $rdir $rmachine || (exit 2) ;;
    svvic   ) getvicflist $pdn $VICTmppath $Tmppath/$Tmpdir && 
              getfeflist  $pdn $DATAPATH   $Tmppath/$Tmpdir && 
              xtrctx $pdn .feflist fepdn &&
              xtrctx $pdn .vicflist vicpdn &&
              slctx A $fepdn $DATAPATH $FeTrgt $vicpdn $VICTmppath ftp1 &&
              obtnfl $Updn $rfln $rdir $rmachine || (exit 3) ;;
    vic     ) getvicflist $pdn $VICTmppath $Tmppath/$Tmpdir && 
              xtrctx $pdn .vicflist vicpdn &&
              obtnfl $Updn $vicpdn $VICTmppath ftp1  || (exit 4) ;;
    *       ) getbeflist $pdn $BETmppath $Tmppath/$Tmpdir $bemchn && 
              xtrctx $pdn .beflist bepdn &&
              obtnfl $Updn $bepdn $BETmppath $bemchn || (exit 5) ;;
   esac &&
   unset Taperr_msg  && Ists=0 || abort='abort' && (exit $Ists)
   fi  || ( exit $?) ;
   } &&
#
  mkdir -m 755 $Tmppath/$Tmpdir &&
 echo "" && echo "" && echo " taprsrv: $Tmppath/$Tmpdir temporay subdirectory created" && echo "" &&
#
# SX3.
 if test ! -f $Tmppath/$Tmpdir/.beflist -a "$mscan" != "besc" -a "$mscan" != "eccc-ppp1" -a "$mscan" != 'eccc-ppp2' -a "$mscan" != 'eccc-ppp3' -a "$mscan" != 'eccc-ppp4' -a "$mscan" != "vic" -a "$mscan" != "svvic" ; then
  ssh $bemchn "cd $BETmppath ; ls -1" | sed -n -e '/^.*\.[0-9][0-9][0-9] *$/p' > $Tmppath/$Tmpdir/.beflist
 fi &&
# Victoria.
#if test ! -f $Tmppath/$Tmpdir/.vicflist -a "$mscan" != "besc" -a "$mscan" != "pollux" -a "$mscan" != "both" -a "$mscan" != "dorval-ib"  -a "$mscan" != "saiph" -a "$mscan" != "maia" ; then
#if test ! -f $Tmppath/$Tmpdir/.vicflist -a "$mscan" != "besc" -a "$mscan" != "pollux" -a "$mscan" != "both" -a "$mscan" != "dorval-ib" -a "$mscan" != "maia" ; then
 if test ! -f $Tmppath/$Tmpdir/.vicflist -a \( "$mscan" = "vic" -o "$mscan" = "svvic" \) ; then
##   if [ "$OS" = 'Linux' ] ; then
## ##   ftp -i ftp1.cccma.ec.gc.ca << wooftp
## ##    cd $VICTmppath
## ##    dir \*.[0-9][0-9][0-9] $Tmppath/$Tmpdir/.tvicflist
## ##    quit
## ## wooftp
##   ssh $CMCFEDEST 'ssh ftp1.cccma.ec.gc.ca "cd $VICTmppath; ls -ld \*.[0-9][0-9][0-9]"' > $Tmppath/$Tmpdir/.tvicflist
##   else
##   ftp -i ftp1.cccma.ec.gc.ca << wooftp 
##    cd $VICTmppath
##    dir \*.[0-9][0-9][0-9] $Tmppath/$Tmpdir/.tvicflist
##    quit
## wooftp
##   fi
##   cat $Tmppath/$Tmpdir/.tvicflist | ( $AWK '{ print $9}' ) > $Tmppath/$Tmpdir/.vicflist
##   (\rm $Tmppath/$Tmpdir/.tvicflist || : )
  (\rm -f $Tmppath/$Tmpdir/.vicflist || : ) &&
  CmdLine="ssh ftp1.cccma.ec.gc.ca '""cd $VICTmppath;"' \ls -1 *.[0-9][0-9][0-9]'"'" &&
  ssh $CMCFEDEST "$CmdLine" > $Tmppath/$Tmpdir/.vicflist 
 fi &&
#  ls \*.[0-9][0-9][0-9] $Tmppath/$Tmpdir/.beflist
#
# if test ! -f $Tmppath/$Tmpdir/.feflist -a "$mscan" != "saiph" -a "$mscan" != "maia" -a "$mscan" != "vic" ; then
  if test ! -f $Tmppath/$Tmpdir/.feflist -a "$mscan" != "zeta" -a "$mscan" != "saiph" -a "$mscan" != "algol" -a "$mscan" != "hadar" -a "$mscan" != "spica" -a "$mscan" != "vic" ; then
   set +x &&
   : &&
   set +v &&
   cd $Tmppath &&
#  (ls *.[0-9][0-9][0-9] > $Tmppath/$Tmpdir/.feflist )  &&
   (pwd > $Tmppath/$Tmpdir/.feflist )  &&
   set -x &&
   sdiagdir
  fi 
 else
# 
  if test "$cfs" != 'yes' -a "$mode" != 'convert' ; then
    Tmpdir="tmp.c${tape}_${Usr}_${Tpid}_${Tstamp}" &&
    Taperr_msg="tape busy or incorrect access permission on $tapback cartridge" &&
    vmodtap vmode=755 tape=$tapback &&
    unset Taperr_msg 
  else
    Tmpdir="tmp.v${tape}_${Usr}_${Tpid}_${Tstamp}"
  fi &&
# echo "" && echo " taprsrv: $tapback cartridge access will be in read/write mode"  &&
#
 #if [ "$CnFs" = 'yes' -o "$CeTs" = 'yes' ] ; then
   mkdir -m 755 $Tmppath/$Tmpdir &&
 #else
 # mkdir $Tmppath/$Tmpdir &&
 # chmod $dvmode $Tmppath/$Tmpdir 
 #fi &&
  echo "" && echo "" && echo " taprsrv: $Tmppath/$Tmpdir temporay subdirectory created" && echo "" 
#
 fi &&
 
  cd $Tmppath/$Tmpdir &&
  if test  "$mode" != 'convert' ; then
   tmode='write=yes'  
  else
   tapexit 0
  fi &&
   set -x || 
  tapexit 1
 
else
  if [ "$hpcarc_mode" = 'yes' -o "$hpcarc_mode" = 'on' ] ; then
   tape='hpnls'
  fi &&
  Tmpdir="tmp.r.${tape}_${Usr}_${Tpid}_${Tstamp}" &&
  (\cp `which retriev` $Diagdir && chmod u+w $Diagdir/retriev || : ) &&
  # (\cp `which arc_rtrv` $Diagdir && chmod u+w $Diagdir/arc_rtrv || : ) &&
  (\cp `which arc_shrd` $Diagdir && chmod u+w $Diagdir/arc_shrd || : ) &&
  (\cp `which arc_lst` $Diagdir && chmod u+w $Diagdir/arc_lst || : ) &&
  (\cp `which taprlse` $Diagdir && chmod u+w $Diagdir/taprlse || : ) &&
  # (\cp `which cfsrip` $Diagdir && chmod u+w $Diagdir/cfsrip || : ) &&
  (\cp `which cmcarc.64` $Diagdir && chmod u+w $Diagdir/cmcarc.64 || : ) &&
  # ls -al $Diagdir &&
  PATH=`clnstrng ".:$Diagdir:$PATH" ':'` &&
  hash -r &&
 #if [ "$CnFs" = 'yes' -o "$CeTs" = 'yes' ] ; then
   mkdir -m 755 $Tmppath/$Tmpdir &&
 #else
 # mkdir $Tmppath/$Tmpdir &&
 # chmod $dvmode $Tmppath/$Tmpdir 
 #fi &&
  echo "" && echo "" && echo " taprsrv: $Tmppath/$Tmpdir temporary subdirectory created" && echo "" &&
 getfile () {
  unset vol pdn id save &&
  eval "$@" &&
  if [ -n "$pdn" ] ; then
   Updn=`echo $pdn | tr '[A-Z]' '[a-z]'` &&
   touch $Updn 
  else
   echo " taprsrv,getfile: Error, filename must be passed via pdn parameter " &&
   abort=abort &&
   (exit 1) 
  fi || (exit $?) ;
     } &&
# 
  unset Taperr_msg &&
  cd $Tmppath/$Tmpdir 
  fi &&
  unset Taperr_msg &&
  set -x || 
  tapexit 2
 
fi
