#!/bin/sh

#   Aug 01/95 - F. Majaess 

#id cfsfind  - find the location of file(s) on the archive server.
 
#   AUTHOR  - F. Majaess

#hd PURPOSE - "cfsfind" script is used to find the location of file(s)
#hd           on the archive server CFS.
#hd
#hd           Note: pollux:~acrnsrc/info/adtcfs file contains a listing
#hd                 of the official files saved in the official subdirectory
#hd                 ${ArcRootPath}/ccrn/offcl_data. Where ${ArcRootPath} is
#hd                 set to "/home/cfs_ccrd" on CFS and "/archive/eccc/crd" 
#hd                 on HPNLS. You may check the above file prior to invoking 
#hd                 the script.
#hd                 The path on CFS/HPNLS for the various dataclasses is as
#hd                 follows:
#hd          
#hd                    for CCCMA official          dataclass:
#hd                                                ${ArcRootPath}/ccrn
#hd
#hd                    for CCCMA users             dataclass:
#hd                                                ${ArcRootPath}/ccrd_user_archive
#hd
#hd                    for network                 dataclass:
#hd                                                ${ArcRootPath}/cnet.
#hd
#hd                    for all accounts short term dataclass:
#hd                                                ${ArcRootPath}/ccrd_sort_term_archive
#hd
#hd                 By default, only associated users's subdirectories on CFS with the
#hd                 the last 3 dataclasses are scanned.           
#hd

#pr PARAMETERS:
#pr 
#pr   POSITIONAL
#pr 
#pr        fil  = name of file(s) to search for.
#pr               (the use of '*' is problematic, please use "all" switch instead)
#pr     cfspath = CFS path from which to start searching
#pr               (=${ArcRootPath}/ccrd_user_archive/"user_dir" 
#pr                 ${ArcRootPath}/cnet/"user_dir"
#pr                 ${ArcRootPath}/ccrd_short_term_archive/"user_dir"
#pr                 ${ArcRootPath}/ccrd_long_term_archive/"user_dir" ; by default
#pr                =${ArcRootPath}/ccrn/offcl_data                 ; if "offcl=yes")
#pr 
#pr   PRIMARY
#pr 
#pr     cfsuser = username - applicable only when not targeting the official data 
#pr                          subdirectory (ie. No valid with "offcl=yes")
#pr                          (=$USER)
#pr 
#pr   SECONDARY
#pr 
#pr           offcl= switch used to target the master data directory for the search.
#pr                  (=no/yes)
#pr 
#pr           long= switch to generate a list in long format (ie. using 'ls -ld')
#pr                 (=no/yes)
#pr 
#pr            all= switch to list all entries found (workaround for using '*' for "fil")
#pr                 (Not valid with "offcl=yes")
#pr                 (=no/yes)
#pr 

#ex EXAMPLE:
#ex 
#ex        cfsfind '*GISS*.nc' cfspath=/home/cfs_ccrd/CCCma_Output
#ex 
#ex     The above will results in searching for '*GISS*.nc' files
#ex     under /home/cfs_ccrd/CCCma_Output subdirectory on CFS.
#ex 
#ex        cfsfind 'plgks*' cfspath=/home/cfs_ccrd/ccrd_user_archive 
#ex 
#ex     The above would lead to target "/home/cfs_ccrd/ccrd_user_archive" 
#ex     path in searching for files 'plgks*' and report the location.
#ex 
#ex        cfsfind cfsuser=acrnxxx '*ma_xyz_*'
#ex 
#ex     The above results in searching the associated subdirectories on CFS
#ex     with "acrnxxx" account and list all matching '*ma_xyz_*' entries in
#ex     them.
#ex 
#ex        cfsfind all cfsuser=acrnxxx 
#ex 
#ex     The above will list all found entries in the associated CFS 
#ex     subdirectories with "cfsuser" account.
#ex 
#ex        cfsfind offcl ncep_narr_19811023_19811027_2008185175914_arc
#ex 
#ex     The above will result in searching for:
#ex         'ncep_narr_19811023_19811027_2008185175914_arc'
#ex     under "/home/cfs_ccrd/ccrn/offcl_data" official data subdirectory
#ex     on the archive server.
#ex 
#ex        cfsfind offcl long 'mc_dal_*'
#ex 
#ex     The above will result in searching 
#ex       "cfs:/home/cfs_ccrd/ccrn/offcl_data/c/dal/m" -- EC network
#ex     or
#ex       "hpnls:/archive/eccc/crd/ccrn/offcl_data/c/dal/m" -- Science network
#ex     subdirectory and produce a list in long format for entries starting 
#ex     with "mc_dal_".


#  * 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 archived.

# set -x
cd
arg_list=$@
Star_list=`echo *`
if [ "$Star_list" != '*' ] ; then
 arg_list=`echo $arg_list | sed -e "s/$Star_list/all/g"`
fi
for arg in $arg_list
do 
  case $arg in
      -*) set $arg                    ;;
     *=*) eval "$arg"                 ;;
      all) all=yes                  ;;
   offcl) offcl='yes'                 ;;
    long) long='yes'                 ;;
       *) if [ "$arg" = '*' ] ; then
           file_list="$file_list "'*'
          else
           file_list="$file_list $arg"
          fi ;;
  esac
done
offcl=${offcl:-'no'}
long=${long:-'no'}
own=${own:-'yes'}
if [ "$offcl" = 'yes' ] ; then
  own='no'}
fi
all=${all:-'no'}
if [ "$all" = 'yes' ] ; then
 if [ "$offcl" = 'yes' ] ; then
  echo "0 cfsfind: Sorry, use of 'all' or '*' is not valid on the official data subdirectory"
  exit 1
 else
  if [ "$own" != 'yes' ] ; then
   echo "0 cfsfind: Sorry, use of 'all' or '*' is restricted to 'own' subdirectories on CFS"
   exit 1
  else 
     file_list='*'
  fi
 fi
fi
# cfsuser=${cfsuser:-"$USER"}
if [ -n "$cfsuser" ] ; then
 Lgname=`echo $cfsuser | tr '[A-Z]' '[a-z]'`
 LgnameEC="$Lgname"
else
 Lgname=`whoami`
 if [ "$SITE_ID" = 'DrvlSC' ] ; then
   LgnameEC=`accmap $Lgname`
 else
   LgnameEC="$Lgname"
 fi
fi
usr=`echo $LgnameEC |  sed 's/^....//'`

if [ -z "$file_list" ] ; then
 echo "0 cfsfind: no file list is specified"
 exit 1
fi
if [ "$SITE_ID" = 'DrvlSC' ] ; then
 ArcRootPath='/archive/eccc/crd'
 ArcSrvr='hpnls'
 ArcSrvr_prfx='hpnl'
else
 ArcRootPath='/home/cfs_ccrd'
 ArcSrvr='cfs2'
 ArcSrvr_prfx='cfs2'
fi
# exit
if [ "$offcl" = 'yes' ] ; then
# cfspath=${cfspath:="${ArcRootPath}/ccrn/offcl_data"}
  if [ -z "$cfspath" ] ; then
   file_list=`echo $file_list | tr '[A-Z]' '[a-z]' | sed -n -e 's/^ *//' -e 's/ *$//' -e '1,$p'`
   TmpFil=`echo $file_list | sed -e 's/ //g'`
   cfspath="${ArcRootPath}/ccrn/offcl_data/others"
   if [ "$TmpFil" = "$file_list" ] ; then
   #tocrack=`echo $TmpFil | sed -n -e '/^.._.*_.*$/s/_/ /gp' | $AWK '{ print $1 " " $2 ; }'`
    tocrack=`echo $TmpFil | 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) > 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
       cfspath="${ArcRootPath}/ccrn/offcl_data/$class/$runame/$type"
     fi
    fi
   fi
  fi
else
  # cfspath=${cfspath:="${ArcRootPath}/ccrd_user_archive ${ArcRootPath}/cnet ${ArcRootPath}/ccrd_short_term_archive ${ArcRootPath}/ccrd_long_term_archive"}
  if [ -z "$cfspath" ] ; then
   if [ "$own" = 'yes' ] ; then
   # cfspath=`ssh ${ArcSrvr} 'echo /home/cfs_ccrd/*/'"$usr" | sed -e 's/\/home\/cfs_ccrd\/ccrn\/...//g'`
     cfspath=`ssh ${ArcSrvr} 'echo '"${ArcRootPath}"'/*/'"$usr" | sed -e 's/\/home\/cfs_ccrd\/ccrn\/...//g' -e 's/\/archive\/eccc\/crd\/ccrn\/...//g'`
   else
    cfspath="${ArcRootPath}/ccrd_user_archive ${ArcRootPath}/cnet ${ArcRootPath}/ccrd_short_term_archive ${ArcRootPath}/ccrd_long_term_archive"
   fi
  fi
fi
# echo "cfspath=$cfspath"
if [ "$SITE_ID" = 'DrvlSC' ] ; then
 unset hstnm
 hstnm=`ssh ${ArcSrvr} hostname 2>/dev/null | cut -c 1-4 `
 hststrgtdirck=`ssh ${ArcSrvr} hsmls -1d ${ArcRootPath}/ccrn`
 while [ "$hstnm" != "${ArcSrvr_prfx}" -o "$hststrgtdirck" != "${ArcRootPath}/ccrn" ]
  do
   Tmstmp=`date`
   echo "\n0 cfsfind: archive server is not accessible ~ $Tmstmp ! \n"
   sleep 300
   unset hstnm 
   hstnm=`ssh ${ArcSrvr} hostname 2>/dev/null | cut -c 1-4 `
   hststrgtdirck=`ssh ${ArcSrvr} hsmls -1d ${ArcRootPath}/ccrn`
  done
 #
 for DirPath in $cfspath
  do
   if [ "$all" = 'yes' ] ; then
    echo "" ; echo "=======> Listing all in $DirPath:" ; echo ""
    if [ "$long" = 'yes' ] ; then
    #if [ "$offcl" = 'yes' ] ; then
      ssh ${ArcSrvr} "cd ${DirPath}/.; hsmls -als"
    #else
    # ssh ${ArcSrvr} find ${DirPath}/. '-exec hsmls -ld {} \;'
    #fi
    else
    #if [ "$offcl" = 'yes' ] ; then
      ssh ${ArcSrvr} "cd ${DirPath}/.; hsmls -1"
    #else
    # ssh ${ArcSrvr} find ${DirPath}/. -print
    #fi
    fi
   else
    for fname in $file_list
    do
     echo "" ; echo "======> Searching in -- $DirPath -- for ... $fname:" ; echo ""
     if [ "$long" = 'yes' ] ; then
     #if [ "$offcl" = 'yes' ] ; then
       ssh ${ArcSrvr} "cd ${DirPath}/.; hsmls -lds $fname"
     #else
     # ssh ${ArcSrvr} find ${DirPath}/. -name $fname '-exec hsmls -ld {} \;'
     #fi
     else
     #if [ "$offcl" = 'yes' ] ; then
       ssh ${ArcSrvr} "cd ${DirPath}/. ; hsmls -1 $fname"
     #else
     # ssh ${ArcSrvr} find ${DirPath}/. -name $fname -print
     #fi
     fi
    done
   fi
  done
else
 unset hstnm
 hstnm=`ssh ${ArcSrvr} hostname 2>/dev/null | cut -c 1-4 `
 # hststate=`rsh ${ArcSrvr} fsstate -f | grep 'The FileServ system is active.'`
 # while [ "$hstnm" != "cfs" -o "$hststate" != 'The FileServ system is active.' ]
 hststate=`ssh ${ArcSrvr} fsstate -f | grep 'The Tertiary Manager system is active.'`
 hststrgtdirck=`ssh ${ArcSrvr} ls -1d ${ArcRootPath}/ccrn`
 while [ "$hstnm" != "${ArcSrvr_prfx}" -o "$hststate" != 'The Tertiary Manager system is active.' -o "$hststrgtdirck" != "${ArcRootPath}/ccrn" ]
  do
   Tmstmp=`date`
   echo "\n0 cfsfind: archive server is not accessible ~ $Tmstmp ! \n"
   sleep 300
   unset hstnm hststate
   hstnm=`ssh ${ArcSrvr} hostname 2>/dev/null | cut -c 1-4 `
   hststate=`ssh ${ArcSrvr} fsstate -f | grep 'The Tertiary Manager system is active.'`
   hststrgtdirck=`ssh ${ArcSrvr} ls -1d ${ArcRootPath}/ccrn`
  done
 #
 for DirPath in $cfspath
  do
   if [ "$all" = 'yes' ] ; then
    echo "" ; echo "=======> Listing all in $DirPath:" ; echo ""
    if [ "$long" = 'yes' ] ; then
    #if [ "$offcl" = 'yes' ] ; then
      ssh ${ArcSrvr} "cd ${DirPath}/.; ls -als"
    #else
    # ssh ${ArcSrvr} find ${DirPath}/. '-exec ls -ld {} \;'
    #fi
    else
    #if [ "$offcl" = 'yes' ] ; then
      ssh ${ArcSrvr} "cd ${DirPath}/.; ls -1"
    #else
    # ssh ${ArcSrvr} find ${DirPath}/. -print
    #fi
    fi
   else
    for fname in $file_list
    do
     echo "" ; echo "======> Searching in -- $DirPath -- for ... $fname:" ; echo ""
     if [ "$long" = 'yes' ] ; then
     #if [ "$offcl" = 'yes' ] ; then
       ssh ${ArcSrvr} "cd ${DirPath}/.; ls -lds $fname"
     #else
     # ssh ${ArcSrvr} find ${DirPath}/. -name $fname '-exec ls -ld {} \;'
     #fi
     else
     #if [ "$offcl" = 'yes' ] ; then
       ssh ${ArcSrvr} "cd ${DirPath}/. ; ls -1 $fname"
     #else
     # ssh ${ArcSrvr} find ${DirPath}/. -name $fname -print
     #fi
     fi
    done
   fi
  done
fi
exit
