#!/bin/sh

#   Jul 12/17 - F. Majaess (Revised for HPNLS)
#   Sep 20/05 - F. Majaess (Implement checks for data corruption)
#   Jun 03/04 - F. Majaess (Implement support for "arcfile" 
#                           and "arclabel" use)
#   Sep 02/03 - F. Majaess (Revise for "short term" dataclass use)
#   Apr 13/99 - F. Majaess (Revise for CFS hardware switch to SGI)
#   May 25/98 - F. Majaess (Improve efficiency and reliability)
#   Nov 05/97 - F. Majaess (Implement support for "s" data type)
#   Mar 18/97 - F. Majaess (Revise for group restructuring)
#   Feb 04/97 - F. Majaess (Revise for CFS official data
#                           directory restructuring)
#   Jan 08/95 - F.Majaess (Enforce aborting if not all the requested 
#                          files are retrieved)
#   May 03/95 - F. Majaess (Put a check on file retrieval 
#                           from tape into disk on cfs)
#   Jan 31/95 - F. Majaess 

#id retriev - retrieves file(s) from the archive server.
 
#   AUTHOR  - F. Majaess

#hd PURPOSE - "retriev" script is used to retrieve file(s) from 
#hd           the archive server.
#hd
#hd           Note: This is HPNLS version. It's expected to be invoked
#hd                 from a subdirectory within sitestore filesystem.
#hd
#hd                 Under default "no hpcarchive" mode: 
#hd                 ----------------------------------
#hd            
#hd                 Short term dataclass target is ignored since no files
#hd                 under this dataclass on CFS were copied into HPNLS.
#hd                 None of the specified filenames to be retrieved 
#hd                 is allowed to terminate with "_arc" suffix unless
#hd                 "arcfile=none".
#hd                 If "arcfile" is specified and not set to "none", 
#hd                 then all inidividual files asked for are expected
#hd                 to be contained in "arcfile". Furthermore, if the
#hd                 list of individual files consist of just the keyword
#hd                 "all", then all individual files contained in 
#hd                 "arcfile" are retrieved.
#hd                 In the absence of "arcfile", attempts will be made 
#hd                 first to locate and retrieve as many files from 
#hd                 "_arc" files as possible with further narrowing the
#hd                 search by making use of "arclabel" functionality.
#hd                 Only "_arc" files residing under the corresponding
#hd                 subdirectories (as dictated by "offcl" and "shorterm"
#hd                 switches) will be scanned.
#hd
#hd                 Under "hpcarchive" mode with "hpcarc_mode=yes": 
#hd                 ----------------------------------------------
#hd            
#hd                 "offcl" switch is ignored.
#hd                 If "hpcarc_name" is specified and not set to "none", 
#hd                 then all inidividual files asked for are expected
#hd                 to be contained under "hpcarc_name" archive name. 
#hd                 Furthermore, if the list of individual files consist 
#hd                 of just the keyword "all", then all individual files 
#hd                 contained in "hpcarc_name" are retrieved.
#hd                 In the absence of "hpcarc_name", the search in
#hd                 "hpcarc_project" to locate and retrieve the files can
#hd                 can be narrowed by making use of "arclabel" archive 
#hd                 name prefix if specified. 
#hd                 By default, archives associated with "hpnls_user" 
#hd                 are targeted unless "hpnls_user=none" is pecified.
#hd                 The default "hpnls_user" setting is the invoker's 
#hd                 science account. 
#hd                 
#hd                 Please consult further info below especially for new
#hd                 options under "hpcarchive" mode.

#pr PARAMETERS:
#pr
#pr   POSITIONAL
#pr
#pr     f1 ... fn  = list of n file names to be retrieved
#pr
#pr   PRIMARY
#pr
#pr     fil_grpname= User has the option of specifying one of
#pr                  'ccrn_shr/ccrn_stf/ccrn_net/ccrn_rcm/ccrn_mam/ccrn_gst'
#pr                  group ownership on the retrieved files.
#pr                  (=$DEFFGRP).
#pr                  SUSPENDED.
#pr
#pr       arclabel = Default mode:
#pr                  A substring which can be used to narrow the search 
#pr                  for individual file(s) in the targeted "arc_dir" 
#pr                  subdirectories.
#pr                  It must map to substring of "arcfile" (see next) when 
#pr                  the latter is set to an "_arc" file. 
#pr                  It must not be set if "arcfile=none" otherwise such
#pr                  combined setting will lead to an abort.
#pr
#pr                  "hpcarchive" mode:
#pr                  A substring prefix to use in targeting hpcarchive 
#pr                  ARCHIVENAME.
#pr                  It's ignored if "hpcarc_name" is set. 
#pr                 
#pr        arcfile = Valid in the default no "hpcarchive" mode. 
#pr                  Filename to retrieve individual files from except in
#pr                  the special case when it's set to "none" where it'll
#pr                  indicate that none of the individual files resides 
#pr                  in an "arcfile" and that retrieval should be done
#pr                  directly on the corresponding file from the archive
#pr                  server. In the exception of such case, the "arcfile" 
#pr                  filename must terminate with "_arc" suffix.
#pr
#pr                  The behaviour of the code based on "arcfile"/"arclabel"
#pr                  switch settings can be summarized as follows:
#pr
#pr                  arclabel   arcfile    behaviour
#pr
#pr                  set/unset    unset    "arc_dir" scanned and any matching
#pr                                        individual file (no "_arc" suffix 
#pr                                        is allowed in any of the filenames) 
#pr                                        found will be retrieved from the 
#pr                                        corresponding "_arc" file. Any
#pr                                        "unmatched" files will be searched 
#pr                                        for and retrieved individually from 
#pr                                        the archive server.
#pr                                        If "arclabel" is specified then
#pr                                        it must map to substring of "arcfile"
#pr                                        and in which case the search for
#pr                                        individual files will be confined to
#pr                                        "_arc" files with matching substring
#pr                                        in the filename.
#pr
#pr                  unset        set      Files are retrieved from the 
#pr                                        "arcfile" on the archive server.
#pr                                        All individual files (no "_arc" 
#pr                                        suffix is allowed in any of the
#pr                                        filenames) are expected to reside 
#pr                                        in "arcfile". Furthermore, if the
#pr                                        list of individual files consist 
#pr                                        of just the keyword "all", then 
#pr                                        all individual files contained in
#pr                                        "arcfile" are retrieved.
#pr
#pr                  unset       "none"    "None" of the files specified is 
#pr                                        assumed to reside in an "_arc" 
#pr                                        file and a search for and retrieval 
#pr                                        of each of these files is to be 
#pr                                        carried on directly on the archive 
#pr                                        server. 
#pr                                        "arclabel" must be unset.
#pr       
#pr    hpcarc_name = Valid in "hpcarchive" mode (ie."hpcarc_mode=yes"). 
#pr                  Can be set to target particular ARCHIVENAME. 
#pr                  (all files contained in specified "hpcarc_name" are
#pr                   retrieved, if the list of files is set to 'all')
#pr 
#pr
#pr   SECONDARY 
#pr
#pr           offcl= Valid in the default no "hpcarchive" mode.
#pr                  switch used to restrict scanning and loading from
#pr                  the master data directory only on the file server
#pr                  (ie. no scanning of the user's own subdirectories)
#pr                  This switch override 'shorterm' switch.
#pr                  (=no/yes)
#pr        shorterm= Ignored under the default no "hpcarchive" mode.
#pr                  Under "hpcarchive" mode (ie."hpcarc_mode=yes"):
#pr                  switch used to target the short term ARCHIVE.
#pr                  (=no/yes)
#pr 
#pr        cfsuser= Valid in the default no "hpcarchive" mode
#pr                 target scanning "cfsuser" EC account name based  
#pr                 subdirectory on archive server instead of 
#pr                 user's own EC account name based subdirectory.
#pr   
#pr     hpnls_user= Valid in "hpcarchive" mode (ie."hpcarc_mode=yes").
#pr                 target scanning archived data associated with
#pr                 "hpnls_user" Science account instead of invoker's 
#pr                 own account.
#pr                 If set to 'none', the account based narrowed scan
#pr                 is skipped leading to wider "hpcarc_project" scan.
#pr  hpcarc_project= Valid in "hpcarchive" mode (ie."hpcarc_mode=yes").
#pr                  It can be used to override the default hpcarchive
#pr                  PROJECT name. Default is associated project name
#pr                  based on CRD account GID. It's set to 
#pr                  "crd_short_term" if "shorterm=yes".
#pr                  Current valid CRD project names under hpcarchive: 
#pr                   crd_cccma/crd_ccmr/crd_cdas/crd_cps/crd_ccdp 
#pr                   & crd_short_term
#pr                  
#pr    hpcarc_mode= Switch controlling switch to "hpcarchive" mode.
#pr                 (=no/yes)
#pr   
#pr   hpcarc_indvdl= Valid in "hpcarchive" mode (ie."hpcarc_mode=yes").
#pr                  Swtich to have separate "hpcarchive" data retrieval 
#pr                  call per file instead of the default of single call  
#pr                  to retrieve all.
#pr                  (=no/yes)
#pr   

#pr   

#ex EXAMPLE:
#ex   
#ex     retriev filea 'fileb*' 
#ex  
#ex     The above transfers 'filea' and all files whose names start
#ex     with 'fileb' from the archive server into local subdirectory.
#ex   
#ex     retriev cfsuser=acrnxyz filex 
#ex  
#ex     The above results in scanning and retrieving 'filex' from 
#ex     'acrnxyz' account subdirectory on archive server.
#ex
#ex     retriev hpcarc_mode=yes hpcarc_project=crd_cccma \
#ex             hpnls_user=abc123 \
#ex             hpcarc_name='mc_xyz789_2005_20181442134' all
#ex
#ex     The above results in scanning and loading all the files
#ex     contained in "mc_xyz789_2005_20181442134" ARCHIVE name 
#ex     associated with "abc123" account under "crd_cccma" PROJECT.
#ex     

#  * Reset field separators (otherwise those defined in the parent process
#  * will be used and these may cause problems if they include special 
#  * characters used in this script). 

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 retrieved.

arg_list=$@
#!!!!!
# echo "arg_list=$arg_list ."
# set -x
# \ls -al
# pwd
#!!!!!
for arg in $arg_list
do 
  case $arg in
       -*) set $arg                       ;;
      *=*) eval "$arg"                    ;;
    norep) norep='yes'                    ;;
  nocheck) nocheck='yes'                  ;;
  SkipChk) SkipChk='yes'                  ;;
    offcl) offcl='yes'                    ;;
 shorterm) shorterm='yes'                 ;;
 hpcarc_indvdl) hpcarc_indvdl='yes'       ;;
#     own) own='yes'                      ;;
   backup) option='-c '                   ;;
        *) if [ -z "$file_list" ] ; then
            file_list="$arg"
           else
            file_list=`clnstrng "$file_list $arg" ' '`
           fi
  esac
done

StStr3='no' ; StStr3=`pwd | sed -e 's/^\/space\/hall3\/sitestore\/eccc\/.*$/yes/'` ; StStr3=${StStr3:-'no'}
StStr4='no' ; StStr4=`pwd | sed -e 's/^\/space\/hall4\/sitestore\/eccc\/.*$/yes/'` ; StStr4=${StStr4:-'no'}

if [ "$StStr3" = 'yes' -o "$StStr4" = 'yes' ] ; then
 :
else 
 echo ' Retriev - Sorry local directory must be on sitestore3 or sitestore4!'
 exit 1
fi

TmnLog=${TmnLog:-'yes'}
if [ "$TmnLog" = 'yes' ] ; then
 Strt_Time=`date`
fi

# Possibly massage "force_output_return" if it's set ...
# It's used to control creating $Diagdir/.force_output_return
# file in case of severe issue.

if [ -n "$force_output_return" ] ; then
 if [ "$force_output_return" = 'on' ] ; then
  force_output_return='yes'
 elif [ "$force_output_return" = 'off' ] ; then
  force_output_return='no'
 fi
else
  force_output_return='yes'
fi

arcvmid='hpnls'
ArcRootPath='/archive/eccc/crd'
ArcSrvr_prfx='hpnl'

hpcarc_mode=${hpcarc_mode:-'no'}

if [ "$hpcarc_mode" = 'yes' -o "$hpcarc_mode" = 'on' ] ; then


 # Ensure "hpcarhive" setup is in place
 
 type hpcarchive >> /dev/null 2>>/dev/null && HpcStat="$?" || HpcStat="$?"
 if [ "$HpcStat" -eq 0 ] ; then
  :
 else
  echo "" ; echo " retriev: Sorry, not able to locate hpcarchive, possibly due to lack of proper setup!"
  exit 1
 fi

 # "hpcarchive" mode 
 
 echo "" ; echo " retriev: attempting retrieval of the requested file(s) under ==> hpcarchive <== mode" ; echo ""

 offcl='no'
 shorterm=${shorterm:='no'}
 if [ -n "$hpnls_user" ] ; then
  hpnls_user=`echo $hpnls_user | tr '[A-Z]' '[a-z]'`
 else
  hpnls_user=`whoami`
 fi
 
 file_list=`echo $file_list | tr '[A-Z]' '[a-z]'`
 file_list=`clnstrng "$file_list" ' '`
 if [ -z "$file_list" ] ; then
  echo "" ; echo "  archive: valid file list to process must be specified!" 
  exit 1
 elif [ "$file_list" = '0' ] ; then
  echo "" ; echo "  archive: invalid -- file_list=$file_list --!"
  exit 1 
 fi
 if [ -n "$hpcarc_name" -a "$hpcarc_name" = '0' ] ; then
  echo "" ; echo "  archive: invalid -- hpcarc_name=$hpcarc_name --!"
  exit 1
 fi
 if [ -n "$arclabel" -a "$arclabel" = '0' ] ; then
  echo "" ; echo "  archive: invalid -- arclabel=$arclabel --!"
  exit 1
 fi
 # hpcarc_project=${hpcarc_project:-'crd_cccma'}
 shorterm=${shorterm:='no'}
 if [ "$shorterm" = 'yes' ] ; then
  hpcarc_project='crd_short_term'
 fi
 if [ -z "$hpcarc_project" ] ; then
  GrpNm=`id -ng`
  case $GrpNm in
   eccc_ccrn_shr|eccc_ccrn_dat) hpcarc_project='crd_cccma' ;;
   eccc_ccmr) hpcarc_project='crd_ccmr' ;;
   eccc_ccrm) hpcarc_project='crd_cdas' ;;
   eccc_ccrp) hpcarc_project='crd_cps' ;;
   eccc_ccdp) hpcarc_project='crd_ccdp' ;;
   *) : ;;
  esac
 fi
 
 if [ -z "$hpcarc_project" ] ; then
  echo "" ; echo "  archive: Sorry, not able to configure needed hpcarchive project name !"
  exit 1
 fi

 if [ -n "$hpnls_user" -a "$hpnls_user" != 'none' ] ; then
  Options="-u $hpnls_user"
 else
  Options="-U"
 fi

 # hpcarc_indvdl controls whether to retrieve files individually
 # or all at once. Default, all at once.
 
 hpcarc_indvdl=${hpcarc_indvdl:-'no'}

 # Options="$Options -v -g" ## DO NOT USE - OUTPUT MESSY 
 if [ -n "$hpcarc_name" ] ; then
  # Confine target archive name to specified "hpcarc_name".
  ArcNm_RGX='^'"$hpcarc_name"'$'
  Options="$Options -c $ArcNm_RGX"
  hpcarc_indvdl='no'
 elif [ -n "$arclabel" ] ; then
  # Confine target archive name to specified "arclabel" prefix.
  ArcLbl_RGX='^'"$arclabel"
  Options="$Options -c $ArcLbl_RGX"
 fi
 if [ -n "$hpcarc_nobundle" -a \( "$hpcarc_nobundle" = 'on' -o "$hpcarc_nobundle" = 'yes' \) ] ; then
  if [ -n "$hpcarc_name" ] ; then
   hpcarc_nobundle='yes'
  else
   echo "" ; echo "  archive: Sorry, hpcarc_nobundle=$hpcarc_nobundle not allowed unless valid archivename is specified in hpcarc_name parameter!"
   exit 1
  fi
 fi
 (\rm -f .cmd_list || : )
 touch .cmd_list
 bad='no'
 Mfile_list=''
 if [ "$hpcarc_indvdl" = 'yes' ] ; then
  # Cmd1="#time hpcarchive -p $hpcarc_project -r $Options -s -b -x -n "
  if [ "$hpcarc_nobundle" = 'yes' ] ; then
   Cmd1="#time hpcarchive -p $hpcarc_project -r $Options -s -x "
  else
   Cmd1="#time hpcarchive -p $hpcarc_project -r $Options -s -b -x "
  fi
  prfx=''
 else
  prfx='# '
  if [ "$hpcarc_nobundle" = 'yes' ] ; then
   Cmd1="time hpcarchive -p $hpcarc_project -r $Options -s -x "
  else
   # Cmd1="time hpcarchive -p $hpcarc_project -r $Options -s -b -x -n "
   Cmd1="time hpcarchive -p $hpcarc_project -r $Options -s -b -x "
  fi
 fi

 # Procced in searching for requested files to retriev and setup 
 # corresponding hpcarchive commnd(s).

 if [ "$file_list" = 'all' -a -n "$hpcarc_name" ] ; then
  # Attempt the retrieval of "all" files contained under "hpcarc_name" archive name.
  unset file_list
  First_Last_fname="$hpcarc_name -- "
  line=`hpcarchive -p $hpcarc_project -l -x $Options 2>> /dev/null | sed -n -e '/^total/p' | $AWK '{ print $2 ; }'`
  if [ -n "$line" -a "$line" -gt 0 ] ; then
   if [ -n "$hpcarc_nobundle" -a \( "$hpcarc_nobundle" = 'on' -o "$hpcarc_nobundle" = 'yes' \) ] ; then
    hpcarc_nobundle='yes'
    file_list=`hpcarchive -p $hpcarc_project -l -x $Options 2>> /dev/null | sed -e '/^total/d' -e '/^ *$/d' -e '/^@/d' | $AWK -F '/' '{ print $2 ;}' | sort | uniq | tr '\012' '\040'`
    echo "time hpcarchive -p $hpcarc_project -r $Options -x &&" >> .cmd_list
    # echo "ls -al &&" >> .cmd_list
    # echo "cd $hpcarc_name &&" >> .cmd_list
    # echo "mv $file_list .. && " >> .cmd_list
    # echo "cd .. && rmdir $hpcarc_name || exit 1 " >> .cmd_list
    # echo "ls -ld $file_list" >> .cmd_list
   else
     # echo "time hpcarchive -p $hpcarc_project -r $Options -b -x -n " >> .cmd_list
     echo "time hpcarchive -p $hpcarc_project -r $Options -b -x " >> .cmd_list
     # file_list=`hpcarchive -p $hpcarc_project -l -x $Options -s 2>> /dev/null | sed -e '/^total/d' -e '/^ *$/d' -e '/^@/d' | sed -e 's/^.* //g' |  sed -e 's/^.*\// /' | tr '\012' '\040'`
     file_list=`hpcarchive -p $hpcarc_project -l -x $Options -s 2>> /dev/null | sed -e '/^total/d' -e '/^ *$/d' -e '/^@/d' | $AWK -F '/' '{ print $NF ;}' | tr '\012' '\040'`
     # echo "ls -ld $file_list" >> .cmd_list
   fi
   First_fname1=`echo $hpcarc_name | $AWK '{ print $1 " -- " ; }'`
   First_Last_fname2=`echo $file_list | $AWK '{ print $1 " ... " $NF ; }'`
   First_Last_fname="$First_fname1 $First_Last_fname2"
  else
   echo "" ; echo "  retriev: no matching $hpcarc_name archive name entry. Please correct and re-submit." ; echo ""
   exit 1
  fi
 else
  First_Last_fname=`echo $file_list | $AWK '{ print $1 " ... " $NF ; }'`
  # Search for and process the list of files to be retrieved. 

  for fln in $file_list
   do
    if [ "$hpcarc_nobundle" = 'yes' ] ; then
     flnRGX="$fln"
     line=`hpcarchive -p $hpcarc_project -l -x $Options -s -f ${flnRGX} 2>> /dev/null | sed -e '/^total/d' -e '/^ *$/d' | sed -e 's/^.* //g' | head -1`
     if [ -n "$line" ] ; then
      line="$line $fln"
     fi
    else
     flnRGX='.*\/'"$fln"'$'
     line=`hpcarchive -p $hpcarc_project -l -x $Options -s -f ${flnRGX} 2>> /dev/null | sed -e '/^total/d' -e '/^ *$/d' | sed -e 's/^.* //g' | head -2 |  tr '\012' '\040' | sed -e 's/ .*\// /'` 
    fi
    #set +x
    if [ -z "$line" ] ; then
     scnd=' '
    else
     scnd=`echo $line | $AWK '{ print $2 ; }'`
    fi
    if [ -n "$line" -a "$scnd" = "$fln" ] ; then
     set +x
     if [ "$hpcarc_nobundle" = 'yes' ] ; then
      echo $line | sed -e 's/ *$//g' | $AWK -v Options="$Options" -v fln="$fln" -v prfx="$prfx" '{ if ( $2 == fln ) { print prfx " time hpcarchive -p $hpcarc_project -r " Options " -x -c ^" $1 "$ -f " fln ; } ; }' >> .cmd_list
     else
      # echo $line | sed -e 's/ *$//g' | $AWK -v Options="$Options" -v fln="$fln" -v prfx="$prfx" '{ if ( $2 == fln ) { print prfx " time hpcarchive -p $hpcarc_project -r " Options " -b -x -n -c ^" $1 "$ -f " fln ; } ; }' >> .cmd_list
      echo $line | sed -e 's/ *$//g' | $AWK -v Options="$Options" -v fln="$fln" -v prfx="$prfx" '{ if ( $2 == fln ) { print prfx " time hpcarchive -p $hpcarc_project -r " Options " -b -x -c ^" $1 "$ -f " fln ; } ; }' >> .cmd_list
     fi
     if [ "$hpcarc_nobundle" = 'yes' ] ; then
      if [ -n "$Mfile_list" ] ; then
       Mfile_list="$Mfile_list"'|'"$fln"
      else
       Mfile_list="$fln"
      fi
     else
      if [ -n "$Mfile_list" ] ; then
       Mfile_list="$Mfile_list"'|.*\/'"$fln"'$'
      else
       Mfile_list='.*\/'"$fln"'$'
      fi
     fi
    else
     set +x
     echo "" ; echo " retriev: not able to find $fln file !"
     bad='yes'
    fi
  
   done
   if [ "$bad" != 'yes' -a -s "./.cmd_list" ] ; then
    echo "$Cmd1 -f '$Mfile_list'" >> .cmd_list
   #echo "ls -ld $file_list" >> .cmd_list
   fi

 fi
 
 if [ "$bad" != 'yes' ] ; then

  if [ -s "./.cmd_list" ] ; then
   if [ "$hpcarc_nobundle" = 'yes' ] ; then
    echo "ls -al &&" >> .cmd_list
    echo "cd $hpcarc_name &&" >> .cmd_list
    echo "mv $file_list .. && " >> .cmd_list
    echo "cd .. && rmdir $hpcarc_name || exit 1 " >> .cmd_list
   fi

  #  * Test for access to the archive server.
  
  unset hstnm
  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" ]
   do
    Tmstmp=`date`
    echo "" ; echo "" ; echo "  retriev: archive server is not accessible ~ $Tmstmp ! " ; echo ""
    sleep 300
    unset hstnm 
    hstnm=`ssh $arcvmid hostname 2>/dev/null | cut -c 1-4 `
    hststrgtdirck=`ssh $arcvmid hsmls -1d ${ArcRootPath}/ccrn`
   done
   # echo "  retriev: contents of .cmd_list file about to be executed:" ; echo "--------"
   # cat .cmd_list
   # echo "--------"
   set -x 
   . .cmd_list
   set +x
   echo "" ; echo " retriev: List of expected retrieved data:" ; echo ""
   \ls -ld $file_list
   \rm -f .cmd_list
  fi
  echo " " ; echo " retriev: Final check on retrieved data:" ; echo ""
  xitno=0
  for fln in $file_list
   do
    if [ -s "./$fln" -o -d "./$fln/." ] ; then
     chmod u+w $fln
     if [ "$nocheck" != 'yes' ] ; then
      filszck $fln || xitno=7
     # else
     #  ls -ld $fln
     fi
    else
     echo "" ; echo " no matching $fln file is present!" ; echo ""
     bad='yes'
     xitno=8
    fi
   done
  if [ "$TmnLog" = 'yes' -a -f "$CCRNSRC/arc_dir/log/hpnls_retriev_log" ] ; then
    echo "RETRIEV;`whoami`,hpcarc_project=$hpcarc_project,runid=$runid,`hostname`,PID=$$,exit=$xitno,Start: ${Strt_Time}; End: `date` -- RUNPATH=${RUNPATH} -- `pwd`/[${First_Last_fname}] " >> $CCRNSRC/arc_dir/log/hpnls_retriev_log
  fi 
  if [ "$xitno" -eq 0 ] ; then
   echo "" ; echo " retriev: Done - normal exit" ; echo ""
  else
   echo "" ; echo " retriev: Abnormal termination" ; echo ""
  fi
  exit $xitno
 else
  xitno=9
  echo "" ; echo " retriev: Not all requested files are found (hpnls_user=$hpnls_user). Possibly need to widen the search scope. Please correct and re-submit." ; echo ""
  if [ -f "$CCRNSRC/arc_dir/log/hpnls_problems_log" ] ; then
    echo "RETRIEV;`whoami`,hpcarc_project=$hpcarc_project,runid=$runid,`hostname`,PID=$$,exit=$xitno,Start: ${Strt_Time}; End: `date` -- RUNPATH=${RUNPATH} -- `pwd`/[${First_Last_fname}] " >> $CCRNSRC/arc_dir/log/hpnls_problems_log
  fi 
  exit 9
 fi

else # hpcarc_mode


 # NO "hpcarchive" mode 

 echo "" ; echo " retriev: NO hpcarchive mode is active " ; echo ""

 if [ -n "$cfsuser" ] ; then
  cfsuser=`echo $cfsuser | tr '[A-Z]' '[a-z]'`
  usr=`echo $cfsuser | sed 's/^....//'`
  grp=`echo $cfsuser | sed 's/...$//'`
 else
  Lgname=`whoami`
  LgnameEC=`accmap $Lgname`
  usr=`echo $LgnameEC |  sed 's/^....//'`
  grp=`echo $LgnameEC |  sed 's/...$//'`
  cfsuser="$LgnameEC"
 fi
 
 # Possibly massage "allow_r_duplicate" if it's set ...
 # It's used to allow bypassing existing archived copy check 
 # in case of duplicates and targets retrieving the latest one found.
 
 if [ -n "$allow_r_duplicate" ] ; then
  if [ "$allow_r_duplicate" = 'on' ] ; then
   allow_r_duplicate='yes'
  elif [ "$allow_r_duplicate" = 'off' ] ; then
   allow_r_duplicate='no'
  fi
 else
  #allow_r_duplicate='no'
   allow_r_duplicate='yes'
 fi
 
 
 # Possibly massage "nocheck" if it's set ...
 # It's used to bypass "md5sum" and "filszck" "invntry or ggstat based" tests ...
 
 if [ -n "$nocheck" ] ; then
  if [ "$nocheck" = 'on' ] ; then
   nocheck='yes'
  elif [ "$nocheck" = 'off' ] ; then
   nocheck='no'
  fi
 
 #if [ "$cfsuser" != 'acrnrsk' -a "$cfsuser" != 'acrncc2' -a "$cfsuser" != 'acrnmr1' -a "$cfsuser" != 'acrnmr2' -a "$cfsuser" != 'acrnsrc' ] ; then
 # nocheck='no'
 #fi
 else
  # # if [ "$offcl" != 'yes' -a "$shorterm" = 'yes' ] ; then
  # if [ "$offcl" != 'yes' -a "$cfsuser" != 'acrngcm' -a "$cfsuser" != 'acrnocn' -a "$cfsuser" != 'acrncbn' -a "$cfsuser" != 'acrnsip' -a "$cfsuser" != 'acrnesm' -a "$cfsuser" != 'acrncc1' -a "$cfsuser" != 'acrncc2' ] ; then
  #  nocheck='yes'
  # else
     nocheck='no'
  # fi
 fi
 ### Enforce "md5sum" and/or "filszck" "invntry or ggstat based" tests....
 ### Comments the next line later on:
   nocheck='no'
 ### Free up CFS disk space...
 
 if [ -n  "${cfsrlct}" ] ; then
  if [ -s "./${cfsrlct}_rmdiskcopy" ] ; then
   # (. ./${cfsrlct}_rmdiskcopy >> /dev/null 2>> /dev/null || : )
   (. ./${cfsrlct}_rmdiskcopy || : )
  fi
  (\rm -f ./${cfsrlct}_rmdiskcopy || : )
 fi
 
 ### Setup for controlling removal of tape retrieved datasets from disk
 
 if [ -n "$keepdskcpy" ] ; then
  if [ "$keepdskcpy" = 'on' ] ; then
   keepdskcpy='yes'
  elif [ "$keepdskcpy" = 'off' ] ; then
   keepdskcpy='no'
  fi
 else
 #keepdskcpy='no'
  keepdskcpy='yes'
 fi
 
 # Set "database" subdirectoy path...
 
 ARCDIR=${ARCDIR:-"/home/scrd101/arc_dir"} ; eval ARCDIR="$ARCDIR"
 
 # Disable backup option:
 unset option
 
 #
 xitno=0
 norep=${norep:='no'}
 SkipChk=${SkipChk:='no'}
 if [ "$arcfile" = 'none' ] ; then
  if [ -n "$arclabel" ] ; then
   echo "" ; echo "  retriev: Sorry, arclabel(=$arclabel) must not be set when arcfile=none !"
   exit 1
  fi
  # noarcmode=${noarcmode:-'yes'}
  noarcmode='yes'
 else
  noarcmode=${noarcmode:='no'}
 fi
 # offcl=${offcl:='no'}
 if [ "$offcl" = 'yes' -a "$shorterm" = 'yes' ] ; then
  shorterm='no'
  echo "" ; echo "  retriev: Sorry, short term dataclass subdirectory will not be targeted"
  echo "                  since the official data subdirectory is already chosen"
  echo "                  and it takes precedence!"
 fi
 
 # Add "u${usr}_" prefix (if need be) to "arclabel" provided the retrieval
 # is not targeting the official data subdirectory ...
 
 if [ "$offcl" = 'no' -a -n "$arclabel" ] ; then
  Usrprfx=`echo $arclabel | $AWK -F '_' '{print $1}'`
  if [ "$Usrprfx" != "u${usr}" ] ; then
   arclabel="u${usr}_${arclabel}"
   # echo "User prefix added, arclabel=$arclabel"
  fi
 fi
 
 # own=${own:='no'}
 
 # File groupname setup.
 
 DEFFGRP=${DEFFGRP:='eccc_ccrn_shr'}
 fil_grpname=${fil_grpname:=$DEFFGRP}
 # fil_grpname=${fil_grpname:='ccrn_shr'}
 
 #### Disable altering "group"
   unset fil_grpname
 
 if [ -n "$fil_grpname" ] ; then
   Tfil_grpname=$fil_grpname
   case $Tfil_grpname in
   eccc_ccrn_shr|eccc_ccrn_dat) fil_grpname=$Tfil_grpname ;;
   *) fil_grpname='eccc_ccrn_shr' ;
      echo "" ; echo "" ; echo "  retriev: Sorry, $fil_grpname file group ownership will be used" ;
      echo "             instead of the specified $Tfil_grpname !" ; echo ""
   esac
   unset Tfil_grpname
 fi
 #
 
 # User's default ...
 
 unset usrdirs
 if [ "$shorterm" != 'yes' -a "$offcl" != 'yes' ] ; then
   ### if [ "$cfsuser" = 'acrpxxx' -o "$cfsuser" = 'acrmxxx' ] ; then
   if [ "$cfsuser" = 'acrp001' -o "$cfsuser" = 'acrplbw' -o "$cfsuser" = 'acrprfb' -o "$cfsuser" = 'acrprgm' -o "$cfsuser" = 'acrpssu' -o "$cfsuser" = 'acrmrmc' -o "$cfsuser" = 'acrmrac' ] ; then
     usrdirs=${cfsuser_dir:="${ArcRootPath}/cnet/$grp/$usr"}
   # offcldir=$usrdirs
   elif [ "$cfsuser" = 'armnrls' ] ; then
     usrdirs=${cfsuser_dir:="/home/cfs_dorard/court_terme/armnrls"}
   elif [ "$cfsuser" = 'armnryj' ] ; then
     usrdirs=${cfsuser_dir:="/home/cfs_dorard/court_terme/armnryj"}
   else
   # 003|004) usrdirs="${ArcRootPath}/ccrn/$usr"  ;;
   # 003) usrdirs="${ArcRootPath}/ccrn/offcl_data"  ;;
   # 003) unset usrdirs ;;
   if [ "$grp" = 'acrn' ] ; then
   case $usr in
                             src ) usrdirs="${ArcRootPath}/ccrn/$usr"  ;;
    nys|rbd|rfp|rmh|yam|yjk) usrdirs="${ArcRootPath}/ccrd_long_term_archive/$usr"  ;;
    001|005|006|002|007|003|nbp|mcr|mky|all|cbn|ocn|opt|sip|cc1|cc2|cc3|cc4|esm|gcm|ice|rdr|rdy|rfm|rgb) usrdirs="${ArcRootPath}/ccrd_user_archive/$usr"  ;;
    mr1|mr2|mr3|mr4|mr5|mr6|mr7|mr8|mr9|pd1|pd2|pd3|pd4|pd5|pd6|pd7|pd8|pd9) usrdirs="${ArcRootPath}/ccrd_user_archive/$usr"  ;;
    01[0-9]|020) usrdirs="${ArcRootPath}/ccrd_user_archive/$usr"  ;;
    rjk|ryj|rmq|wsl|rjl|rjs|smp|rby|ros|rdp|rcw|rcs|rrh|rsk|rks|rrm|rmn|rtm|008|rjc|rls|rms|rns|rrr|raa|rfc|rrs|rsa|ror|rjm|rpg|red) usrdirs="${ArcRootPath}/ccrd_user_archive/$usr"  ;;
    rkz|rwm|rmm|ram|rng|rkd|rkm|rgf|ref|rjf|rmg|rcc|rva|rbb|rbt|rkl|ncs|ras|jcl|rlr|rbw|jos|rja|sah|004|rmb|rml|rnm|rsl|rwl) usrdirs="${ArcRootPath}/ccrd_user_archive/$usr"  ;;
    rrd|rhb|rec|rdv|jpb|vrm ) usrdirs="${ArcRootPath}/cnet/$usr"  ;;
    009|c02|y01|y03|y04|y06|y07|y09|yww|ydc|ysb|yvf|ycm ) usrdirs="${ArcRootPath}/cnet/$usr"  ;;
    #q0[1-7]|qrj|qaf|qdc|qdp|qhc|qmg|qsb ) usrdirs="${ArcRootPath}/cnet/$usr"  ;;
     qmg ) usrdirs="${ArcRootPath}/cnet/$usr"  ;;
                               * ) echo "" ; echo "  retriev: your account is not valid, usr=$usr" 
                                   exit 1 ;;
   esac
   else
    if [ -n "$cfsuser_dir" ] ; then
     usrdirs="$cfsuser_dir"
    fi
   fi
   # offcldir="${ArcRootPath}/ccrn/offcl_data"
   fi
 fi
 
 # User's short term ...
 
 unset shrtmdir
 if [ "$shorterm" != 'no' -a "$offcl" != 'yes' ] ; then
   ### if [ "$cfsuser" = 'acrpmdm' -o "$cfsuser" = 'acrmril' ] ; then
   ### if [ "$cfsuser" = 'acrpxxx' -o "$cfsuser" = 'acrmxxx' ] ; then
   if [ "$cfsuser" = 'acrp001' -o "$cfsuser" = 'acrplbw' -o "$cfsuser" = 'acrprfb' -o "$cfsuser" = 'acrprgm' -o "$cfsuser" = 'acrpssu' -o "$cfsuser" = 'acrmrmc' -o "$cfsuser" = 'acrmrac' ] ; then
     shrtmdir="${ArcRootPath}/ccrd_short_term_archive/$grp/$usr"
   elif [ "$cfsuser" = 'armnrls' ] ; then
     shrtmdir="/home/cfs_dorard/court_terme/armnrls"
   elif [ "$cfsuser" = 'armnryj' ] ; then
     shrtmdir="/home/cfs_dorard/court_terme/armnryj"
   else
   if [ "$grp" = 'acrn' ] ; then
   case $usr in
                             src ) shrtmdir=""  ;;
    nys|rbd|rfp|rmh|yam|yjk) shrtmdir="${ArcRootPath}/ccrd_long_term_archive/$usr"  ;;
    001|005|006|002|007|003|nbp|mcr|mky|all|cbn|ocn|opt|sip|cc1|cc2|cc3|cc4|esm|gcm|ice|rdr|rdy|rfm|rgb) shrtmdir="${ArcRootPath}/ccrd_short_term_archive/$usr"  ;;
    mr1|mr2|mr3|mr4|mr5|mr6|mr7|mr8|mr9|pd1|pd2|pd3|pd4|pd5|pd6|pd7|pd8|pd9) shrtmdir="${ArcRootPath}/ccrd_short_term_archive/$usr"  ;;
    01[0-9]|020) shrtmdir="${ArcRootPath}/ccrd_short_term_archive/$usr"  ;;
    rjk|ryj|rmq|wsl|rjl|rjs|smp|rby|ros|rdp|rcw|rcs|rrh|rsk|rks|rrm|rmn|rtm|008|rjc|rls|rms|rns|rrr|raa|rfc|rrs|rsa|ror|rjm|rpg|red) shrtmdir="${ArcRootPath}/ccrd_short_term_archive/$usr"  ;;
    rkz|rwm|rmm|ram|rng|rkd|rkm|rgf|ref|rjf|rmg|rcc|rva|rbb|rbt|rkl|ncs|ras|jcl|rlr|rbw|jos|rja|sah|004|rmb|rml|rnm|rsl|rwl) shrtmdir="${ArcRootPath}/ccrd_short_term_archive/$usr"  ;;
    rrd|rhb|rec|rdv|jpb|vrm ) shrtmdir="${ArcRootPath}/ccrd_short_term_archive/$usr"  ;;
    009|c02|y01|y03|y04|y06|y07|y09|yww|ydc|ysb|yvf|ycm ) shrtmdir="${ArcRootPath}/ccrd_short_term_archive/$usr"  ;;
    #q0[1-7]|qrj|qaf|qdc|qdp|qhc|qmg|qsb ) shrtmdir="${ArcRootPath}/ccrd_short_term_archive/$usr"  ;;
     qmg ) shrtmdir="${ArcRootPath}/ccrd_short_term_archive/$usr"  ;;
                               * ) echo "" ; echo "  retriev: your account is not valid, usr=$usr" 
                                   exit 2 ;;
   esac
   fi
   # offcldir="${ArcRootPath}/ccrn/offcl_data"
   fi
   usrdirs=`clnstrng "$usrdirs $shrtmdir" ' '`
 fi
 ###
 offcldir="${ArcRootPath}/ccrn/offcl_data"
 if [ "$offcl" = 'yes' ] ; then
   usrdirs="$offcldir"
   echo "" ; echo "" ; echo "  retriev: As requested, only $usrdirs directory will be scanned"
 elif [ "$offcl" != 'no' ] ; then
   usrdirs=`clnstrng "$usrdirs $offcldir" ' '`
 fi
 
 if [ -n "$Rm_Arc_List" ] ; then
  unset Rm_Arc_List
 fi
 
 # "all" individual files are to retrieved from the specified "arcfile" case
 
 if [ "$noarcmode" != 'yes' -a -n "$arcfile" ] ; then
  Arc_extnsn=`echo $arcfile | tr '[A-Z]' '[a-z]' | sed -n -e 's/^.*_arc$/Yes/p'`
  if [ "$Arc_extnsn" != 'Yes' ] ; then
   echo "" ; echo "  retriev: Sorry; Invalid arcfile(=$arcfile); it must terminates with _arc suffix!"
   exit 1
  fi
  if [ "$file_list" = 'all' ] ; then
   #
   if [ ! -f "./.$arcfile" ] ; then
    Arc_Location=`arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm location $arcfile`
    if [ -s "$Arc_Location" ] ; then
     ln -s $Arc_Location ./.$arcfile
     Rm_Arc_List="$Rm_Arc_List ./.$arcfile"
    fi
   fi
   #
   # file_list=`arc_lst own indvdlst=yes user=$cfsuser masterdir=$offcl shorterm=$shorterm $arcfile`
   file_list=`cat ./.$arcfile | $AWK '{ print \$1 }' | tr '\\012' '\\040'`
   if [ -z "$file_list" ] ; then
    echo "" ; echo "  retriev: Sorry; could not get the individual files list from the specified arcfile(=$arcfile) !" 
    echo "           Perhaps, improper set of switches used, missing entry in 'arc_dir' or incorrect arcfile filename!"
    if [ -n "$Rm_Arc_List" ] ; then
     \rm -f $Rm_Arc_List
    fi
    exit 1 
   fi
   arclabel=`echo $arcfile | sed -e 's/_arc *$//'`
   echo "" ; echo "" ; echo "  retriev: the list of all individual files to be retrieved and are contained in "
   echo "               --> $arcfile <-- "
   echo "           consists of:" ; echo ""
   for fname in $file_list
    do
     echo "   --> $fname "
    done
   echo " "
   # echo "New file_list=$file_list"
  else
   if [ -n "$arclabel" ] ; then
    Match=`echo arcfile arclabel | $AWK -v arcfile=$arcfile -v arclabel=$arclabel ' { print match(arcfile,arclabel) ; }'`
    if [ "$Match" -le 0 ] ; then
      echo "" ; echo "  retriev: Sorry; arclabel(=$arclabel) must be a substring of the specified arcfile(=$arcfile)!"
      echo "           Correct and re-submit."
      exit 1
    fi
   fi
 
   # Ensure proper list of individual files is specified.
   
   if [ "$SkipChk" != 'yes' ] ; then
    if [ -z "$file_list" ] ; then
     echo "" ; echo "  retriev: no retrieve file list is found"
     exit 2
    else
     file_list=`echo $file_list | sed -e 's/^ *//'`
     if [ -z "$file_list" -o "$file_list" = '*' ] ; then
      echo '0 retriev: invalid or empty file list '
      exit 3
     else
      file_list=`echo $file_list | tr '[A-Z]' '[a-z]'`
      
      #
      if [ ! -f "./.$arcfile" ] ; then
       Arc_Location=`arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm location $arcfile`
       if [ -s "$Arc_Location" ] ; then
        ln -s $Arc_Location ./.$arcfile
        Rm_Arc_List="$Rm_Arc_List ./.$arcfile"
       fi
      fi
      #
 
      # Ensure existence of each individual file in the specified "arcfile".
  
      # TarcFlist=`arc_lst own indvdlst=yes user=$cfsuser masterdir=$offcl shorterm=$shorterm $arcfile`
      TarcFlist=`cat ./.$arcfile | $AWK '{ print \$1 }' | tr '\\012' '\\040'`
      if [ -z "$TarcFlist" ] ; then
       echo "" ; echo "  retriev: Sorry; could not get the contents of the specified arcfile(=$arcfile) !" 
       echo "           Perhaps, improper set of switches used, missing entry in 'arc_dir' or incorrect arcfile filename!"
       if [ -n "$Rm_Arc_List" ] ; then
        \rm -f $Rm_Arc_List
       fi
       exit 4 
      fi
  
      xitno=0
      for fname in $file_list
      do
       f_found='false'
       for fnd_fname in $TarcFlist
       do
        if [ "$fname" = "$fnd_fname" ] ; then
         f_found='true'
         break
        fi
       done
       if [ "$f_found" != 'true' ] ; then
        echo "" ; echo "  retriev: No matching entry for $fname is found in $arcfile !"
        xitno=5
       fi
      done 
      if [ "$xitno" -ne 0 ] ; then
       echo "" ; echo "  retriev: Correct the individual files list or 'arcfile' filename, and re-submit." 
       if [ -n "$Rm_Arc_List" ] ; then
        \rm -f $Rm_Arc_List
       fi
       exit 5
      fi
     fi
    fi
   fi
 
  fi
 fi
 
 # Ensure proper list of individual files is specified.
 
 if [ -z "$file_list" ] ; then
  echo "" ; echo "  retriev: no retrieve file list is found"
  if [ -n "$Rm_Arc_List" ] ; then
   \rm -f $Rm_Arc_List
  fi
  exit 1
 else
  file_list=`echo $file_list | sed -e 's/^ *//'`
  if [ -z "$file_list" -o "$file_list" = '*' ] ; then
   echo '0 retriev: invalid or empty file list '
   if [ -n "$Rm_Arc_List" ] ; then
    \rm -f $Rm_Arc_List
   fi
   exit 2
  else
   file_list=`echo $file_list | tr '[A-Z]' '[a-z]'`
 #
 # Check for possible existence of local file(s) sharing same 
 # filename(s) as the requested one(s)...
 #
   for fname in $file_list
   do
    if [ "$noarcmode" != 'yes' -o \( "$noarcmode" = 'yes' -a -n "$cfsrlct" \) ] ; then
     Arc_extnsn=`echo $fname | sed -n -e 's/^.*_arc$/Yes/p'`
     if [ "$Arc_extnsn" = 'Yes' ] ; then
      # Abort soon a filename is detected to terminate with "_arc" suffix.
      echo "" ; echo "  retriev: Invalid $fname specified which terminates with _arc suffix!"
      if [ -n "$Rm_Arc_List" ] ; then
       \rm -f $Rm_Arc_List
      fi
      exit 2
     fi
    fi
    if [ "$norep" = 'yes' ] ; then
     if [ -s "$fname" -o -L "$fname" ] ; then
      echo "" ; echo "  retriev: local dataset $fname already exists"
      if [ -n "$Rm_Arc_List" ] ; then
       \rm -f $Rm_Arc_List
      fi
      exit 2
     fi
    fi
   done
 #
  fi
 fi
 #  * Test for access to the archive server.
 
 unset hstnm
 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" ]
  do
   Tmstmp=`date`
   echo "" ; echo "" ; echo "  retriev: archive server is not accessible ~ $Tmstmp ! " ; echo ""
   sleep 300
   unset hstnm 
   hstnm=`ssh $arcvmid hostname 2>/dev/null | cut -c 1-4 `
   hststrgtdirck=`ssh $arcvmid hsmls -1d ${ArcRootPath}/ccrn`
  done
 #
 # echo "usrdirs=$usrdirs"
 for savdir in $usrdirs
  do
   usrdir=`ssh $arcvmid "cd $savdir ; pwd "`
   if [ -z "$usrdir" ] ; then
    sleep 5
    usrdir=`ssh $arcvmid "cd $savdir ; pwd "`
    if [ ! -z "$usrdir" -a -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
     echo "RETRIEV;...:`whoami`,`date`,......-$$; failed previously in ssh check on $savdir!" >> $CCRNSRC/info/.cfs_problems_log
    fi
   fi
   if [ "$savdir" != "$usrdir" ] ; then
     echo "" ; echo "" ; echo "  retriev: Sorry, can not access $savdir on $arcvmid !"
     if [ -n "$Rm_Arc_List" ] ; then
      \rm -f $Rm_Arc_List
     fi
     exit 3
   else
     echo "" ; echo "  retriev: subdirectory to be scanned --> $savdir <--"
   fi
  done
  echo " " ; echo ""
 
 ### byFreeSpace=${byFreeSpace='on'}
 ### if [ "$byFreeSpace" = 'on' ] ; then
 ###  # Make sure of the space on the disk
 ###  FreeSpace=`ssh $arcvmid df -Pm $savdir | tail -1 | awk '{ print $4 ; }'`
 ###  FreeSpace=${FreeSpace=0} 
 ###  while test $FreeSpace -lt 95000
 ###  do
 ###   echo "" ; echo "" ; echo "  retriev : waiting, free disk space is $FreeSpace MB (< 95GB) on cfs:$savdir ~ `date` " ; echo "" 
 ###   sleep 300 
 ###   unset FreeSpace 
 ###   FreeSpace=`ssh $arcvmid df -Pm $savdir | tail -1 | awk '{ print $4 ; }'`
 ###   FreeSpace=${FreeSpace=0} 
 ###   done 
 ###  #
 ### else
 ###  # Make sure of the space on the disk
 ###  # DskSpace=`rsh $arcvmid df $savdir | tail -1 | awk 'BEGIN {} { print $6 ; } END {} ' `
 ###  # DskSpace=`rsh $arcvmid df $savdir | tail -1 | sed -e 's/%/ /g' | awk 'BEGIN {} { print $5 ; } END {} ' `
 ###  # DskSpace=`ssh $arcvmid "df $savdir" 2>/dev/null | tail -1 | sed -e 's/%/ /g' | awk 'BEGIN {} { print $5 ; } END {} ' `
 ###  DskSpace=`ssh $arcvmid "df -P $savdir" 2>/dev/null | tail -1 | sed -e 's/%/ /g' | awk 'BEGIN {} { print $5 ; } END {} ' `
 ###  DskSpace=${DskSpace=100}
 ###  while test "$DskSpace" -gt 90
 ###  do
 ###   echo "" ; echo "" ; echo "  retriev : waiting, disk space usage is $DskSpace % on cfs:$savdir ~ `date` " ; echo ""
 ###   sleep 300
 ###   unset DskSpace
 ###   # DskSpace=`ssh $arcvmid "df $savdir" 2>/dev/null | tail -1 | sed -e 's/%/ /g' | awk 'BEGIN {} { print $5 ; } END {} ' `
 ###   DskSpace=`ssh $arcvmid "df -P $savdir" 2>/dev/null | tail -1 | sed -e 's/%/ /g' | awk 'BEGIN {} { print $5 ; } END {} ' `
 ###   DskSpace=${DskSpace=100}
 ###   done
 ###  #
 ### fi
 
 #!!!!!
 # Adjust setup based on "noarcmode" parameter setting ...
 
 # unset arc_list files_in_arc_list indvdl_list 
 arc_list='' ; files_in_arc_list='' ; indvdl_list=''
 if [ "$noarcmode" != 'yes' ] ; then
  
  if [ -n "$arcfile" ] ; then
 
   Arc_extnsn=`echo $arcfile | tr '[A-Z]' '[a-z]' | sed -n -e 's/^.*_arc$/Yes/p'`
   if [ "$Arc_extnsn" != 'Yes' ] ; then
    echo "" ; echo "  retriev: Sorry; Invalid arcfile(=$arcfile); it must terminates with _arc suffix!"
    if [ -n "$Rm_Arc_List" ] ; then
     \rm -f $Rm_Arc_List
    fi
    exit 1
   fi
   files_in_arc_list="$file_list"
   file_list="$arcfile"
 
  else
  
   # Assess shared prefix of "_" separated subfields in the supplied names
   # and extract into "./.sub_arclist" all matching entries in order to speedup
   # the scan further on...
 
   Shrd_Fields_Prfx=`arc_shrd $file_list`
 
   if [ -n "$Shrd_Fields_Prfx" -a "$offcl" = 'yes' ] ; then
 
    # Discard single shared field if the master data directory is targeted ...
    # (ie. Ensure a minimum of 2 shared fields in the case of "$offcl" = 'yes' )
 
 
    NshrdFlds=`echo $Shrd_Fields_Prfx | $AWK -F '_' '{print NF}'`
    NshrdFlds=${NshrdFlds:=0}
    if [ "$NshrdFlds" -lt 2 ] ; then
     Shrd_Fields_Prfx=''
     NshrdFlds='' 
    fi
   fi
 
   if [ -n "$Shrd_Fields_Prfx" ] ; then
    (\rm -f ./.sub_arclist || : )
    touch ./.sub_arclist
   #if [ -n "$arclabel" ] ; then
   #  arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=$arclabel $Shrd_Fields_Prfx >> ./.sub_arclist
   #else
      arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $Shrd_Fields_Prfx  >> ./.sub_arclist
   #fi
    if [ ! -s "./.sub_arclist" ] ; then
    #if [ -n "$arclabel" ] ; then
    # arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=$arclabel $Shrd_Fields_Prfx >> ./.sub_arclist
    #else
      arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $Shrd_Fields_Prfx >> ./.sub_arclist
    #fi
    fi
    Rm_Arc_List="$Rm_Arc_List ./.sub_arclist"
   fi 
 
   # search "arc_dir" for individual files and setup (as 
   # appropriate) in:
   #
   #  arc_list          - list of "arcfile" names
   #  indvdl_list       - list individual files
   #  files_in_arc_list - list of individual files found 
   #                      in "arcfile"(s)
 
   Dplcts='no'
   Dplcts_list='' 
   Hold_Dplcts='no'
   Hold_Dplcts_list=''
   Hold_Dplcts_indufile_list=''
   for fname in $file_list
   do
  
    # Search for "fname" in "arc_dir"...
 
    # Find out about # of existing copies in "_arc" files.
 
    if [ -s "./.sub_arclist"  ] ; then
     nmbr_exstng_copies=`cat ./.sub_arclist | sed -n -e "/^$fname /p" | wc -l | sed -e 's/ //g'`
    else
    #if [ -n "$arclabel" ] ; then
    # nmbr_exstng_copies=`arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=$arclabel $fname | sed -n -e "/^$fname /p" | wc -l | sed -e 's/ //g'`
    #else
      # nmbr_exstng_copies=`arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm $fname | sed -n -e "/^$fname /p" | wc -l | sed -e 's/ //g'`
      nmbr_exstng_copies=`arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $fname | sed -n -e "/^$fname /p" | wc -l | sed -e 's/ //g'`
    #fi
    fi
 
    if [ -z "$nmbr_exstng_copies" -o \( -n "$nmbr_exstng_copies" -a "$nmbr_exstng_copies" -eq 0 \) ] ; then
     if [ -s "./.sub_arclist"  ] ; then
      nmbr_exstng_copies=`cat ./.sub_arclist | sed -n -e "/^$fname /p" | wc -l | sed -e 's/ //g'`
     else
     #if [ -n "$arclabel" ] ; then
     # nmbr_exstng_copies=`arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=$arclabel $fname | sed -n -e "/^$fname /p" | wc -l | sed -e 's/ //g'`
     #else
       # nmbr_exstng_copies=`arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm $fname | sed -n -e "/^$fname /p" | wc -l | sed -e 's/ //g'`
       nmbr_exstng_copies=`arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $fname | sed -n -e "/^$fname /p" | wc -l | sed -e 's/ //g'`
     #fi
     fi
     if [ "$nmbr_exstng_copies" -gt 0 ] ; then
       if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
        echo "RETRIEV;---:`whoami`,`date`,......; reporting prior attempt failure in obtaining $fname info from database!" >> $CCRNSRC/info/.cfs_problems_log  
       fi
     fi
    fi
 
    # Take proper action based on the # copies found (if any) ...
 
    unset filsrchrslt
    
    if [ "$nmbr_exstng_copies" -gt 1 ] ; then
     Dplcts='yes'
     Hold_Dplcts='yes'
     Dplcts_list="$Dplcts_list $fname"
     Hold_Dplcts_list=`clnstrng "$Hold_Dplcts_list $fname" ' '`
     echo "" ; echo "  retriev: More than one archived copy of -- $fname -- is found in:" ; echo ""
     if [ -n "${Diagdir}" -a -d "${Diagdir}/." -a "${force_output_return}" != 'no' ] ; then touch ${Diagdir}/.force_output_return ; fi 
     if [ -s "./.sub_arclist"  ] ; then
      cat ./.sub_arclist | sed -n -e "/^$fname /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
      if [ "$allow_r_duplicate" = 'yes' ] ; then
       filsrchrslt=`cat ./.sub_arclist | sed -n -e "/^$fname /p" | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'`
       if [ -z "$filsrchrslt" ] ; then
        filsrchrslt=`cat ./.sub_arclist | sed -n -e "/^$fname /p" | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'`
       fi
       if [ -z "$filsrchrslt" ] ; then
        echo "" ; echo "           Failed for the second time in obtaining expected entry for $fname from the database!"
        if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
         echo "RETRIEV;2nd:`whoami`,`date`,SEVERE-$$; failed database entry return for $fname!" >> $CCRNSRC/info/.cfs_problems_log  
        fi
        echo "" ; echo " "
        echo "           ***************************************************************         "
        echo "    **>>>*** PLEASE NOTIFY CCCma CFS SUPPORT PERSONNEL OF THIS OCCURENCE ***<<<**"
        echo "           ***************************************************************       " ; echo ""
        if [ -n "$Rm_Arc_List" ] ; then
         \rm -f $Rm_Arc_List
        fi
        if [ -n "$Rm_Arc_List" ] ; then
         \rm -f $Rm_Arc_List
        fi
        exit 1 
       fi
      fi
     else
     #if [ -n "$arclabel" ] ; then
     # arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=$arclabel $fname | sed -n -e "/^$fname /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
     #else
      #arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm $fname | sed -n -e "/^$fname /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
       arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $fname | sed -n -e "/^$fname /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
       if [ "$allow_r_duplicate" = 'yes' ] ; then
        filsrchrslt=`arc_lst sngl own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $fname`
        if [ -z "$filsrchrslt" ] ; then
         filsrchrslt=`arc_lst sngl own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $fname`
        fi
        if [ -z "$filsrchrslt" ] ; then
         echo "" ; echo "           Failed for the second time in obtaining expected entry for $fname from the database!"
         if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
          echo "RETRIEV;2nd:`whoami`,`date`,SEVERE-$$; failed database entry return for $fname!" >> $CCRNSRC/info/.cfs_problems_log  
         fi
         echo "" ; echo " "
         echo "           ***************************************************************         "
         echo "    **>>>*** PLEASE NOTIFY CCCma CFS SUPPORT PERSONNEL OF THIS OCCURENCE ***<<<**"
         echo "           ***************************************************************       " ; echo ""
         if [ -n "$Rm_Arc_List" ] ; then
          \rm -f $Rm_Arc_List
         fi
         if [ -n "$Rm_Arc_List" ] ; then
          \rm -f $Rm_Arc_List
         fi
         exit 1 
        fi
       fi
     #fi 
     fi 
     echo " "
 
    else
     if [ "$nmbr_exstng_copies" -eq 1 ] ; then
      if [ -s "./.sub_arclist"  ] ; then
      #nmbr_exstng_copies=`cat ./.sub_arclist | sed -n -e "/^$fname /p" | wc -l | sed -e 's/ //g'`
       filsrchrslt=`cat ./.sub_arclist | sed -n -e "/^$fname /p"`
      else
      #if [ -n "$arclabel" ] ; then
      # filsrchrslt=`arc_lst sngl own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=$arclabel $fname`
      #else
        # filsrchrslt=`arc_lst sngl own user=$cfsuser masterdir=$offcl shorterm=$shorterm $fname`
        filsrchrslt=`arc_lst sngl own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $fname`
      #fi
      fi
 
      if [ -z "$filsrchrslt" ] ; then
       echo "" ; echo "           Failed to obtain expected entry for $fname from the database!"
       if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
        echo "RETRIEV;1st:`whoami`,`date`,......; failed database entry return for $fname!" >> $CCRNSRC/info/.cfs_problems_log  
       fi
       echo "           Giving it another try..."
       if [ -s "./.sub_arclist"  ] ; then
       #nmbr_exstng_copies=`cat ./.sub_arclist | sed -n -e "/^$fname /p" | wc -l | sed -e 's/ //g'`
        filsrchrslt=`cat ./.sub_arclist | sed -n -e "/^$fname /p"`
       else
       #if [ -n "$arclabel" ] ; then
       # filsrchrslt=`arc_lst sngl own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=$arclabel $fname`
       #else
         # filsrchrslt=`arc_lst sngl own user=$cfsuser masterdir=$offcl shorterm=$shorterm $fname`
         filsrchrslt=`arc_lst sngl own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $fname`
       #fi
       fi
       if [ -z "$filsrchrslt" ] ; then
        echo "" ; echo "           Failed for the second time in obtaining expected entry for $fname from the database!"
        if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
         echo "RETRIEV;2nd:`whoami`,`date`,SEVERE-$$; failed database entry return for $fname!" >> $CCRNSRC/info/.cfs_problems_log  
        fi
        echo "" ; echo " "
        echo "           ***************************************************************         "
        echo "    **>>>*** PLEASE NOTIFY CCCma CFS SUPPORT PERSONNEL OF THIS OCCURENCE ***<<<**"
        echo "           ***************************************************************       " ; echo ""
        if [ -n "$Rm_Arc_List" ] ; then
         \rm -f $Rm_Arc_List
        fi
        if [ -n "$Rm_Arc_List" ] ; then
         \rm -f $Rm_Arc_List
        fi
        exit 1 
       fi
      fi
     fi
    fi
 
 
    if [ -n "$filsrchrslt" ] ; then
 
     # Extract "arcfile" name ...
 
     arcflname=`echo $filsrchrslt | $AWK '{print $3}'`
     #
     if [ ! -f "./.$arcflname" ] ; then
      Arc_Location=`arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm location $arcflname`
      if [ -z "$Arc_Location" ] ; then
       if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
        echo "RETRIEV;1st:`whoami`,`date`,......; failed in locating $arcflname in the database!" >> $CCRNSRC/info/.cfs_problems_log  
       fi
       Arc_Location=`arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm location $arcflname`
      fi
      if [ -s "$Arc_Location" ] ; then
       ln -s $Arc_Location ./.$arcflname
       Rm_Arc_List="$Rm_Arc_List ./.$arcflname"
      fi
     fi
     #
     # Adjust accordingly ...
 
     if [ "$nmbr_exstng_copies" -gt 1 ] ; then
      echo "            Target -- $fname -- copy from  --> $arcflname" ; echo ""
      Hold_Dplcts_indufile_list=`clnstrng "$Hold_Dplcts_indufile_list $arcflname" ' '`
     else
      echo "  A match of -- $fname -- is found in --> $arcflname"
     fi
     arc_list=`clnstrng "$arc_list $arcflname" ' '`
     files_in_arc_list=`clnstrng "$files_in_arc_list $fname" ' '`
    else
     indvdl_list=`clnstrng "$indvdl_list $fname" ' '`
    fi
   done
   # echo "files_in_arc_list=$files_in_arc_list"
   # echo "arc_list=$arc_list"
   # echo "indvdl_list=$indvdl_list"
 
   if [ "$Dplcts" = 'yes' ] ; then
    if [ "$allow_r_duplicate" = 'yes' ] ; then
     echo "" ; echo "           Job will continue allowing targeting retrieval of the latest" ; 
     echo "           archived version for datasets with more than one active copy." ; echo ""
     if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
      echo "RETRIEV;...:`whoami`,`date`,......-$$; multiple archived copies found; retrieval of latest version mode is enabled; cfsuser=$cfsuser masterdir=$offcl shorterm=$shorterm ; cwd=`pwd`; Dplcts_list= -> $Dplcts_list <- " >> $CCRNSRC/info/.cfs_problems_log
     fi
    else
     echo "           Please seek assistance from CCCma CFS resource manager if"
     echo "           you can not resolve the duplicate entries conflict in "
     echo "           filenames on your own" ; echo ""
     if [ -n "$Rm_Arc_List" ] ; then
      \rm -f $Rm_Arc_List
     fi
     exit 1 
    fi
   fi
   unset Dplcts Dplcts_list
   unset arcflname
   
   if [ -n "$files_in_arc_list" ] ; then
    noarcmode='no'
    file_list="$arc_list $indvdl_list"
   else
 #  noarcmode='yes'
    file_list="$indvdl_list"
   fi
  fi
 fi
 # echo "" ; echo "noarcmode=$noarcmode"
 # echo "file_list=$file_list"
 # echo "files_in_arc_list=$files_in_arc_list"
 echo " " ; echo ""
 if [ -n "$files_in_arc_list" ] ; then
  First_fname1=`echo $file_list | $AWK '{ print $1 " ... " ; }'`
  First_Last_fname2=`echo $files_in_arc_list | $AWK '{ print $1 " ... " $NF ; }'`
  First_Last_fname="$First_fname1 $First_Last_fname2"
 else
  First_Last_fname=`echo $file_list | $AWK '{ print $1 " ... " $NF ; }'`
 fi
 
 #!!!!!
 
 if [ -z "$arcfile" ] ; then
 
  # Provided specific "_arc" filename is not specified in "arcfile" ...
  # Ensure no duplicate filename for any of the files found in "_arc" 
  # already existed on CFS 
  
  Dplcts='no'
  
  for savdir in $usrdirs
   do
   if [ "$savdir" = "$offcldir" ] ; then
    :
 ##    for fname in $files_in_arc_list
 ##    do
 ##     unset ufile
 ##     if [ -z "$ufile" ] ; then
 ##      # tocrack=`echo $fname | sed -n -e '/^.._.*_.*$/s/_/ /gp' | $AWK '{ print $1 " " $2 ; }'`
 ##      tocrack=`echo $fname | sed -n -e '/^.._.*_.*$/s/_/ /gp' | $AWK '{ f1=$1 ; f2=$2 ; if ( f2 != "ipcc-ddc" ) { i=index(f2,"-") ; if ( i > 1 ) { f2=substr(f2,1,i-1) ; } ; } ; print f1 " " f2 ; }'`
 ##      if [ -n "$tocrack" ] ; then
 ##   
 ##   #   If the filename conform to the standard naming convention,
 ##   #   crack it by "class/run name/type".
 ##   
 ##       tocrack=`echo "$tocrack 99 99"`
 ##       type=`echo $tocrack | $AWK '{ chr = substr($1,1,1) } ; END { print chr ; }' `
 ##       class=`echo $tocrack | $AWK '{ chr = substr($1,2,1) } ; END { print chr ; }' `
 ##   #   runame=`echo $tocrack | $AWK '{ if ( length($2) > 5 ) { print "99" } else { print $2 ; } ; }'`
 ##       runame=`echo $tocrack | $AWK '{ if ( length($2) > 8 ) { print "99" } else { print $2 ; } ; }'`
 ##       if [ "$runame" != '99' ] ; then
 ##        NoSpclCrctr=`echo $tocrack | $AWK '{ print $2 ; }' | sed -e 's/^[a-z][a-z][a-z0-9]*/true/'`
 ##        if [ "$NoSpclCrctr" != 'true' ] ; then
 ##         runame='99'
 ##        fi
 ##        unset NoSpclCrctr
 ##       fi
 ##       case $type in
 ##        m|p|d|t|s) : ;;
 ##        *) unset type
 ##       esac
 ##       case $class in
 ##        a|o|c|m|r|f|d) : ;;
 ##        *) unset class
 ##       esac
 ##       if [ -n "$type" -a -n "$class" -a "$runame" != '99' ] ; then
 ##        ufile=`ssh $arcvmid "hsmls $savdir/$class/$runame/$type/$fname" 2>/dev/null | tr '\\012' '\\040'`
 ##       fi
 ##      fi
 ##      ufile=${ufile:=`ssh $arcvmid "hsmls $savdir/others/$fname" 2>/dev/null | tr '\\012' '\\040'`}
 ##     fi
 ##  
 ##     if [ -n "$ufile" ] ; then
 ##      Dplcts='yes'
 ##      Fffname=`basename $fname`
 ##      echo "" ; echo "  retriev: More than one archived copy of $Fffname is found in the master data directory $arcvmid:$ufile" ; echo ""
 ##      echo "             and in the following '_arc':"
 ## ###
 ##      cat ./.*_arc | sed -n -e "/^${Fffname} /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
 ## ###
 ## ##   if [ -n "$arclabel" ] ; then
 ## ##    arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=$arclabel $Fffname | sed -n -e "/^$Fffname /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
 ## ##   else
 ## ##    # arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm $Fffname | sed -n -e "/^$Fffname /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
 ## ##    arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $Fffname | sed -n -e "/^$Fffname /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
 ## ##   fi
 ##      echo " "
 ##     fi
 ##    done
   else
    if [ -n "$files_in_arc_list" ] ; then
     Tufile_list=`ssh $arcvmid "cd $savdir ; hsmls $files_in_arc_list" 2>/dev/null | tr '\\012' '\\040'`
     if [ -n "Tufile_list" ] ; then
      for Tfname in $Tufile_list
      do
       Dplcts='yes'
       Fffname=`basename $Tfname`
       echo "" ; echo "  retriev: More than one archived copy of $Fffname is found in $arcvmid:$savdir/$Tfname" ; echo ""
       echo "             and in the following '_arc':"
       if [ -n "${Diagdir}" -a -d "${Diagdir}/." -a "${force_output_return}" != 'no' ] ; then touch ${Diagdir}/.force_output_return ; fi
 ###
       cat ./.*_arc | sed -n -e "/^${Fffname} /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
 ###
 ##    if [ -n "$arclabel" ] ; then
 ##     arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=$arclabel $Fffname | sed -n -e "/^$Fffname /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
 ##    else
 ##     # arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm $Fffname | sed -n -e "/^$Fffname /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
 ##     arc_lst own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $Fffname | sed -n -e "/^$Fffname /p" | $AWK ' { print "             ---->  " $3 "  <----" ; }'
 ##    fi
       echo " "
      done
     fi
    fi
   fi
  done
  
  unset Tufile_list
  
  if [ "$Dplcts" = 'yes' ] ; then
   echo "           Please seek assistance from CCCma CFS resource manager if"
   echo "           you can not resolve the duplicate entries conflict in "
   echo "           filenames on your own" ; echo ""
   if [ -n "$Rm_Arc_List" ] ; then
    \rm -f $Rm_Arc_List
   fi
   exit 1
  fi
  unset Dplcts
 
 fi
 #!!!!!
 
 unset indufile_list
 unset ufile_list
 for savdir in $usrdirs
  do
 #echo "scanning savdir=$savdir"
  if [ "$savdir" = "$offcldir" ] ; then
   for fname in $file_list
   do
 #  ufile=`rsh $arcvmid "find ${savdir}/. -name $fname -exec hsmls {} \;" | tail -1 | tr '\\012' '\\040'`
 #  ufile=`rsh $arcvmid "hsmls $savdir/$fname" 2>/dev/null | tr '\\012' '\\040'`
    unset ufile
    if [ -z "$ufile" ] ; then
     #tocrack=`echo $fname | sed -n -e '/^.._.*_.*$/s/_/ /gp' | $AWK '{ print $1 " " $2 ; }'`
     tocrack=`echo $fname | sed -n -e '/^.._.*_.*$/s/_/ /gp' | $AWK '{ f1=$1 ; f2=$2 ; if ( f2 != "ipcc-ddc" ) { i=index(f2,"-") ; if ( i > 1 ) { f2=substr(f2,1,i-1) ; } ; } ; print f1 " " f2 ; }'`
     if [ -n "$tocrack" ] ; then
  
  #   If the filename conform to the standard naming convention,
  #   crack it by "class/run name/type".
  
      tocrack=`echo "$tocrack 99 99"`
      type=`echo $tocrack | $AWK '{ chr = substr($1,1,1) } ; END { print chr ; }' `
      class=`echo $tocrack | $AWK '{ chr = substr($1,2,1) } ; END { print chr ; }' `
 #    runame=`echo $tocrack | $AWK '{ print $2 ; }'`
 #    runame=`echo $tocrack | $AWK '{ if ( length($2) > 5 ) { print "99" } else { print $2 ; } ; }'`
      runame=`echo $tocrack | $AWK '{ if ( length($2) > 8 ) { print "99" } else { print $2 ; } ; }'`
      if [ "$runame" != '99' ] ; then
       NoSpclCrctr=`echo $tocrack | $AWK '{ print $2 ; }' | sed -e 's/^[a-z][a-z][a-z0-9]*/true/'`
       if [ "$NoSpclCrctr" != 'true' ] ; then
        runame='99'
       fi
       unset NoSpclCrctr
      fi
      case $type in
       m|p|d|t|s) : ;;
       *) unset type
      esac
      case $class in
       a|o|c|m|r|f|d) : ;;
       *) unset class
      esac
      if [ -n "$type" -a -n "$class" -a "$runame" != '99' ] ; then
       ufile=`ssh $arcvmid "hsmls $savdir/$class/$runame/$type/$fname" 2>/dev/null | tr '\\012' '\\040'`
      fi
     fi
     ufile=${ufile:=`ssh $arcvmid "hsmls $savdir/others/$fname" 2>/dev/null | tr '\\012' '\\040'`}
 #   ufile=${ufile:=`rsh $arcvmid "find ${savdir}/. -name $fname -exec hsmls {} \;" | tail -1 | tr '\\012' '\\040'`}
    fi
 
    if [ -n "$ufile" ] ; then
 #   ufile_list="$ufile_list $ufile"
     ufile_list=`clnstrng "$ufile_list $ufile" ' '`
 #   indufile_list="$indufile_list $fname"
     indufile_list=`clnstrng "$indufile_list $fname" ' '`
     echo "" ; echo "  retriev: ==> $fname found in the master data directory $arcvmid:$ufile <=="
    fi
   done
  else
   Tufile_list=`ssh $arcvmid "cd $savdir ; hsmls $file_list" 2>/dev/null | tr '\\012' '\\040'`
   if [ -n "Tufile_list" ] ; then
    for Tfname in $Tufile_list
    do
 #   ufile_list="$ufile_list $savdir/$Tfname"
     ufile_list=`clnstrng "$ufile_list $savdir/$Tfname" ' '`
 #   indufile_list="$indufile_list $Tfname"
     indufile_list=`clnstrng "$indufile_list $Tfname" ' '`
     echo "" ; echo "  retriev: ==> $Tfname is found in $arcvmid:$savdir/$Tfname <=="
    done
   fi
  fi
 done
 if [ -z "$ufile_list" ] ; then
  echo "" ; echo "  retriev: Sorry, none of the requested files is found!"
  echo "           Perhaps improper switches setting or wrong filename(s)!"
  if [ -n "$Rm_Arc_List" ] ; then
   \rm -f $Rm_Arc_List
  fi
  exit 3
 fi
 # exit
 #!!!!
 
 # Abort if no matching file is found.
 
 if [ -z "$indufile_list" ] ; then
   echo "" ; echo "  retriev: No match is found for any of the requested file(s)!"
   echo "           Perhaps improper switches setting or wrong filename(s)!"
   xitno=7
 else
   xitno=0
   #
   # Abort if there is a conflict in filename(s) between users's and
   # the master data directories,
   #
 # if [ -n "$indufile_list" ] ; then
   for fname in $indufile_list
   do
    counter=0
    unset Dplcts
    for ssfname in $ufile_list
    do
     sfname=`basename $ssfname`
     if [ "$fname" = "$sfname" ] ; then
       counter=`expr $counter + 1`
       Dplcts="$Dplcts $ssfname"
     fi
    done
    if [ "$counter" -gt 1 ] ; then
      echo "" ; echo "  retriev: located more than one single copy for $fname in:"
      for ssfname in $Dplcts
       do
         echo "           ---> $ssfname <---" ; echo ""
       done
      xitno=9
    fi
   done
 # fi
   Tlist="$ufile_list"
   
   #
   # Check for the existence of all the file(s) asked for on CFS.
   #
   
   for fname in $file_list
   do
    f_found='false'
    for fnd_fname in $indufile_list
    do
     if [ "$fname" = "$fnd_fname" ] ; then
      f_found='true'
      break
     fi
    done
    if [ "$f_found" != 'true' ] ; then
     echo "" ; echo "  retriev: File $fname is not found on $arcvmid"
     xitno=8
    fi
   done
 fi
 #!!!!
 # Retrieve the files ...
 
 if [ $xitno -eq 0 ] ; then
 
  if [ -n "$cfsrlct" ] ; then
   WkDay=`date '+%a'`
   (\rm -f ./${cfsrlct} ./${cfsrlct}_rmdiskcopy || : )
  fi
 
  if [ "$noarcmode" = 'yes' ] ; then
  
   # All files are to be retrieved individually  
   # (none residing in "_arc" file)
 
   # Make sure all needed files are retrievable.
 
    echo "" ; echo "" ; echo "  retriev: List of files on the archive server:" ; echo ""
    ( ssh $arcvmid "hsmls -lds $Tlist" || ssh $arcvmid "hsmls -lds $Tlist" || : )
    # echo "" ; echo "" ; echo "  retriev: Checking for retrievability of the needed files:" ; echo ""
    # ssh $arcvmid "fsavail $Tlist" || xitno=3
    # echo ""
    # if [ "$xitno" -eq 3 ] ; then
    # #echo "Not all requested files are presently retrievable, please retry later"
    # #exit $xitno
    #  echo "Not all requested files are presently retrievable, will attempt the retrieve. If failed, you may have to retry later"
    #  xitno=0
    # fi
  
   # Make sure the needed datafiles are residing on disk.
   
   #date
   #rsh $arcvmid "df -k ${ArcRootPath}/."
   ## rsh $arcvmid "hsmstage $Tlist "
 
   echo $Tlist | ssh $arcvmid "hsmrip - -n 1 -exec hsmcp -p {} ${Cwd}/basename{}"
   # echo $Tlist | ssh $arcvmid "hsmrip - -n 1 -exec hsmcp -p {} ${Cwd}/."
 
   #rsh $arcvmid "hsmls -l $Tlist"
   #rsh $arcvmid "df -k ${ArcRootPath}/."
   #date
    echo " " 
    Cache_sync_file="./.retriev_cache_sync_""`date -u '+%Y%j%H%M%S%s'`""_$$"
    (\rm -f ${Cache_sync_file} || : ) ; echo 'Sync' > ${Cache_sync_file} ; (\rm -f ${Cache_sync_file} || : ) 
    for pfname in $Tlist
    do
    
      #  * Retrieve "$fname".
    
   #  set -x
      fname=`expr //$pfname : '.*/\(.*\)'`
   #  echo "" ; echo "  retriev: retrieving $fname from $arcvmid:$pfname"
      echo "" ; echo "  retriev: Checking retrieved $fname from $arcvmid:$pfname"
      unset Xpctsz
      Actlsz='-1'
      unset chkldsz
   #  chkldsz=`rsh $arcvmid hsmls -l $pfname | $AWK '{ print substr(\$1,1,1) " " \$5  ; }' `
      chkldsz=`ssh $arcvmid hsmls -l $pfname 2>/dev/null | $AWK '{ print substr(\$1,1,1) " " \$5  ; }' `
      unset chkload
   #  Xpctsz=`rsh $arcvmid hsmls -alg $pfname | $AWK '{ print \$5 ; }' `
   #  Xpctsz=`rsh $arcvmid hsmls -al  $pfname | $AWK '{ print \$5 ; }' `
      Xpctsz=`echo $chkldsz |$AWK '{ print \$2 ; }' `
   #  chkload=`rsh $arcvmid hsmls -l $pfname | $AWK '{ print substr(\$1,1,1) ; }' `
   #  chkload=`ssh $arcvmid fsfileinfo $pfname | sed -n -e '/Location:/s/^.* DISK.*$/-/p'`
      chkload=`ssh $arcvmid hsmfi -location -f $pfname | tail -1 | sed -n -e 's/^.* DISK.*$/-/p'`
   #  chkload=`echo $chkldsz |$AWK '{ print \$1 ; }' `
      if [ "$chkload" != '-' ] ; then
       echo "" ; echo "" ; echo "  retriev: Most likely, a problem in loading $fname from tape on $arcvmid"
       echo "           or file diskcopy freed ... " ; echo ""
       date
       ssh $arcvmid "df -k ${ArcRootPath}/ccrn/."
   #   rsh $arcvmid hsmls -l $pfname
       ssh $arcvmid hsmls -l $pfname 2>/dev/null
       ssh $arcvmid hsmfi -f $pfname
       if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
        echo "RETRIEV;1st:`whoami`,`date`,......; no diskcopy of $arcvmid:$pfname present!" >> $CCRNSRC/info/.cfs_problems_log  
       fi
       ssh $arcvmid hsmpurge $pfname
       echo "" ; echo "" ; echo "  retriev: Attempt, for the second time, to reload files from tape on $arcvmid" ; echo ""
       date
       ssh $arcvmid "hsmstage $Tlist "
       ssh $arcvmid "df -k ${ArcRootPath}/ccrn/."
   #   rsh $arcvmid hsmls -l $pfname
       ssh $arcvmid hsmls -l $pfname 2>/dev/null
       ssh $arcvmid hsmfi -f $pfname
       date
       echo "" ; echo "" ; echo "  retriev: Second attempt to retrieve $fname from $arcvmid:$pfname" ; echo ""
       unset Xpctsz
       Actlsz='-1'
       unset chkldsz
   #   chkldsz=`rsh $arcvmid hsmls -l $pfname | $AWK '{ print substr(\$1,1,1) " " \$5  ; }' `
       chkldsz=`ssh $arcvmid hsmls -l $pfname 2>/dev/null | $AWK '{ print substr(\$1,1,1) " " \$5  ; }' `
       unset chkload
   #   Xpctsz=`rsh $arcvmid hsmls -alg $pfname | $AWK '{ print \$5 ; }' `
   #   Xpctsz=`rsh $arcvmid hsmls -al  $pfname | $AWK '{ print \$5 ; }' `
       Xpctsz=`echo $chkldsz |$AWK '{ print \$2 ; }' `
   #   chkload=`rsh $arcvmid hsmls -l $pfname | $AWK '{ print substr(\$1,1,1) ; }' `
   #   chkload=`ssh $arcvmid fsfileinfo $pfname | sed -n -e '/Location:/s/^.* DISK.*$/-/p'`
       chkload=`ssh $arcvmid hsmfi -location -f $pfname | tail -1 | sed -n -e 's/^.* DISK.*$/-/p'`
   #   chkload=`echo $chkldsz |$AWK '{ print \$1 ; }' `
      fi
      if [ "$chkload" != '-' ] ; then
       echo "" ; echo "" ; echo "  retriev: Problem in loading $fname from tape on $arcvmid" ; echo ""
       echo "" ; echo " "
       echo "           ***************************************************************         "
       echo "    **>>>*** PLEASE NOTIFY CCCma CFS SUPPORT PERSONNEL OF THIS OCCURENCE ***<<<**"
       echo "           ***************************************************************       " ; echo ""
       date
       ssh $arcvmid "df -k ${ArcRootPath}/ccrn/."
       ssh $arcvmid hsmls -al $pfname
   #   rsh $arcvmid hsmls -l $pfname
       ssh $arcvmid hsmls -l $pfname 2>/dev/null
       ssh $arcvmid hsmfi -f $pfname
       if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
        echo "RETRIEV;2nd:`whoami`,`date`,SEVERE-$$; no diskcopy of $arcvmid:$pfname present!" >> $CCRNSRC/info/.cfs_problems_log  
       fi
       ssh $arcvmid hsmpurge $pfname
   #   pwd
   #   \ls -al
       \rm -f $fname
   #   touch $fname
      else
   #   rsh $arcvmid hsmls -al $pfname
   ##  rcp -p $arcvmid:$pfname $fname
       Cwd=`pwd`
       msgd_pfname=`echo $pfname | sed -e 's/^\/archive\//\/archive_ro\//'` 
       if [ ! -s "$fname" ] ; then
        ssh $arcvmid "hsmcp -p $msgd_pfname ${Cwd}/${fname} ; chmod u+w ${Cwd}/${fname}"
       fi
       chmod u+w $fname
   #   smcp $arcvmid:$pfname $fname
   #   \ls -al $fname
       Actlsz=`ls -al $fname | $AWK '{ print \$5 ; } '`
       if [ "$Xpctsz" != "$Actlsz" -o ! -s "$fname" ] ; then
        ssh $arcvmid "hsmcp -p $msgd_pfname ${Cwd}/${fname} ; chmod u+w ${Cwd}/${fname}"
        chmod u+w ${Cwd}/${fname}
        Actlsz=`ls -al $fname | $AWK '{ print \$5 ; } '`
       fi
      fi
   
      if [ "$Xpctsz" != "$Actlsz" -o ! -s "$fname" ] ; then
       echo "" ; echo "  retriev: incomplete $fname dataset transfer from $arcvmid:$pfname"
       echo "           please ensure proper job setting and correct filenames were"
       echo "           specified, and retry later"
       pwd
       \ls -al
       xitno=3
       if [ -n "$Rm_Arc_List" ] ; then
        \rm -f $Rm_Arc_List
       fi
       exit $xitno
      else
       chmod u+w $fname
       if [ -n "$fil_grpname" ] ; then
        chgrp $fil_grpname $fname
       fi
 #!!!!!
       if [ -n "$cfsrlct" ] ; then
         trshdir=`expr $pfname : '\(/[^/]*/[^/]*/[^/]*\)'`
         DtaClss=`basename $trshdir`
         if [ "$DtaClss" = 'ccrd_short_term_archive' -o "$DtaClss" = 'ccrd_user_archive' -o "$DtaClss" = 'cnet' -o "$DtaClss" = 'ccrn' ] ; then
          trshdir="${trshdir}/trash/$WkDay"
          touch ./$cfsrlct
          echo "ssh $arcvmid 'mv $pfname $trshdir ' && sleep 1 &&" >> ./$cfsrlct
          echo "echo ' $pfname file is relocated to $trshdir on $arcvmid ' &&" >> ./$cfsrlct
          dskcopyfn=`basename $pfname`
          touch ./${cfsrlct}_rmdiskcopy
          echo "ssh $arcvmid 'hsmpurge $trshdir/$dskcopyfn ' && sleep 1 &&" >> ./${cfsrlct}_rmdiskcopy
          echo "echo ' $arcvmid:$trshdir/$dskcopyfn disk copy is removed ' &&" >> ./${cfsrlct}_rmdiskcopy
         fi
       fi
 #!!!!!
      fi
    done
   #!!!!
   # Final check to make sure all requested files are retrieved.
    
    echo " "
    for fname in $file_list
    do
      if [ ! -s "$fname" ] ; then
       echo "" ; echo "  retriev: File $fname retrieval was unsuccessful!"
       xitno=5
      else
       if [ "$nocheck" != 'yes' ] ; then
        Xpctfp=`cat $ARCDIR/SUM_DIR/active/sumlist | sed -n -e "/\/$fname /p" | sort -k3,3 | tail -1 | $AWK '{print $1}'`
        if [ -n "$Xpctfp" ] ; then
         Actlfp=`md5sum $fname | $AWK '{print $1}' | sed -e 's/ //g' `
         if [ "$Xpctfp" != "$Actlfp" ] ; then
          echo "" ; echo "  retriev: Unmatched -> $fname <- file fingerprint; Xpctfp=$Xpctfp , Actlfp=$Actlfp !"
          if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
           echo "RETRIEV:`whoami`,`date`,SEVERE-$$; unmatched fingerprint for -> $fname <- file" >> $CCRNSRC/info/.cfs_problems_log
          fi
          xitno=6
         fi
         filszck $fname || xitno=6
        else
         filszck $fname || xitno=6
        fi
       fi
      fi
    done
 #!!!!
    if [ -n "$cfsrlct" ] ; then
     if [ -s ./$cfsrlctn ] ; then
      echo 'ExtStats=0 || ExtStats=1 &&' >> ./$cfsrlct
      echo 'if [ "$ExtStats" -ne 0 ] ; then ' >> ./$cfsrlct 
      echo ' abort=abort &&'                     >> ./$cfsrlct
      echo " echo 'Problem encountered in files relocation on $arcvmid ' &&" >> ./$cfsrlct
      echo 'touch haltit &&' >> ./$cfsrlct
      echo " echo 'Problem encountered in files relocation on $arcvmid ' >> haltit && " >> ./$cfsrlct
      echo " (exit 3) " >> ./$cfsrlct
      echo 'fi || (exit 4)'                               >> ./$cfsrlct 
      if [ -s ./${cfsrlct}_rmdiskcopy ] ; then
        echo 'ExtStats=0 || ExtStats=1 &&' >> ./${cfsrlct}_rmdiskcopy
        echo 'if [ "$ExtStats" -ne 0 ] ; then ' >> ./${cfsrlct}_rmdiskcopy
        # echo ' abort=abort &&'                     >> ./${cfsrlct}_rmdiskcopy
        echo " echo 'Problem encountered in removing disk copy on $arcvmid ' &&" >> ./${cfsrlct}_rmdiskcopy
        # echo 'touch haltit &&' >> ./${cfsrlct}_rmdiskcopy
        # echo " echo 'Problem encountered in removing disk copy on $arcvmid ' >> haltit && " >> ./${cfsrlct}_rmdiskcopy
        # echo " (exit 3) " >> ./${cfsrlct}_rmdiskcopy
        echo " : " >> ./${cfsrlct}_rmdiskcopy
        # echo 'fi || (exit 4)'                               >> ./${cfsrlct}_rmdiskcopy
        echo 'fi || (exit 0)'                               >> ./${cfsrlct}_rmdiskcopy
      fi
     else
      echo "Abort: $cfsrlct file was not generated since it seems none of the individual files"
      echo "       resides in the standard subdirectories on $arcvmid"
      xitno=5
     fi
    fi
 #!!!!
   
    xitno=${xitno:-0}
    if [ "$xitno" -eq 0 ] ; then
     if [ ! -s ./${cfsrlct}_rmdiskcopy -a "$keepdskcpy" != 'yes' ] ; then
      ( ssh $arcvmid "hsmpurge $Tlist" || : )
     else
      # DskSpace=`ssh $arcvmid "df ${ArcRootPath}/." 2>/dev/null | tail -1 | sed -e 's/%/ /g' | awk 'BEGIN {} { print $5 ; } END {} ' `
      DskSpace=`ssh $arcvmid "df -P ${ArcRootPath}/ccrn/." 2>/dev/null | tail -1 | sed -e 's/%/ /g' | awk 'BEGIN {} { print $5 ; } END {} ' `
      DskSpace=${DskSpace=100}
    # if [ ! -s ./${cfsrlct}_rmdiskcopy -a "$DskSpace" -gt 10 ] ; then
    # if [ ! -s ./${cfsrlct}_rmdiskcopy -a "$DskSpace" -gt 40 ] ; then
      if [ ! -s ./${cfsrlct}_rmdiskcopy -a "$DskSpace" -gt 85 ] ; then
       ( ssh $arcvmid "hsmpurge $Tlist" || : )
      fi
     fi
    fi
    if [ -n "$Rm_Arc_List" ] ; then
     \rm -f $Rm_Arc_List
    fi
    if [ "$TmnLog" = 'yes' -a -f "$CCRNSRC/arc_dir/log/cfs_retriev_log" ] ; then
      echo "RETRIEV;`whoami`,nocheck=$nocheck,runid=$runid,`hostname`,PID=$$,exit=$xitno,Start: ${Strt_Time}; End: `date` -- RUNPATH=${RUNPATH} -- `pwd`/[${First_Last_fname}] " >> $CCRNSRC/arc_dir/log/cfs_retriev_log 
    fi
    exit $xitno
 
  else
 
   # "noarcmode != yes" case  ...
   # Individual files are to be retrieved from "_arc" file(s) 
   # and/or from a corresponding dataset on the archive server.
 
   Fullindvllist="$indvdl_list $files_in_arc_list" 
 
   echo "" ; echo "" ; echo "  retriev: List of files on the archive server:" ; echo ""
   ( ssh $arcvmid "hsmls -lds $Tlist" || ssh $arcvmid "hsmls -lds $Tlist" || : )
  
   # Make sure all needed files are retrievable.
   # echo "" ; echo "" ; echo "  retriev: Checking for retrievability of the needed files:" ; echo ""
 
   xitno=0
   # ssh $arcvmid "fsavail $Tlist" || xitno=3
   # echo ""
   # if [ "$xitno" -eq 3 ] ; then
   # #echo "Not all requested files are presently retrievable, please retry later"
   # #exit $xitno
   #  echo "Not all requested files are presently retrievable, will attempt the retrieve. If failed, you may have to retry later"
   #  xitno=0
   # fi
 
   ### >> echo $Tlist | cfsrip - -exec "arc_rtrv {} $files_in_arc_list" || xitno=3
     Cwd=`pwd` 
     # echo $Tlist | ssh $arcvmid "hsmrip - -n 1 -exec arc_xtrct trgtdir=$Cwd {} $files_in_arc_list" || xitno=3
 
     # Split "Tlist" into "cmcarc" set in "Tarc_list" and rest in "Tindvdl_list"
 
     Tarc_list='' ; Tindvdl_list=''
     for Tfilnm in $Tlist
      do
       Arc_extnsn=`basename $Tfilnm | tr '[A-Z]' '[a-z]' | sed -n -e 's/^.*_arc$/Yes/p'`
       if [ "$Arc_extnsn" = 'Yes' ] ; then
        Tarc_list="$Tarc_list $Tfilnm"
       else
        Tindvdl_list="$Tindvdl_list $Tfilnm"
       fi
      done
     
     ### Process individual files retrieving and copying
     if [ -n "$Tindvdl_list" ] ; then
      # echo $Tindvdl_list | ssh $arcvmid "cd $Cwd ; pwd ; hsmrip - -n 1 -exec hsmcp -p {} ${Cwd}/basename{}" || xitno=3
      echo $Tindvdl_list | ssh $arcvmid "cd $Cwd ; hsmrip - -n 1 -exec hsmcp -p {} ${Cwd}/basename{}" || xitno=3
      # echo $Tindvdl_list | ssh $arcvmid "cd $Cwd ; hsmrip - -n 1 -exec hsmcp -p {} ${Cwd}/." || xitno=3
     fi
     # (set -x ; pwd ; ls -al )
     chmod u+w *
     ### Process loading "cmcrc" files and extracting files from them
     if [ -n "$Tarc_list" ] ; then
      ## Needed to precede '+' by '\' and protect such occurences (if present)
      files_in_arc_listp=`echo $files_in_arc_list | sed -e 's/[\\]*//g' | sed -e 's/\+/\\\+/g' | sed -e "s/  */' '/g"`
      files_in_arc_listp="'""$files_in_arc_listp""'"
      echo $Tarc_list | ssh $arcvmid "cd $Cwd ; hsmrip - -n 1 -exec hsmcmcarc -f {} -p -m -x $files_in_arc_listp" || xitno=3
     fi
 
     # (set -x ; pwd ; ls -al )
     chmod u+w *
 
   # Re-load files identified as duplicate from the appropriate files in case
   # of presence of bad copy in other valid "arc" file...
 
   if [ "$Hold_Dplcts" = 'yes' -a -n "$Hold_Dplcts_indufile_list" -a -n "$Hold_Dplcts_list" -a "$xitno" -eq 0 ] ; then
    Dplcts_Tlist=''
    Dplcts_files_in_arc_list=''
    # Filter out list of actual datasets on the archive server
    #echo "Tlist=$Tlist"
    #echo "Hold_Dplcts_indufile_list=$Hold_Dplcts_indufile_list"
    #ls -al 
    for pfname in $Hold_Dplcts_indufile_list
     do
      Dplcts_pfname=`basename $pfname`
      for fname in $Tlist
       do
        Dplcts_fname=`basename $fname` 
        if [ "$Dplcts_pfname" = "$Dplcts_fname" ] ; then
         Dplcts_Tlist=`clnstrng "$Dplcts_Tlist $fname" ' '`
        fi
       done
     done
    #echo "Dplcts_Tlist=$Dplcts_Tlist"
    Dplcts_pfname=''
    Dplcts_fname=''
    # Filter out list of files to be extracted from "arc" files.
    #echo "files_in_arc_list=$files_in_arc_list"
    #echo "Hold_Dplcts_list=$Hold_Dplcts_list"
    for pfname in $Hold_Dplcts_list
     do
      Dplcts_pfname=`basename $pfname`
      [ -s "$Dplcts_pfname" ] && chmod u+w $Dplcts_pfname
      for fname in $files_in_arc_list 
       do
        Dplcts_fname=`basename $fname` 
        if [ "$Dplcts_pfname" = "$Dplcts_fname" ] ; then
         Dplcts_files_in_arc_list=`clnstrng "$Dplcts_files_in_arc_list $fname" ' '`
        fi
       done
     done
     #echo "Dplcts_files_in_arc_list=$Dplcts_files_in_arc_list"
     Dplcts_pfname=''
     Dplcts_fname=''
     if [ -n "$Dplcts_Tlist" -a -n "$Dplcts_files_in_arc_list" ] ; then
      # Re-load files identified as having duplicate copies.
      # The aim is to ensure targeted copy is retrieved back.
      # (set -x ; \rm -f $Dplcts_files_in_arc_list || : )
      (\rm -f ${Dplcts_files_in_arc_list} || : )
      ### >> echo $Dplcts_Tlist | cfsrip - -exec "arc_rtrv {} $Dplcts_files_in_arc_list" || xitno=3
      Cwd=`pwd` 
      # echo $Dplcts_Tlist | ssh $arcvmid "hsmrip - -n 1 -exec arc_xtrct trgtdir=$Cwd {} $Dplcts_files_in_arc_list" || xitno=3
  
      # Split "Tlist" into "cmcarc" set in "Tarc_list" and rest in "Tindvdl_list"
  
      Tarc_list='' ; Tindvdl_list=''
      for Tfilnm in $Dplcts_Tlist
       do
        Arc_extnsn=`basename $Tfilnm | tr '[A-Z]' '[a-z]' | sed -n -e 's/^.*_arc$/Yes/p'`
        if [ "$Arc_extnsn" = 'Yes' ] ; then
         Tarc_list="$Tarc_list $Tfilnm"
        else
         Tindvdl_list="$Tindvdl_list $Tfilnm"
        fi
       done
  
      ### Process individual files retrieving and copying
      if [ -n "$Tindvdl_list" ] ; then
       # echo $Tindvdl_list | ssh $arcvmid "cd $Cwd ; pwd ; hsmrip - -n 1 -exec hsmcp -p {} ${Cwd}/basename{}" || xitno=3
       echo $Tindvdl_list | ssh $arcvmid "cd $Cwd ; hsmrip - -n 1 -exec hsmcp -p {} ${Cwd}/basename{}" || xitno=3
      #  echo $Tindvdl_list | ssh $arcvmid "cd $Cwd ; hsmrip - -n 1 -exec hsmcp -p {} ${Cwd}/." || xitno=3
      fi
      ### Process loading "cmcrc" files and extracting files from them
      if [ -n "$Tarc_list" ] ; then
       ## Needed to precede '+' by '\' and protect such occurences (if present)
       Dplcts_files_in_arc_listp=`echo $Dplcts_files_in_arc_list | sed -e 's/[\\]*//g' | sed -e 's/\+/\\\+/g' | sed -e "s/  */' '/g"`
       Dplcts_files_in_arc_listp="'""$Dplcts_files_in_arc_listp""'"
       echo $Tarc_list | ssh $arcvmid "cd $Cwd ; hsmrip - -n 1 -exec hsmcmcarc -f {} -p -m -x $Dplcts_files_in_arc_listp" || xitno=3
      fi
     fi 
   fi
 
   if [ "$xitno" -eq 0 ] ; then
    if [ -n "$files_in_arc_list" ] ; then
     Cache_sync_file="./.retriev_cache_sync_""`date -u '+%Y%j%H%M%S%s'`""_$$"
     (\rm -f ${Cache_sync_file} || : ) ; echo 'Sync' > ${Cache_sync_file} ; (\rm -f ${Cache_sync_file} || : ) 
     for pfname in $files_in_arc_list
      do
       fname=`basename $pfname`
       # filsrchrslt=`cat ./.*_arc | sed -n -e "/^${fname} /p" | sort +1 -3 +3 -4 | tr '[A-Z]' '[a-z]'`
       filsrchrslt=`cat ./.*_arc | sed -n -e "/^${fname} /p" | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'`
       if [ -z "filsrchrslt" ] ; then
       #if [ -n "$arclabel" ] ; then
       # filsrchrslt=`arc_lst sngl own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=$arclabel $fname`
       #else
         # filsrchrslt=`arc_lst sngl own user=$cfsuser masterdir=$offcl shorterm=$shorterm $fname`
         filsrchrslt=`arc_lst sngl own user=$cfsuser masterdir=$offcl shorterm=$shorterm arclabel=arclist $fname`
       #fi
       fi
       if [ -n "$filsrchrslt" ] ; then
        Xpctsz=`echo $filsrchrslt | $AWK '{print $6}'`
        if [ "$nocheck" != 'yes' ] ; then
         Xpctfp=`echo $filsrchrslt | $AWK '{print $8}' | sed -e 's/ //g' `
        fi
        Actlsz=`ls -al $fname | $AWK '{ print \$5 ; } '`
        # A workaround initial bug where size exceeding '2147483647' written with '%12d' as '2147483647'
        if [ "$Xpctsz" = '2147483647' ] ; then
         Xpctsz=`echo Xpctsz Actlsz | $AWK -v Xpctsz=$Xpctsz -v Actlsz=$Actlsz ' { if ( Actlsz > Xpctsz ) { print Actlsz ; } else { print Xpctsz ; } ; }'`
        fi
        if [ "$Xpctsz" != "$Actlsz" -o ! -s "$fname" ] ; then
         echo "" ; echo "" ; echo "  retriev: incomplete $fname dataset transfer from $arcvmid or wrong filename was specified!" ; echo ""
         if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
           Actlsz2=`ls -al $fname | $AWK '{ print \$5 ; } '`
           echo "RETRIEV:`whoami`,`date`,SEVERE-$$; Xpctsz=$Xpctsz ; Actlsz=$Actlsz ; Actlsz2=$Actlsz2 ; problem retrieving $fname via hsmrip!" >> $CCRNSRC/info/.cfs_problems_log
         fi
         xitno=3
        else
         if [ "$nocheck" != 'yes' ] ; then
          if [ -n "$Xpctfp" ] ; then
           Actlfp=`md5sum $fname | $AWK '{print $1}' | sed -e 's/ //g' `
           if [ "$Xpctfp" != "$Actlfp" ] ; then
            echo "" ; echo "  retriev: Unmatched -> $fname <- file fingerprint; Xpctfp=$Xpctfp , Actlfp=$Actlfp !"
            if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
             echo "RETRIEV:`whoami`,`date`,SEVERE-$$; unmatched fingerprint for -> $fname <- file" >> $CCRNSRC/info/.cfs_problems_log
            fi
            xitno=3
           fi
           filszck $fname || xitno=3
          else
           filszck $fname || xitno=3
          fi
         fi
         chmod u+w $fname
         #chmod o-x $fname
         #chmod g+r $fname
         if [ -n "$fil_grpname" ] ; then
          chgrp $fil_grpname $fname
         fi
        fi
       else
        echo "" ; echo "  retriev: Unexpected no match for $fname in any arcfile!"
        xitno=3
       fi
      done
    fi
 
    for pfname in $Tlist
     do
      fname=`basename $pfname`
      Arc_extnsn=`echo $fname | sed -n -e 's/^.*_arc$/Yes/p'`
      if [ "$Arc_extnsn" != 'Yes' ] ; then
       chkldsz=`ssh $arcvmid hsmls -l $pfname 2>/dev/null | $AWK '{ print substr(\$1,1,1) " " \$5  ; }' `
       if [ -z "$chkldsz" ] ; then
        sleep 5
        chkldsz=`ssh $arcvmid hsmls -l $pfname 2>/dev/null | $AWK '{ print substr(\$1,1,1) " " \$5  ; }' `
        if [ ! -z "$chkldsz" -a -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
         echo "RETRIEV;...:`whoami`,`date`,......-$$; failed previously in ssh check on $pfname!" >> $CCRNSRC/info/.cfs_problems_log
        fi
       fi
       Xpctsz=`echo $chkldsz |$AWK '{ print \$2 ; }' `
       Actlsz='-1'
       if [ -s "$fname" ] ; then
        Actlsz=`ls -al $fname | $AWK '{ print \$5 ; } '`
       fi
       if [ "$Xpctsz" != "$Actlsz" -o ! -s "$fname" ] ; then
       #chkload=`ssh $arcvmid fsfileinfo $pfname | sed -n -e '/Location:/s/^.* DISK.*$/-/p'`
        chkload=`ssh $arcvmid hsmfi -location -f $pfname | tail -1 | sed -n -e 's/^.* DISK.*$/-/p'`
        if [ "$chkload" = '-' ] ; then 
         if [ -f "$fname" ] ; then
          chmod u+w $fname
         fi
         Cwd=`pwd`
         msgd_pfname=`echo $pfname | sed -e 's/^\/archive\//\/archive_ro\//'` 
         ssh $arcvmid "hsmcp -p $msgd_pfname ${Cwd}/${fname} ; chmod u+w ${Cwd}/${fname}"
         chmod u+w ${Cwd}/${fname}
         if [ -s "$fname" ] ; then
          Actlsz=`ls -al $fname | $AWK '{ print \$5 ; } '`
         fi
        fi
       fi
       if [ "$Xpctsz" != "$Actlsz" -o ! -s "$fname" ] ; then
        # chkload=`ssh $arcvmid fsfileinfo $pfname | sed -n -e '/Location:/s/^.* DISK.*$/-/p'`
        chkload=`ssh $arcvmid hsmfi -location -f $pfname | tail -1 | sed -n -e 's/^.* DISK.*$/-/p'`
        if [ "$chkload" != '-' ] ; then 
         date
         ssh $arcvmid "df -k ${ArcRootPath}/ccrn/."
         ssh $arcvmid hsmls -l $pfname 2>/dev/null
         ssh $arcvmid hsmfi -f $pfname
        fi
        if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
          echo "RETRIEV:`whoami`,`date`,SEVERE-$$; problem retrieving $pfname via hsmrip!" >> $CCRNSRC/info/.cfs_problems_log
        fi
        echo "" ; echo "" ; echo "  retriev: incomplete $fname dataset transfer from $arcvmid:$pfname or wrong filename was specified" ; echo ""
        xitno=3
       else
        if [ "$nocheck" != 'yes' ] ; then
         Xpctfp=`cat $ARCDIR/SUM_DIR/active/sumlist | sed -n -e "/\/$fname /p" | sort -k3,3 | tail -1 | $AWK '{print $1}'`
         if [ -n "$Xpctfp" ] ; then
          Actlfp=`md5sum $fname | $AWK '{print $1}' | sed -e 's/ //g' `
          if [ "$Xpctfp" != "$Actlfp" ] ; then
           echo "" ; echo "  retriev: Unmatched -> $fname <- file fingerprint; Xpctfp=$Xpctfp , Actlfp=$Actlfp !"
           if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
            echo "RETRIEV:`whoami`,`date`,SEVERE-$$; unmatched fingerprint for -> $fname <- file" >> $CCRNSRC/info/.cfs_problems_log
           fi
           xitno=3
          fi
          filszck $fname || xitno=3
         else
          filszck $fname || xitno=3
         fi
        fi
        chmod u+w $fname
        if [ -n "$fil_grpname" ] ; then
         chgrp $fil_grpname $fname
        fi
       fi
      fi
 
      if [ -n "$cfsrlct" ] ; then
        trshdir=`expr $pfname : '\(/[^/]*/[^/]*/[^/]*\)'`
        DtaClss=`basename $trshdir`
        if [ "$DtaClss" = 'ccrd_short_term_archive' -o "$DtaClss" = 'ccrd_user_archive' -o "$DtaClss" = 'cnet' -o "$DtaClss" = 'ccrn' ] ; then
         trshdir="${trshdir}/trash/$WkDay"
         touch ./$cfsrlct
         echo "ssh $arcvmid 'mv $pfname $trshdir ' && sleep 1 &&" >> ./$cfsrlct
         echo "echo ' $pfname file is relocated to $trshdir on $arcvmid ' &&" >> ./$cfsrlct
         dskcopyfn=`basename $pfname`
         touch ./${cfsrlct}_rmdiskcopy
         echo "ssh $arcvmid 'hsmpurge $trshdir/$dskcopyfn ' && sleep 1 &&" >> ./${cfsrlct}_rmdiskcopy
         echo "echo ' $arcvmid:$trshdir/$dskcopyfn disk copy is removed ' &&" >> ./${cfsrlct}_rmdiskcopy
        fi
      fi
 
     done
 
     if [ -n "$cfsrlct" ] ; then
      if [ -s ./$cfsrlctn ] ; then
       echo 'ExtStats=0 || ExtStats=1 &&' >> ./$cfsrlct
       echo 'if [ "$ExtStats" -ne 0 ] ; then ' >> ./$cfsrlct 
       echo ' abort=abort &&'                     >> ./$cfsrlct
       echo " echo 'Problem encountered in files relocation on $arcvmid ' &&" >> ./$cfsrlct
       echo 'touch haltit &&' >> ./$cfsrlct
       echo " echo 'Problem encountered in files relocation on $arcvmid ' >> haltit && " >> ./$cfsrlct
       echo " (exit 3) " >> ./$cfsrlct
       echo 'fi || (exit 4)'                               >> ./$cfsrlct 
       if [ -s ./${cfsrlct}_rmdiskcopy ] ; then
         echo 'ExtStats=0 || ExtStats=1 &&' >> ./${cfsrlct}_rmdiskcopy
         echo 'if [ "$ExtStats" -ne 0 ] ; then ' >> ./${cfsrlct}_rmdiskcopy
         # echo ' abort=abort &&'                     >> ./${cfsrlct}_rmdiskcopy
         echo " echo 'Problem encountered in removing disk copy on $arcvmid ' &&" >> ./${cfsrlct}_rmdiskcopy
         # echo 'touch haltit &&' >> ./${cfsrlct}_rmdiskcopy
         # echo " echo 'Problem encountered in removing disk copy on $arcvmid ' >> haltit && " >> ./${cfsrlct}_rmdiskcopy
         # echo " (exit 3) " >> ./${cfsrlct}_rmdiskcopy
         echo " : " >> ./${cfsrlct}_rmdiskcopy
         # echo 'fi || (exit 4)'                               >> ./${cfsrlct}_rmdiskcopy
         echo 'fi || (exit 0)'                               >> ./${cfsrlct}_rmdiskcopy
       fi
      else
       echo "Abort: $cfsrlct file was not generated since it seems none of the individual files"
       echo "       resides in the standard subdirectories on $arcvmid"
       if [ -n "$Rm_Arc_List" ] ; then
        \rm -f $Rm_Arc_List
       fi
       exit 3   
      fi
     fi
 
    # Final check to make sure all requested files are retrieved.
 
    # for fname in $Fullindvllist
    #  do
    #   if [ ! -s "$fname" ] ; then
    #    echo "" ; echo "  retriev: File $fname retrieval was unsuccessful!"
    #    xitno=3
    #   else
    #    chmod u+w $fname
    #    if [ -n "$fil_grpname" ] ; then
    #     chgrp $fil_grpname $fname
    #    fi
    #   fi
    #  done
 
   else
 
    echo "" ; echo "  retriev: Problem in retrieving the files"
    echo "           Please report this incidence to CCCma support staff person"
    echo "           once you have doubled checked your submission job"
    date
    ssh $arcvmid "df -k ${ArcRootPath}/ccrn/."
    if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
        echo "RETRIEV;1st:`whoami`,`date`,SEVERE-$$; problem retrieving files via hsmrip!" >> $CCRNSRC/info/.cfs_problems_log
    fi
   fi
   if [ -n "$xitno" -a "$xitno" -ne 0 ] ; then  
     echo "" ; echo "  retriev: Provided no detected data corruption was reported,"
     echo "           you may retry retrieval later on"
     pwd
     \ls -al
     if [ -n "$Rm_Arc_List" ] ; then
      \rm -f $Rm_Arc_List
     fi
     exit $xitno
   else
     if [ ! -s ./${cfsrlct}_rmdiskcopy -a "$keepdskcpy" != 'yes' ] ; then
      ( ssh $arcvmid "hsmpurge $Tlist" || : )
     else
      # DskSpace=`ssh $arcvmid "df ${ArcRootPath}/." 2>/dev/null | tail -1 | sed -e 's/%/ /g' | awk 'BEGIN {} { print $5 ; } END {} ' `
      DskSpace=`ssh $arcvmid "df -P ${ArcRootPath}/ccrn/." 2>/dev/null | tail -1 | sed -e 's/%/ /g' | awk 'BEGIN {} { print $5 ; } END {} ' `
      DskSpace=${DskSpace=100}
    # if [ ! -s ./${cfsrlct}_rmdiskcopy -a "$DskSpace" -gt 10 ] ; then
    # if [ ! -s ./${cfsrlct}_rmdiskcopy -a "$DskSpace" -gt 40 ] ; then
      if [ ! -s ./${cfsrlct}_rmdiskcopy -a "$DskSpace" -gt 85 ] ; then
       ( ssh $arcvmid "hsmpurge $Tlist" || : )
      fi
     fi
   fi
 
  fi
  if [ -n "$Rm_Arc_List" ] ; then
   # echo "Rm_Arc_List=$Rm_Arc_List"
   # ls -ld $Rm_Arc_List
   \rm -f $Rm_Arc_List
  fi
  if [ "$TmnLog" = 'yes' -a -f "$CCRNSRC/arc_dir/log/cfs_retriev_log" ] ; then
    echo "RETRIEV;`whoami`,nocheck=$nocheck,runid=$runid,`hostname`,PID=$$,exit=$xitno,Start: ${Strt_Time}; End: `date` -- RUNPATH=${RUNPATH} -- `pwd`/[${First_Last_fname}] " >> $CCRNSRC/arc_dir/log/cfs_retriev_log 
  fi
 
 else
 
 #if [ $xitno -eq 8 ] ; then
 # if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
 #   ( echo "RETRIEV;`whoami`,`date`,`hostname`,SEVERE-$$,xitno=$xitno,cwd=`pwd`:" ; ls -al ; echo "... file_list=$file_list" ; echo "... indufile_list=$indufile_list" ; echo "... arc_list=$arc_list"; echo "... indvdl_list=$indvdl_list" ; echo "... files_in_arc_list=$files_in_arc_list" ; echo "......END......" ) >> $CCRNSRC/info/.cfs_problems_log
 # fi
 #fi
 
  if [ -f "$CCRNSRC/info/.cfs_problems_log" ] ; then
   echo "RETRIEV;`whoami`,`date`,`hostname`,SEVERE-$$,xitno=$xitno,cwd=`pwd`" >> $CCRNSRC/info/.cfs_problems_log
  fi
 
  echo "" ; echo "  retriev: Aborts due to user filename(s) conflict with file(s) in the master data directory"
  echo "           and/or requested file(s) not found on $arcvmid"
  echo "           and/or multiple matching datasets are found on $arcvmid"
  echo "           or perhaps improper settings in the submission job"
  if [ -n "$Rm_Arc_List" ] ; then
   \rm -f $Rm_Arc_List
  fi
  if [ "$TmnLog" = 'yes' -a -f "$CCRNSRC/arc_dir/log/cfs_retriev_log" ] ; then
    echo "RETRIEV;`whoami`,nocheck=$nocheck,runid=$runid,`hostname`,PID=$$,exit=$xitno,Start: ${Strt_Time}; End: `date` -- RUNPATH=${RUNPATH} -- `pwd`/[${First_Last_fname}] " >> $CCRNSRC/arc_dir/log/cfs_retriev_log 
  fi
  exit $xitno
 
 fi
fi
