#! /bin/sh

#    Nov 23/04 - F.Majaess (Added "nostrip" switch support)
#    Oct 15/04 - F.Majaess (Speedup by narrowing down the "offcl" subdirectory
#                           by cracking the specified filename)
#    May 26/04 - F.Majaess

#id arc_lst - List or locate files under "arc_dir" subdirectory.

#   AUTHOR  - F.Majaess

#hd PURPOSE - "arc_lst" script is used list or locate files under official 
#hd           "arc_dir" front-end subdirectory.

#pr  PARAMETERS:
#pr
#pr   POSITIONAL
#pr
#pr      file   = target filename, 
#pr               behaviour follows the rules in the following table:
#pr
#pr                              "location" 
#pr               "file"           switch    action 
#pr               ======         ==========  ======
#pr
#pr               not "arclist"
#pr               nor "*_arc"    "ignored"   search for indiviual "file" 
#pr                                          filename in matching "_arc" 
#pr                                          files   
#pr               "arclist" or
#pr               "*_arc"        no          list contents of matching
#pr                                          "file"
#pr
#pr               "arclist" or
#pr               "_arc"         yes         report location(s) of 
#pr                                          matching "file"
#pr
#pr
#pr      dirs   = full path target subdirectories (optional)
#pr               If specified, it'll override the resulting target
#pr               subdirectories to scan which otherwise got determined
#pr               based on the other key switches specified.
#pr
#pr
#pr   PRIMARY
#pr
#pr       user = username - used only in conjunction with the "own"
#pr              parameter below (=$USER)
#pr              (ignored if "full=yes")
#pr
#pr   arclabel = Parameter valid only when searching for individual file(s)
#pr              where it'll control limiting the search to "_arc" files 
#pr              with matching "arclabel" substring in the filename except
#pr              for the case of "arclabel=arclist" in which case the search
#pr              will be confined to "arclist" files instead.
#pr              Note: The combined information from all active "_arc" files 
#pr                    in a given subdirectory are collected into "arclist" 
#pr                    file  under the same subdirectory. Making use of 
#pr                    "arclabel=arclist" would result in considerable
#pr                    speedup in searching for individual files instead
#pr                    of doing the search in "*_arc" files.
#pr              (no default value)
#pr
#pr   SECONDARY
#pr
#pr   location = switch to report the location of "file".
#pr              (honoured only if "file" is "arclist" or has "_arc" suffix)
#pr              (=no/yes)
#pr   indvdlst = switch to request returning just the list of individual
#pr              files contained in the "arcfile" searched for.
#pr              (honoured only if "file" contains "_arc" suffix)
#pr              (=no/yes)
#pr    nostrip = switch to control not stripping out "newline" characters 
#pr              when returning the list of individual files contained in 
#pr              the "arcfile" searched for.
#pr              Default is to return the list in a single line.
#pr              (honoured only if "indvdlst=yes")
#pr              (=no/yes)
#pr       sngl = switch valid only when searching for individual file(s) 
#pr              where it'll control limiting the returned output to a 
#pr              single entry (of exact match) or not (default).
#pr              (=no/yes)
#pr       full = switch to scan the entire "arc_dir" tree subdirectories.
#pr              It should be used in conjunction with appropriate "arclabel"
#pr              setting being specified (otherwise, it 's quite search load 
#pr              extensive). The least "arclabel=arclist" should be specified
#pr              unless it's justified not to.
#pr              ("user","masterdir" and "shorterm" switches described 
#pr               below are ignored if "full=yes")
#pr              (=no/yes)
#pr        own = switch to scan only user's associated subdirectories
#pr              instead of the subdirectories associated with a given
#pr              dataclass (on CFS), or the whole "arc_dir" tree 
#pr              subdirectories (if "full=yes").
#pr   masterdir= switch controls targeting only the associated subdirectories
#pr              with the master data subdirectory dataclass (on CFS) instead
#pr              of associated user's own directories as well. This switch 
#pr              overrides "shortermdir" (described next) setting.
#pr              (ignored if "full=yes")
#pr              (no default value; valid settings "no/yes")
#pr   shorterm = switch controls targeting user's associated subdirectories
#pr              with the short term dataclass (on CFS) instead of user's
#pr              associated default subdirectory.
#pr              (ignored if "full=yes")
#pr              (no default value; valid settings "no/yes")
#pr
#pr    The outcome from "masterdir" and/or "shortermdir" switch settings can
#pr    be described as follows (provided "full" switch is not set to "yes"):
#pr
#pr      masterdir    shortermdir     targeted directory for "file" scanning
#pr      ---------    -----------     --------------------------------------
#pr      unset        unset           user's default,short term
#pr                                   and official master
#pr      unset        no              user's default    & official master
#pr      unset        yes             user's short term & official master
#pr      no           unset           user's default    & short term
#pr      no           no              user's default    only
#pr      no           yes             user's short term only
#pr      yes          unset/no/yes    official master   only
#pr

#ex  EXAMPLE:
#ex  
#ex 
#ex arc_lst user=acrnxxx own ma_abc_001_m06_ss
#ex  
#ex The above will result in searching for "ma_abc_001_m06_ss" 
#ex individual file entry in all "_arc" files found under the
#ex associated subdirectories with "acrnxxx" account.
#ex 
#ex arc_lst user=acrnxxx own shorterm=no masterdir=no \
#ex              arclabel='ma_abc_001' ma_abc_001_m06_ss
#ex  
#ex Limit the search for "ma_abc_001_m06_ss" to '*ma_abc_001*_arc' 
#ex files under the user's associated default subdirectory.
#ex  
#ex arc_lst user=acrnxxx own shorterm=no masterdir=no \
#ex              arclabel='arclist' ma_abc_001_m06_ss
#ex  
#ex Limit the search for "ma_abc_001_m06_ss" to the "arclist"
#ex files under the user's associated default subdirectory.
#ex The combined information from all active "_arc" files in 
#ex a given subdirectory are collected into "arclist" file 
#ex under the same subdirectory.
#ex the the collection
#ex  
#ex arc_lst uxxx_ma_abc_001_arc
#ex  
#ex List contents of "uxxx_ma_abc_001_arc" file. Associated subdirectories
#ex with all dataclasses where the invoker user's account is allowed to check
#ex are scanned.
#ex  
#ex arc_lst full uxxx_ma_abc_001_arc location
#ex  
#ex Reports location(s) of "uxxx_ma_abc_001_arc" file. The whole "arc_dir" tree
#ex subdirectory structure is scanned to locate "uxxx_ma_abc_001_arc" file.
#ex  
#ex arc_lst full arclabel=ma_abc_001 ma_abc_001_m06 
#ex  
#ex Search for all individual files with "ma_abc_001_m06" prefix. The whole 
#ex "arc_dir" tree subdirectory structure is scanned with parsing limited to
#ex files whose filename contains "ma_abc_001" substring.
#ex  
#ex arc_lst full sngl arclabel=arclist ma_abc_001_m06_gs 
#ex  
#ex Search for exact "ma_abc_001_m06_gs" file match with the output limited 
#ex to the last single entry (in case of multiple matches are found). The 
#ex whole "arc_dir" tree subdirectory structure is scanned with parsing 
#ex limited to "arclist" files.
#ex  
#ex arc_lst indvdlst uxxx_ma_abc_001_arc $CCRNSRC/arc_dir/active/ccrd_user_archive/xxx
#ex  
#ex Only the list of individual files, contained in "uxxx_ma_abc_001_arc" file under
#ex $CCRNSRC/arc_dir/active/ccrd_user_archive/xxx subdirectory, is returned in a single 
#ex line format.
#ex  
#ex arc_lst indvdlst nostrip uxxx_ma_abc_001_arc $CCRNSRC/arc_dir/active/ccrd_user_archive/xxx
#ex  
#ex Only the list of individual files, contained in "uxxx_ma_abc_001_arc" file under
#ex $CCRNSRC/arc_dir/active/ccrd_user_archive/xxx subdirectory, is returned in a 
#ex one line per entry format.
#ex  
#ex arc_lst own masterdir=off shorterm=off arclist
#ex 
#ex List contents of "arclist" file found under the associated subdirectory with
#ex the user's long term subdirectory on CFS.
#ex  
#ex arc_lst masterdir=on arclabel=mc_igm_xx arclist | sed -n -e '/^mc_igm_.*[rco]s /p' | less
#ex 
#ex Produces a list of archived "rs,cs,os" restart files associated with "igm" CGCM run.
#ex 

# set -x

IFS=' '
export IFS
unset Filex dirs nostrip

#  * Obtain specified options, if any.

for arg in $@
do
  case $arg in
      *=*) eval $arg                ;;
       -*) set $arg                 ;;
      own) own=yes                  ;;
     full) full=yes                 ;;
 location) location=yes             ;;
 indvdlst) indvdlst=yes             ;;
  nostrip) nostrip=yes              ;;
     sngl) sngl=yes                 ;;
 shorterm) shorterm=yes             ;;
 masterdir) masterdir=yes           ;;
 timesort) timesort=yes             ;;
#       *) Filex=${Filex:-$arg}
        *) if [ -z "$Filex" ] ; then
             Filex="$arg"
           else
             if [ -z "$dirs" ] ; then
              dirs="$arg"
             else
              dirs="$dirs $arg"
             fi
           fi
  esac
done

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

AWK=${AWK:='awk'}

#  * Set the defaults.

user=${user:=$USER}
if [ -n "$user" ] ; then
 Lgname=`echo $user | tr '[A-Z]' '[a-z]'`
 LgnameEC="$Lgname"
else
 # Lgname=`logname`
 Lgname=`whoami`
 if [ "$SITE_ID" = 'DrvlSC' ] ; then
   LgnameEC=`accmap $Lgname`
 else
   LgnameEC="$Lgname"
 fi
fi
usr=`echo $LgnameEC | sed 's/^....//'`
grp=`echo $LgnameEC | sed 's/...$//'`
location=${location:='no'}
indvdlst=${indvdlst:='no'}
nostrip=${nostrip:='no'}
sngl=${sngl:='no'}
# shorterm=${shorterm:='no'}
# masterdir=${masterdir:='no'}
if [ -n "$masterdir" -a "$masterdir" = 'on' ] ; then
 masterdir='yes'
fi
if [ -n "$masterdir" -a "$masterdir" = 'off' ] ; then
 masterdir='no'
fi
if [ -n "$shorterm" -a "$shorterm" = 'on' ] ; then
 shorterm='yes'
fi
if [ -n "$shorterm" -a "$shorterm" = 'off' ] ; then
 shorterm='no'
fi
offcl="$masterdir"
full=${full:='no'}
own=${own:='no'}
if [ "$SITE_ID" = 'DrvlSC' -a "$OS" = 'Linux' -a -s /home/scrd101/bin/gnufind ] ; then
 eval FiNd='/home/scrd101/bin/gnufind'
elif [ "$SITE_ID" = 'Dorval' -a "$OS" = 'Linux' -a -s '/users/tor/acrn/src/bin_linux/gnufind' ] ; then
 FiNd='/users/tor/acrn/src/bin_linux/gnufind'
else
 FiNd='find'
fi
FiNd="$FiNd -L"

# Abort if no target file is specified...

if [ -z "$Filex" ] ; then
 echo "arc_lst: Sorry, you need to specify a target file!"
 exit 1
else
 Filex=`echo $Filex | tr '[A-Z]' '[a-z]'`
fi

# * Set default target path:
if [ "$SITE_ID" = 'DrvlSC' ] ; then
 ARCDIR=${ARCDIR:-"/home/scrd101/arc_dir"}
 eval ARCDIR="$ARCDIR"
elif [ "$SITE_ID" = 'Dorval' ] ; then
 ARCDIR=${ARCDIR:-"/users/tor/acrn/src/arc_dir"}
else
 ARCDIR=${ARCDIR:-"$CCRNSRC/arc_dir"}
fi
arcdirpth=${arcdirpth:-"$ARCDIR"}

# * Abort if "arcdirpth" subdirectory does not exist...

if [ ! -d "$arcdirpth/." ] ; then
  echo "arc_lst: $arcdirpth/. does not exist!"
  exit 1
fi


Arc_file=`echo $Filex | sed -n -e 's/^.*_arc$/Yes/p'`
if [ "$Arc_file" != 'Yes' -a "$indvdlst" = 'yes' ] ; then
 # indvdlst='no'
 echo "arc_lst: Illegal use of indvdlst=yes setting!"
 exit 1
fi
if [ "$Arc_file" != 'Yes' -a "$indvdlst" != 'yes' -a "$nostrip" = 'yes' ] ; then
 # nostrip='no'
 echo "arc_lst: Illegal use of nostrip=yes setting!"
 exit 2
fi
HldSngl=''
if [ "$full" = 'yes' ] ; then
  dirs=${dirs:-"$arcdirpth/active"}
  # set -x
  for Cmcarc_dir in $dirs
  do
    if [ -d "$Cmcarc_dir/." ] ; then
     cd $Cmcarc_dir/.
     if [ "$Arc_file" = 'Yes' -o "$Filex" = 'arclist' ] ; then
     #set -x
      if [ "$location" = 'yes' ] ; then
      #$FiNd ./ -name "$Filex" -print 
       $FiNd $Cmcarc_dir -name "$Filex" -exec \ls -1 {} \;
      else
       if [ "$indvdlst" = 'yes' -a "$Arc_file" = 'Yes' ] ; then
        if [ "$nostrip" = 'yes' ] ; then
         ( $FiNd ./ -name "$Filex" -exec cat {} \; ) 2>>/dev/null | $AWK '{ print $1 }'
        else
         IndvdlList=`( $FiNd ./ -name "$Filex" -exec cat {} \; ) 2>>/dev/null | $AWK '{ print \$1 }' | tr '\\012' '\\040'`
         if [ -n "$IndvdlList" ] ; then
          echo "$IndvdlList"
         fi
        fi
       else
        $FiNd ./ -name "$Filex" -exec cat {} \;
       fi
      fi
     else
      if [ -n "$arclabel" ] ; then
       if [ "$arclabel" = 'arclist' ] ; then
        Arclabel="'arclist'"
       else
        Arclabel=`echo $arclabel | tr '[A-Z]' '[a-z]' | sed -n -e 's/^.*_arc$/Yes/p'`
        if [ "$Arclabel" = 'Yes' ] ; then
         Arclabel="'"'*'"$arclabel""'"
        else
         Arclabel="'"'*'"$arclabel"'*_arc'"'"
        fi
       fi
      else
       Arclabel="'"'*_arc'"'"
      fi
      if [ "$sngl" = 'no' ] ; then
       Strng="'/^""${Filex}""/p'"
       if [ "$Arclabel" = "'arclist'" -a -s "./arclist" ] ; then
        # eval "( cat ./arclist | sed -n -e ${Strng} ) | sort +1 -3 +3 -4 | tr '[A-Z]' '[a-z]'"
        eval "( cat ./arclist | sed -n -e ${Strng} ) | sort -k2,3 -k4,4 | tr '[A-Z]' '[a-z]'"
       else
        # eval "( $FiNd ./ -name $Arclabel -exec sed -n -e ${Strng} {} \; ) | sort +1 -3 +3 -4 | tr '[A-Z]' '[a-z]'"
        eval "( $FiNd ./ -name $Arclabel -exec sed -n -e ${Strng} {} \; ) | sort -k2,3 -k4,4 | tr '[A-Z]' '[a-z]'"
       fi
      else
       Strng="'/^""${Filex}"" /p'"
       if [ "$Arclabel" = "'arclist'" -a -s "./arclist" ] ; then
        # eval "( cat ./arclist | sed -n -e ${Strng} ) | sort +1 -3 +3 -4 | tail -1 | tr '[A-Z]' '[a-z]'"
       #eval "( cat ./arclist | sed -n -e ${Strng} ) | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'"
        HldSnglx=`eval "( cat ./arclist | sed -n -e ${Strng} ) | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'"`
        if [ -n "$HldSnglx" ] ; then
         if [ -z "$HldSngl" ] ; then
          HldSngl="$HldSnglx"
         else
          HldSngl=` (echo "$HldSngl" ; echo "$HldSnglx") |  sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'`
         fi
        fi
       else
        # eval "( $FiNd ./ -name $Arclabel -exec sed -n -e ${Strng} {} \; ) | sort +1 -3 +3 -4 | tail -1 | tr '[A-Z]' '[a-z]'"
       #eval "( $FiNd ./ -name $Arclabel -exec sed -n -e ${Strng} {} \; ) | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'"
        HldSnglx=`eval "( $FiNd ./ -name $Arclabel -exec sed -n -e ${Strng} {} \; ) | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'"`
        if [ -n "$HldSnglx" ] ; then
         if [ -z "$HldSngl" ] ; then
          HldSngl="$HldSnglx"
         else
          HldSngl=` (echo "$HldSngl" ; echo "$HldSnglx") |  sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'`
         fi
        fi
       fi
      fi
     fi
    fi
  done
  if [ -n "$HldSngl" ] ; then
   echo "$HldSngl"
   HldSngl=''; HldSnglx=''
  fi
else
#!!!!!
  # Not "full=yes" case
  
  if [ -z "$dirs" ] ; then

   # Setup in "usrdirs" the subdirectories to scan taking into
   # account "masterdir" and "shorterm" switches settings...
 
   # User's default ...
   
   unset usrdirs
 
   if [ "$shorterm" != 'yes' -a "$offcl" != 'yes' ] ; then
     ### if [ "$Lgname" = 'acrpmdm' -o "$Lgname" = 'acrmril' ] ; then
     ### if [ "$Lgname" = 'acrpxxx' -o "$Lgname" = 'acrmxxx' ] ; then
     # if [ "$Lgname" = 'acrp001' -o "$Lgname" = 'acrplbw' -o "$Lgname" = 'acrpssu' -o "$Lgname" = 'acrmrmc' -o "$Lgname" = 'acrmrac' ] ; then
     # if [ "$Lgname" = 'acrplbw' -o "$Lgname" = 'acrpssu' -o "$Lgname" = 'acrmrmc' -o "$Lgname" = 'acrmrac' ] ; then
     if [ "$LgnameEC" = 'acrplbw' -o "$LgnameEC" = 'acrprfb' -o "$LgnameEC" = 'acrprgm' -o "$LgnameEC" = 'acrpssu' -o "$LgnameEC" = 'acrmrmc' -o "$LgnameEC" = 'acrmrac' ] ; then
       usrdirs=${Lgname_dir:="$arcdirpth/active/cnet/$grp/$usr"}
      if [ "$own" != 'yes' ] ; then
       usrdirs=`dirname $usrdirs`
      fi
     else
      if [ "$grp" = 'acrn' ] ; then
       case $usr in
                                 src ) usrdirs="$arcdirpth/active/ccrn/$usr"  ;;
        nys|rbd|rfp|rmh|yam|yjk) usrdirs="$arcdirpth/active/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="$arcdirpth/active/ccrd_user_archive/$usr"  ;;
        mr1|mr2|mr3|mr4|mr5|mr6|mr7|mr8|mr9|pd1|pd2|pd3|pd4|pd5|pd6|pd7|pd8|pd9) usrdirs="$arcdirpth/active/ccrd_user_archive/$usr"  ;;
        01[0-9]|020) usrdirs="$arcdirpth/active/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="$arcdirpth/active/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="$arcdirpth/active/ccrd_user_archive/$usr"  ;;
        rrd|rhb|rec|rdv|jpb|vrm ) usrdirs="$arcdirpth/active/cnet/$usr"  ;;
        009|c02|y01|y03|y04|y06|y07|y09|yww|ydc|ysb|yvf|ycm ) usrdirs="$arcdirpth/active/cnet/$usr"  ;;
        #q0[1-7]|qrj|qaf|qdc|qdp|qhc|qjf|qmg|qsb ) usrdirs="$arcdirpth/active/cnet/$usr"  ;;
         qmg ) usrdirs="$arcdirpth/active/cnet/$usr"  ;;
                                   * ) # echo "" ; echo "  arc_lst: your account is not valid, usr=$usr" 
                                       exit 1 ;;
       esac
       if [ "$own" != 'yes' -a -n "$usrdirs" ] ; then
        usrdirs=`dirname $usrdirs`
       fi
      else
       if [ -n "$Lgname_dir" ] ; then
        usrdirs="$Lgname_dir"
       fi
      fi
     fi
   fi
   
   # User's short term ...
   
   unset shrtmdir
   if [ "$shorterm" != 'no' -a "$offcl" != 'yes' ] ; then
     ### if [ "$Lgname" = 'acrpmdm' -o "$Lgname" = 'acrmril' ] ; then
     ### if [ "$Lgname" = 'acrpxxx' -o "$Lgname" = 'acrmxxx' ] ; then
     # if [ "$Lgname" = 'acrp001' -o "$Lgname" = 'acrplbw' -o "$Lgname" = 'acrpssu' -o "$Lgname" = 'acrmrmc' -o "$Lgname" = 'acrmrac' ] ; then
     if [ "$LgnameEC" = 'acrp001' -o "$LgnameEC" = 'acrplbw' -o "$LgnameEC" = 'acrprfb' -o "$LgnameEC" = 'acrprgm' -o "$LgnameEC" = 'acrpssu' -o "$LgnameEC" = 'acrmrmc' -o "$LgnameEC" = 'acrmrac' ] ; then
      shrtmdir="$arcdirpth/active/ccrd_short_term_archive/$grp/$usr"
      if [ "$own" != 'yes' ] ; then
       shrtmdir=`dirname $shrtmdir`
      fi
     else
      if [ "$grp" = 'acrn' ] ; then
       case $usr in
                                 src ) shrtmdir=""  ;;
        nys|rbd|rfp|rmh|yam|yjk) shrtmdir="$arcdirpth/active/ccrd_long_term_archive/$usr"  ;;
        001|005|006|002|007|003|mcr|mky|all|cbn|ocn|opt|sip|cc1|cc2|cc3|cc4|esm|gcm|ice|rdr|rdy|rfm|rgb) shrtmdir="$arcdirpth/active/ccrd_short_term_archive/$usr"  ;;
        mr1|mr2|mr3|mr4|mr5|mr6|mr7|mr8|mr9|pd1|pd2|pd3|pd4|pd5|pd6|pd7|pd8|pd9) shrtmdir="$arcdirpth/active/ccrd_short_term_archive/$usr"  ;;
        01[0-9]|020) shrtmdir="$arcdirpth/active/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="$arcdirpth/active/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="$arcdirpth/active/ccrd_short_term_archive/$usr"  ;;
        rrd|rhb|rec|rdv|jpb|vrm ) shrtmdir="$arcdirpth/active/ccrd_short_term_archive/$usr"  ;;
        # 001|009|cop|y0[1-9]|yww|yjd|ycm|ydc|ysb|yvf ) shrtmdir="$arcdirpth/active/ccrd_short_term_archive/$usr"  ;;
        009|ycm ) shrtmdir="$arcdirpth/active/ccrd_short_term_archive/$usr"  ;;
        #q0[1-7]|qrj|qaf|qdc|qdp|qhc|qjf|qmg|qsb ) shrtmdir="$arcdirpth/active/ccrd_short_term_archive/$usr"  ;;
                                   * ) # echo "" ; echo "  arc_lst: your account is not valid, usr=$usr" 
                                       exit 2 ;;
       esac
       if [ "$own" != 'yes' -a -n "$shrtmdir" ] ; then
        shrtmdir=`dirname $shrtmdir`
       fi
      fi
     fi
     usrdirs=`clnstrng "$usrdirs $shrtmdir" ' '`
   fi
   ###
 
   # Official subdirectory...

   offcldir="$arcdirpth/active/ccrn/offcl_data"

   if [ -n "$arclabel" -a "$arclabel" != 'arclist' ] ; then
    # Filey="'""$arclabel""'"
    Filey="$arclabel"
   else
    Filey="$Filex"
   fi
 
   # Make use of "extra_scan" switch to control allowing scan of all "arclist"s under a given
   # run name subdirectories instead of just the ones associated with particular "m,d,p,s,..." 
   # data type.

   extra_scan='no'
   if [ "$offcl" != no -a "$Filey" != 'arclist' ] ; then
   #tocrack=`echo $Filey | sed -n -e '/^.._.*_.*$/s/_/ /gp' | $AWK '{ print $1 " " $2 ; }'`
    tocrack=`echo $Filey | 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
     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
      offcldir="$arcdirpth/active/ccrn/offcl_data/$class/$runame/$type"
      if [ "$arclabel" = 'arclist' -o ! -d "$offcldir/." ] ; then
       # offcldir=`echo $arcdirpth/active/ccrn/offcl_data/$class/$runame/*`
       Toffclarclist=`echo $arcdirpth/active/ccrn/offcl_data/$class/$runame/*/arclist | $AWK '{print $1;}' `
       if [ -f "$Toffclarclist" ] ; then
        extra_scan='yes'
        offcldir=`dirname $Toffclarclist`
       fi
      fi
     else
      offcldir="$arcdirpth/active/ccrn/offcl_data/others"
     fi 
    else
     offcldir="$arcdirpth/active/ccrn/offcl_data/others"
    fi
   fi
   if [ "$offcl" = 'yes' ] ; then
     usrdirs=$offcldir
   elif [ "$offcl" != 'no' ] ; then
     usrdirs=`clnstrng "$usrdirs $offcldir" ' '`
   fi
   dirs="$usrdirs"
  fi

  # Scan each of subdirectory specified in "usrdirs".
  HldSngl=''
  # for Cmcarc_dir in $usrdirs
  for Cmcarc_dir in $dirs
  do
   if [ -d "$Cmcarc_dir/." ] ; then
    # echo "Scanning $Cmcarc_dir :"
    cd $Cmcarc_dir/.
    if [ "$Arc_file" = 'Yes' -o "$Filex" = 'arclist' ] ; then
     if [ "$location" = 'yes' ] ; then
     #$FiNd ./ -name "$Filex" -print 
      $FiNd $Cmcarc_dir -name "$Filex" -exec \ls -1 {} \;
     else
      if [ "$indvdlst" = 'yes' -a "$Arc_file" = 'Yes' ] ; then
       if [ "$nostrip" = 'yes' ] ; then
        ( $FiNd ./ -name "$Filex" -exec cat {} \; ) 2>>/dev/null | $AWK '{ print $1 }'
       else
        IndvdlList=`( $FiNd ./ -name "$Filex" -exec cat {} \; ) 2>>/dev/null | $AWK '{ print \$1 }' | tr '\\012' '\\040'`
        if [ -n "$IndvdlList" ] ; then
         echo "$IndvdlList"
        fi
       fi
      else
       $FiNd ./ -name "$Filex" -exec cat {} \;
      fi
     fi
    else
     if [ -n "$arclabel" ] ; then
      if [ "$arclabel" = 'arclist' ] ; then
       Arclabel="'arclist'"
      else
       Arclabel=`echo $arclabel | tr '[A-Z]' '[a-z]' | sed -n -e 's/^.*_arc$/Yes/p'`
       if [ "$Arclabel" = 'Yes' ] ; then
        Arclabel="'"'*'"$arclabel""'"
       else
        Arclabel="'"'*'"$arclabel"'*_arc'"'"
       fi
      fi
     else
      Arclabel="'"'*_arc'"'"
     fi
     if [ "$sngl" = 'no' ] ; then
      Strng="'/^""${Filex}""/p'"
      if [ "$Arclabel" = "'arclist'" -a -s "./arclist" ] ; then
       # eval " ( cat ./arclist | sed -n -e ${Strng} ) | sort +1 -3 +3 -4 | tr '[A-Z]' '[a-z]'"
       if [ "$extra_scan" = 'yes' ] ; then
        eval " ( cat ./../*/arclist | sed -n -e ${Strng} ) | sort -k2,3 -k4,4 | tr '[A-Z]' '[a-z]'"
       else
        eval " ( cat ./arclist | sed -n -e ${Strng} ) | sort -k2,3 -k4,4 | tr '[A-Z]' '[a-z]'"
       fi
      else
       # eval " ( $FiNd ./ -name  $Arclabel  -exec sed -n -e ${Strng} {} \; ) | sort +1 -3 +3 -4 | tr '[A-Z]' '[a-z]'"
       if [ "$extra_scan" = 'yes' ] ; then
        eval " ( $FiNd ../ -name  $Arclabel  -exec sed -n -e ${Strng} {} \; ) | sort -k2,3 -k4,4 | tr '[A-Z]' '[a-z]'"
       else
        eval " ( $FiNd ./ -name  $Arclabel  -exec sed -n -e ${Strng} {} \; ) | sort -k2,3 -k4,4 | tr '[A-Z]' '[a-z]'"
       fi
      fi
     else
      Strng="'/^""${Filex}"" /p'"
      if [ "$Arclabel" = "'arclist'" -a -s "./arclist" ] ; then
       # eval "( cat ./arclist | sed -n -e ${Strng} ) | sort +1 -3 +3 -4 | tail -1 | tr '[A-Z]' '[a-z]'"
      #eval "( cat ./arclist | sed -n -e ${Strng} ) | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'"
       if [ "$extra_scan" = 'yes' ] ; then
        HldSnglx=`eval "( cat ./../*/arclist | sed -n -e ${Strng} ) | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'"`
       else
        HldSnglx=`eval "( cat ./arclist | sed -n -e ${Strng} ) | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'"`
       fi
       if [ -n "$HldSnglx" ] ; then
        if [ -z "$HldSngl" ] ; then
         HldSngl="$HldSnglx"
        else
         HldSngl=` (echo "$HldSngl" ; echo "$HldSnglx") |  sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'`
        fi
       fi
      else
       # eval "( $FiNd ./ -name $Arclabel -exec sed -n -e ${Strng} {} \; ) | sort +1 -3 +3 -4 | tail -1 | tr '[A-Z]' '[a-z]'"
      #eval "( $FiNd ./ -name $Arclabel -exec sed -n -e ${Strng} {} \; ) | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'"
       if [ "$extra_scan" = 'yes' ] ; then
        HldSnglx=`eval "( $FiNd ../ -name $Arclabel -exec sed -n -e ${Strng} {} \; ) | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'"`
       else
        HldSnglx=`eval "( $FiNd ./ -name $Arclabel -exec sed -n -e ${Strng} {} \; ) | sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'"`
       fi
       if [ -n "$HldSnglx" ] ; then
        if [ -z "$HldSngl" ] ; then
         HldSngl="$HldSnglx"
        else
         HldSngl=` (echo "$HldSngl" ; echo "$HldSnglx") |  sort -k2,3 -k4,4 | tail -1 | tr '[A-Z]' '[a-z]'`
        fi
       fi
      fi
     fi
    fi
   fi
  done
  if [ -n "$HldSngl" ] ; then
   echo "$HldSngl"
   HldSngl=''; HldSnglx=''
  fi
fi
