#!/bin/sh

#   Jun 07/06 - F. Majaess (Added "xdays" switch support)
#   Jun 22/04 - F. Majaess (Added "expand" switch support)
#   Sep 02/03 - F. Majaess (Revise for "short term" dataclass use)
#   Apr 13/99 - F. Majaess (Revise for CFS hardware switch to SGI)
#   May 05/97 - F. Majaess (Adjust default for CFS with "full")
#   Mar 05/97 - F. Majaess (Adjust for 8-characters group names)
#   Feb 19/97 - F. Majaess (Cosmetic change for displaying directories 
#                           scanned)
#   Jul 23/96 - F. Majaess (Revise "Total" printed value format)
#   Mar 20/95 - F. Majaess (Add cfs switch)
#   Mar 04/93 - E. Chan (Modify printout of totals when 'own' is not used)
#   Nov 26/92 - E. Chan (Add option to do audit on the symbolic links rather
#                        than on the files to which they point)
#   Nov 13/92 - E. Chan 

#id auditor - obtain audit of disk space usage on a Unix machine 

#   AUTHOR  - E. Chan

#hd PURPOSE - "auditor" generates a formatted listing of the files 
#hd           on the specified directory using the Unix utility "ls -lF".
#hd           In addition, the "-L" option is also used unless it is 
#hd           switched off by specifying "links" on the command line.
#hd           The "-L" option retrieves information about the file to
#hd           which a symbolic link points rather than on the link itself.
#hd  
#hd           The default directory is the directory from which "auditor" 
#hd           is invoked. 
#hd   
#hd           The "own" option returns a listing of just the user's own
#hd           files. The total amount of disk space used is listed at the
#hd           end. 
#hd  
#hd           If "own" is not specified, an audit of all the files in the
#hd           directory is returned. The output is sorted by user and 
#hd           by filename. The total disk space of each user is calculated
#hd           and the grand total for all users is generated at the end.
#hd 
#hd           Directories and non-accessible files are not included in
#hd           the totals. 
#hd
#hd           Options (preceded by a dash) to the "ls" command may be
#hd           specified on the "auditor" command line. These are passed on to
#hd           the "ls" utility. A very useful option is "-R" which lists
#hd           recursively all accesible files in all accessible subdirectories
#hd           in addition to the usual output. This option may also be accessed
#hd           by specifying the keyword "full" on the command line.

#pr PARAMETERS:
#pr   
#pr   POSITIONAL
#pr  
#pr        dir = list of files/directories - metacharacters (wildcards) 
#pr              may be used, but they must be quoted on the command line 
#pr              (defaults to the directory from which "auditor" is invoked.
#pr               Limited to a single target when "xdays" is set) 
#pr  
#pr   PRIMARY    
#pr  
#pr       user = username - used only in conjunction with the "own"
#pr              parameter below (=$USER)
#pr   
#pr   SECONDARY
#pr  
#pr        own = switch to list only user's own files 
#pr  
#pr       full = switch to do a recursive audit of all files in all 
#pr              subdirectories below the directory from which "auditor"
#pr              is invoked 
#pr   
#pr      links = switch to do the audit on the symbolic links rather than 
#pr              on the files to which they point
#pr    timesort= switch to allow sorting the list of files by time stamp
#pr              (ie. invoking "-t" switch on the "ls" command).
#pr      xdays = Cutoff number of days to base the list on. Mainly used 
#pr              to produce a list of files older than "xdays" in the 
#pr              short term dataclass on the archive server.
#pr              (If set; it overrides "timesort" setting also no more than
#pr                       a single target subdirectory will be permitted).
#pr      cfs   = switch used to obtain a list of archived data
#pr              on the archive server.
#pr              (=no/yes)
#pr    shorterm= switch used to target the short term dataclass
#pr              subdirectory on the file server.
#pr              This switch is valid only in conjunction with "cfs" switch.
#pr              (=no/yes)
#pr    expand  = switch used to expand "_arc" file entries in the produced
#pr              output by listing the contained individual files based on
#pr              the "_arc" file log under "$CCRNSRC/arc_dir/active" 
#pr              subdirectory. 
#pr              This switch is valid only in conjunction with "cfs" switch
#pr              and provided the script is running on one of CMC platforms.
#pr              (=no/yes)

#ex EXAMPLES:
#ex   
#ex     auditor own
#ex  
#ex     The above example generates an audit of the user's own files in the
#ex     directory from which "auditor" is invoked.
#ex  
#ex     auditor full
#ex  
#ex     The above example generates a recursive audit of all files residing 
#ex     in the directory from which "auditor" is invoked. The listing is 
#ex     sorted alphabetically by user and by filename.
#ex   
#ex     auditor own /dir/subdir
#ex   
#ex     The above example generates an audit of the user's own files in the
#ex     directory "/dir/subdir".
#ex  
#ex     auditor 'd* m*'   or:   auditor 'd*' 'm*' 
#ex   
#ex     The above example generates a sorted audit (by username and by filename)
#ex     of all files beginning with "d" and "m" in the directory from which  
#ex     "auditor" is invoked. 
#ex  
#ex     auditor full cfs shorterm timesort
#ex    
#ex     The above will result in generating a list, sorted by timestamp, of 
#ex     all files archived under the invoker username's associated short term
#ex     dataclass subdirectory on the archive server.
#ex  
#ex     auditor own cfs expand 
#ex    
#ex     The above will result in generating a list of all files archived under 
#ex     the invoker username's associated default dataclass subdirectory on the 
#ex     archive server with any "_arc" entries expanded to include underneath
#ex     the list of contained individual files.
#ex  
#ex     auditor full cfs shorterm expand xdays=75
#ex    
#ex     The above will result in generating a list of files which are at least 
#ex     75 days old, (ie. due to expire within 15 days), and archived under 
#ex     the invoker username's associated short term dataclass subdirectory 
#ex     on the archive server. The "_arc" entries are expanded to include 
#ex     underneath the list of contained individual files.
#ex
#ex     auditor cfs /home/cfs_ccrd/ccrn/offcl_data/a/xyz/d
#ex    
#ex     The above example will result in getting a listing of "da_xyz_*"
#ex     files archived within /home/cfs_ccrd/ccrn/offcl_data/a/xyz/d official
#ex     data subdirectory on CFS.

#  * 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
#  * Obtain specified options, if any.
for arg in $@
do
  case $arg in 
      *=*) eval "$arg"              ;;
       -*) options="$options $arg"  ;;
      own) own=yes                  ;;
      cfs) cfs=yes                  ;;
 shorterm) shorterm=yes             ;;
   expand) expand=yes               ;;
     full) options="$options -R"    ;;
 timesort) timesort=yes             ;;
    links) links=yes                ;;
        *) dir="$dir $arg" 
  esac
done

if [ -n "$dir" ] ; then
 dir=`echo $dir | sed -e 's/"//g' | sed -e "s/'//g"`
fi

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

AWK=${AWK:='awk'}

# Set "LC_TIME" to display date/time in acceptable parsing format 

LC_TIME='POSIX'
export LC_TIME

#  * Set the defaults.

HOSTID=${HOSTID:=`hostname| cut -d'.' -f1 `}
HOSTID=`echo $HOSTID | sed -e 's/hpcr4-in/ppp4/' -e 's/hpcr3-in/ppp3/' `
user=${user:=$USER}
if [ -n "$user" ] ; then
 Lgname=`echo $user | tr '[A-Z]' '[a-z]'`
else
 Lgname=`whoami`
fi
usr=`echo $Lgname |  sed 's/^....//'`
grp=`echo $Lgname |  sed 's/...$//'`
shorterm=${shorterm:='no'}
if [ "$cfs" != 'yes' -a "$shorterm" = 'yes' ] ; then
 shorterm='no'
 echo "0 auditor: Sorry, short term dataclass subdirectory is valid only when"
 echo "           the archive server is targeted!"
 echo "           shorterm switch setting will be ignored"
fi

expand=${expand:='no'}
if [ \( "$cfs" != 'yes' -o "$SITE_ID" != 'Dorval' \) -a "$expand" = 'yes' ] ; then
 expand='no'
 echo "0 auditor: Sorry, expand switch can only be used if the target is the"
 echo "           archive server and the script is running at CMC in Dorval!"
 echo "           expand switch setting will be ignored"
fi
ARCDIR=${ARCDIR:-"$CCRNSRC/arc_dir"}
if [ "$cfs" = 'yes' ] ; then
 ARCDIRchk=`ssh cfs2 ls -1d /home/cfs_ccrd/ccrn`
 #if [ `ssh cfs2 hostname | cut -c 1-4` != 'cfs2' -o `ssh cfs2 ls -1d /home/cfs_ccrd/ccrn` != '/home/cfs_ccrd/ccrn' ] ; then
  if [ `ssh cfs2 hostname | cut -c 1-4` != 'cfs2' -o "$ARCDIRchk" != '/home/cfs_ccrd/ccrn' ] ; then
   echo "0 auditor: Sorry, cfs2 is not fully functional now. Please retry later on."
   exit 1
  fi
fi

if [ "$cfs" = 'yes' -a -z "$dir" ] ; then
 unset savdir
 # 003) unset savdir ;;
 ### if [ "$Lgname" = 'acrpmdm' -o "$Lgname" = 'acrmril' ] ; then
 ### if [ "$Lgname" = 'acrpxxx' -o "$Lgname" = 'acrmxxx' ] ; then
 if [ "$Lgname" = 'acrp001' -o "$Lgname" = 'acrplbw' -o "$Lgname" = 'acrprfb' -o "$Lgname" = 'acrprgm' -o "$Lgname" = 'acrpssu' -o "$Lgname" = 'acrmrmc' -o "$Lgname" = 'acrmrac' ] ; then
  if [ "$shorterm" = 'yes' ] ; then
   savdir="/home/cfs_ccrd/ccrd_short_term_archive/$grp/$usr"
  else
   savdir="/home/cfs_ccrd/cnet/$grp/$usr"
  fi
 elif [ "$Lgname" = 'armnrls' ] ; then
   savdir="/home/cfs_dorard/court_terme/armnrls"
 else
 if [ "$grp" = 'acrn' ] ; then
  if [ "$shorterm" = 'yes' ] ; then
    CcRdUsRa='ccrd_short_term_archive'
    CnEtUsRa='ccrd_short_term_archive'
    unset CcRnUsRa
  else
    CcRdUsRa='ccrd_user_archive'
    CnEtUsRa='cnet'
    CcRnUsRa="/home/cfs_ccrd/ccrn/$usr"
  fi
 case $usr in
                           src ) savdir="/home/cfs_ccrd/ccrn/$usr"  ;;
 #rmh) savdir="/home/cfs_ccrd/ccrd_user_archive/$usr /home/cfs_ccrd/ccrd_long_term_archive/$usr /home/cfs_ccrd/ccrn/$usr" ;;
  nys|rbd|rfp|rmh|yam|yjk) savdir="/home/cfs_ccrd/ccrd_long_term_archive/$usr" ;;
  002|005|006|007|003|nbp|mcr|mky|all|cbn|ocn|opt|sip|cc1|cc2|cc3|cc4|esm|gcm|ice|rdr|rdy|rfm|rgb) savdir="/home/cfs_ccrd/$CcRdUsRa/$usr $CcRnUsRa" ;;
  mr1|mr2|mr3|mr4|mr5|mr6|mr7|mr8|mr9|pd1|pd2|pd3|pd4|pd5|pd6|pd7|pd8|pd9) savdir="/home/cfs_ccrd/$CcRdUsRa/$usr $CcRnUsRa" ;;
  rjk|ryj|rmq|wsl|rjl|rjs|smp|rby|rdp|rcw|rcs|rrh|rsk|rks|rrm|rmn|rtm|008|rjc|rls|rms|rns|rrr|raa|rfc|rrs|rsa|ror|rjm|rpg|red) savdir="/home/cfs_ccrd/$CcRdUsRa/$usr $CcRnUsRa" ;;
  rkz|rwm|rmm|ram|rng|rkd|rkm|rgf|ref|rjf|rmg|rcc|rva|rbb|rbt|rkl|ncs|ras|jcl|rlr|rbw|jos|rja|sah|rmb|rml|rnm|rsl|ros|rwl|004) savdir="/home/cfs_ccrd/$CcRdUsRa/$usr $CcRnUsRa" ;;
  01[0-9]|020) savdir="/home/cfs_ccrd/$CcRdUsRa/$usr $CcRnUsRa" ;;
  rrd|rhb|rec|rdv|jpb|vrm ) savdir="/home/cfs_ccrd/$CnEtUsRa/$usr"  ;;
  001|009|c02|y01|y03|y04|y06|y07|y09|yww|ydc|ysb|yvf|ycm ) savdir="/home/cfs_ccrd/$CnEtUsRa/$usr"  ;;
  # q0[1-7]|qrj|qaf|qdc|qdp|qhc|qmg|qsb ) savdir="/home/cfs_ccrd/$CnEtUsRa/$usr"  ;;
  qmg ) savdir="/home/cfs_ccrd/$CnEtUsRa/$usr"  ;;
                             * ) unset savdir ;;
 esac
 fi
 fi
 own=${own:='no'}
 if [ "$own" != 'yes' -a "$user" = 'acrnsrc' ] ; then
 # stdcfsdir=${stdcfsdir:="/home/cfs_ccrd/ccrn/offcl_data $savdir "}
   stdcfsdir=${stdcfsdir:="/home/cfs_ccrd/ccrn /home/cfs_ccrd/ccrd_user_archive /home/cfs_ccrd/cnet /home/cfs_ccrd/ccrd_short_term_archive"}
 else
   savdir=${savdir:='/home/cfs_ccrd/ccrn/src'}
   stdcfsdir=${stdcfsdir:="$savdir "}
 fi
fi
if [ -n "$dir" ] ; then
 cfsdir=${cfs:+$dir}
else
 cfsdir=${cfs:+$stdcfsdir}
fi
if [ -n "$xdays" ] ; then
#set -x
 if [ -n "$timesort" ] ; then
  unset timesort
 fi
 # prfx=${cfs:+'rsh cfs "find '}
 # prfx=${cfs:+'rsh cfs " '}
 prfx=${cfs:+'ssh cfs2 " '}
 # prfx=${prfx:='find '}
 prfx=${prfx:=' '}
 # sffx=${cfs:+" -mtime +$xdays -exec fsls "}
 sffx=${cfs:+" -mtime +$xdays -exec ls "}
 sffx=${sffx:=" -mtime +$xdays -exec \ls "}
 sffx2=${cfs:+' {} \;" 2>/dev/null'}
 sffx2=${sffx2:=' {} \;'}
 
else
 # prfx=${cfs:+'rsh cfs fsls '}
 # prfx=${cfs:+'ssh cfs fsls '}
 prfx=${cfs:+'ssh cfs2 ls '}
 prfx=${prfx:='\ls '}
 sffx=${cfs:+' 2>/dev/null'}
 sffx=${sffx:=' '}
fi
# opext=${cfs:+' -g '}
# options="$options $opext"
cfs=${cfs='no'}
dir=${cfsdir:-$dir}
dirpath=${dir:-`pwd`}
if [ "$links" != 'yes' ] ; then
  options="$options -L"
fi
if [ "$timesort" = 'yes' ] ; then
  options="$options -t"
fi
if [ "$SITE_ID" != 'Dorval' -a "$OS" = 'Linux' ] ; then
 # Ensure month/day format instead of yyyy-mm-dd ...
 options="$options --time-style=locale"
fi

#  * Generate heading for listing.
unset Xdir LXdir ldir
echo
for tldir in $dirpath
 do
  if [ -z "$ldir" ] ; then
   ldir=$tldir
   echo "            AUDIT FOR: $ldir"
   Xdir="$tldir/."
   LXdir="$tldir"
  else
   echo "                       $tldir"
   Xdir="$Xdir $tldir/."
   LXdir="$LXdir $tldir"
  fi
 done

if [ -n "$xdays" ] ; then
    echo
#   echo "            ***> ONLY FILES AT LEAST $xdays DAYS OLD ARE LISTED. <***"
    echo "            ***> ONLY FILES LAST MODIFIED AT LEAST $xdays DAYS AGO ARE LISTED. <***"
    echo
fi
echo
# if [ "$HOSTID" = 'cidsv08' ] ; then
#   echo "            DATE/TIME:  `/usr/bsd43/bin/date`" 
# else
    echo "            DATE/TIME:  `date`" 
# fi

echo 
echo 

#
#  * Generate the listing.
#
# Tprfx=`echo $prfx | sed -e 's/^rsh .*$/rsh/'`
Tprfx=`echo $prfx | sed -e 's/^rsh .*$/rsh/' -e 's/^ssh .*$/ssh/'`
if [ -n "$Tprfx" -a \( "$Tprfx"  = 'rsh' -o "$Tprfx"  = 'ssh' \) ] ; then
  xdir="${dir}"
else
  for xdir in ${Xdir}
  do
   if [ ! -d "${xdir}/." ] ; then
    echo "Auditor: Sorry, invalid local --> ${xdir} <-- subdirectory!"
    exit 1
   fi
  done
  xdir="${Xdir}"
fi
# echo " xdir=$xdir"

#  * Ensure no more than a single subdirectory is targeted when
#  * "xdays" is set ...

if [ -n "$xdays" ] ; then
 # set -x
 xdir=`echo ${xdir} | sed -e 's/^ *//' | sed -e 's/ *$//'`
 if [ ! -n "${xdir}" ] ; then
  echo "Auditor: A valid target subdirectory must be specified!"
  exit 1
 fi
 Txdir=`echo ${xdir} | sed -e 's/ *//g'`
 if [ "${xdir}" != "${Txdir}" ] ; then
   echo "Auditor: Sorry, with xdays (=$xdays) the scan must be limited to a single subdirectory!"
   exit 2
 fi
fi


if [ "$own" = 'yes' ] ; then

  #  * Pipe output from "ls" to "awk" to format and calculate the 
  #  * total disk space usage.
 
# $prfx \ls -Fl $options $dir | $AWK '
# eval "$prfx -Fl $options $dir" | $AWK '
# eval "$prfx -Fl $options ${xdir}" $sffx | $AWK '
  if [ -n "$xdays" ] ; then
   # line="$prfx cd ${xdir} ; find ./ $sffx -Fl $options"
   line="$prfx cd ${xdir} ; find -L ./ -name '[a-zA-Z0-9]*' $sffx -Fl $options"
   sffx="$sffx2"
  else
   line="$prfx -Fl $options ${xdir}" 
  fi
  eval "$line " $sffx | $AWK '
  #
  #  * Print out column headings.
  #
  BEGIN {
    printf ( "%-11s%10s%10s%12s%11s%7s\n", \
             " PERMISSIONS","USER","GROUP","BYTES","MODIFIED","NAME" )
    print
  }
  #
  #  * Extract and save the name if a subdirectory is listed (only with
  #  * the "-R" option). 
  #
  $1 ~ /^[^.][^ ]*:/ {
    len = length($1) - 1
    dir = substr($1,1,len)
  }
  $1 ~ /^\..*:/ {
    len = length($1) - 3 
    dir = substr($1,3,len)
  }
  #
  #  * As above except that provision must be made for the case when the
  #  * full pathname of a directory is passed to the "ls" command. In this
  #  * case, all names of subdirectories also contain the full pathname and
  #  * should be shortened. 
  #
  $1 ~ /^\/.*:/ {
    pos = 2
    dir = $1
    while ( pos > 1 ) {
      dir = substr(dir,pos)
      pos = index(dir,"/") + 1
    }
    len = length(dir) - 1 
    dir = substr(dir,1,len)
  }
  #
  #  * Add file size in bytes to the cumulative total for the user and
  #  * print out the line. If the file exists in a subdirectory and the "-R"
  #  * option is specified, place the name of the subdirectory in front of
  #  * the filename. 
  #
  NF >= 9 && $3 == user {
    if ( $1 ~ /^[Mm-]/ ) {
      sum += $5
      ++num
    }
    if ( dir ~ /^$/ ) {
      printf ( "%11s%4s%8s%9s%12s%4s%3s%6s%s%s%s%s%s%s\n", \
   	        $1,$2,$3,$4,$5,$6,$7,$8," ",$9," ",$10," ",$11 )
    }
    else {
      printf ( "%11s%4s%8s%9s%12s%4s%3s%6s%s%s%s%s%s%s%s%s\n", \
   	        $1,$2,$3,$4,$5,$6,$7,$8," ",dir,"/",$9," ",$10," ",$11 )
    }
  }
  #
  #  * Print out the totals.
  #
  END { 
    printf ("%s\n"," ")
    print " Total: ", sum, "bytes (" num " files)"
    printf ("%s\n"," ")
  }'         user=$user | $AWK -v expand="$expand" -v LXdir="$LXdir" -v LXdirp="$ARCDIR/active/" '
   BEGIN { gsub(/\/home\/cfs_ccrd\//,LXdirp,LXdir) } 
   {
    { print $0 }
    if ( expand == "yes" ) {
     origline = $0
     if ( $0 ~ /^.*_arc *$/ ) {
      arcfile = $NF
      sub (/.*\//,"",arcfile)
      arc_lst_cmd = "arc_lst indvdlst=yes nostrip=yes " arcfile " " LXdir " 2>>/dev/null"
      stat = arc_lst_cmd | getline List
      while ( stat == 1 ) {
       printf "%58s%-40s\n", " ^^^^>>>> ", List
       stat = arc_lst_cmd | getline List
      }

      close ( arc_lst_cmd )

     }
    }
   }' 2>>/dev/null

elif [ "$timesort" = 'yes' ] ; then

  #  * Pipe output from "ls" to "awk" to add pathnames to filenames that
  #  * are generated when the "-R" option is used. 

# $prfx \ls -Fl $options $dir | $AWK '
# eval "$prfx -Fl $options $dir" | $AWK '
  eval "$prfx -Fl $options ${xdir}" $sffx | $AWK '
  #
  #  * Extract and save the name if a subdirectory is listed (only with
  #  * the "-R" option). 
  #
  $1 ~ /^[^.][^ ]*:/ {
    len = length($1) - 1
    dir = substr($1,1,len)
  }
  $1 ~ /^\..*:/ {
    len = length($1) - 3 
    dir = substr($1,3,len)
  }
  #
  #  * As above except that provision must be made for the case when the
  #  * full pathname of a directory is passed to the "ls" command. In this
  #  * case, all names of subdirectories also contain the full pathname and
  #  * should be shortened. 
  #
  $1 ~ /^\/.*:/ {
    pos = 2
    dir = $1
    while ( pos > 1 ) {
      dir = substr(dir,pos)
      pos = index(dir,"/") + 1
    }
    len = length(dir) - 1 
    dir = substr(dir,1,len)
  }
  #
  #  * Print out the line with the name of the subdirectories added to
  #  * filenames if the "-R" option is used.
  #
  NF >= 9 {
    if ( dir ~ /^$/ ) {
      printf ( "%11s%4s%8s%9s%12s%4s%3s%6s%s%s%s%s%s%s\n", \
   	        $1,$2,$3,$4,$5,$6,$7,$8," ",$9," ",$10," ",$11 )
    }
    else {
      printf ( "%11s%4s%8s%9s%12s%4s%3s%6s%s%s%s%s%s%s%s%s\n", \
   	        $1,$2,$3,$4,$5,$6,$7,$8," ",dir,"/",$9," ",$10," ",$11 )
    }
  }'   |   

  #  * Pipe output from "awk" to "sort" to sort the listing by user and 
  #  * then by filename. Finally pipe the output from "sort" to "awk"
  #  * again to format the listing and calculate the file usage statistics.

  $AWK '
  #
  #  * Define variables containing username.
  #
  { olduser = user
    user = $3
  }
  NF >= 9 {
    #
    #  * Print out totals at the end of each section and add to grand
    #  * total for the entire directory.
    #
    if ( $3 != olduser && flag == "true" ) {
      printf ("%s\n"," ")
      # print " Total: ", sum, "bytes (" num " files)"
      printf ( "%8s%15s%30s\n", \
               " ", "Total Files", "Total Disk Space (bytes)" ) 
      # printf ( "%8s%15d%30d%26s\n", \
      # printf ( "%8s%15d%30.0f%26s\n", \
      printf ( "%8s%15.0f%30.0f%26s\n", \
                olduser, num, sum, "*" ) 
      printf ("%s\n"," ")
      user = $3
      bigsum += sum
      bignum += num
      sum = 0
      num = 0
      flag = "false"
      dir = ""
    }
    #
    #  * Sum up the sizes of the individual files.
    #
    if ( $1 ~ /^[Mm-]/ ) {
      sum += $5
      ++num
    }
    #
    #  * Print out column headings at the beginning of the listing of 
    #  * each section.
    #
    if ( flag != "true" ) {
      print " ------------------------------------------------------------------------------"
      print " "
      print " USER: ", user
      print " "
      printf ( "%-11s%10s%10s%12s%11s%7s\n", \
               " PERMISSIONS","USER","GROUP","BYTES","MODIFIED","NAME" )
      print " "
    }
    flag = "true"
    print 
  }
  #
  #  * Print out grand total disk space used for the directory.
  #
  END { 
    bigsum += sum
    bignum += num
    printf ("%s\n"," ")
    #print " Total: ", sum, "bytes (" num " files)"
    printf ( "%8s%15s%30s\n", \
             " ", "Total Files", "Total Disk Space (bytes)" ) 
    # printf ( "%8s%15d%30d%26s\n", \
    # printf ( "%8s%15d%30.0f%26s\n", \
    printf ( "%8s%15.0f%30.0f%26s\n", \
              olduser, num, sum, "*" ) 
    printf ("%s\n"," ")
    print " ------------------------------------------------------------------------------"
    printf ("%s\n"," ")
    print "\t", "       Grand Total: ", bigsum, "bytes (" bignum " files)"
  }' | $AWK -v expand="$expand" -v LXdir="$LXdir" -v LXdirp="$ARCDIR/active/" '
   BEGIN { gsub(/\/home\/cfs_ccrd\//,LXdirp,LXdir) } 
   {
    { print $0 }
    if ( expand == "yes" ) {
     origline = $0
     if ( $0 ~ /^.*_arc *$/ ) {
      arcfile = $NF
      sub (/.*\//,"",arcfile)
      arc_lst_cmd = "arc_lst indvdlst=yes nostrip=yes " arcfile " " LXdir " 2>>/dev/null"
      stat = arc_lst_cmd | getline List
      while ( stat == 1 ) {
       printf "%58s%-40s\n", " ^^^^>>>> ", List
       stat = arc_lst_cmd | getline List
      }

      close ( arc_lst_cmd )

     }
    }
   }' 2>>/dev/null

else

  #  * Pipe output from "ls" to "awk" to add pathnames to filenames that
  #  * are generated when the "-R" option is used. 

# $prfx \ls -Fl $options $dir | $AWK '
# eval "$prfx -Fl $options $dir" | $AWK '
# eval "$prfx -Fl $options ${xdir}" $sffx | $AWK '
  if [ -n "$xdays" ] ; then
   # line="$prfx cd ${xdir} ; find ./ $sffx -Fl $options"
   line="$prfx cd ${xdir} ; find -L ./ -name '[a-zA-Z0-9]*' $sffx -Fl $options"
   sffx="$sffx2"
  else
   line="$prfx -Fl $options ${xdir}" 
  fi
  # set -x
  eval "$line " $sffx | $AWK '
  #
  #  * Extract and save the name if a subdirectory is listed (only with
  #  * the "-R" option). 
  #
  $1 ~ /^[^.][^ ]*:/ {
    len = length($1) - 1
    dir = substr($1,1,len)
  }
  $1 ~ /^\..*:/ {
    len = length($1) - 3 
    dir = substr($1,3,len)
  }
  #
  #  * As above except that provision must be made for the case when the
  #  * full pathname of a directory is passed to the "ls" command. In this
  #  * case, all names of subdirectories also contain the full pathname and
  #  * should be shortened. 
  #
  $1 ~ /^\/.*:/ {
    pos = 2
    dir = $1
    while ( pos > 1 ) {
      dir = substr(dir,pos)
      pos = index(dir,"/") + 1
    }
    len = length(dir) - 1 
    dir = substr(dir,1,len)
  }
  #
  #  * Print out the line with the name of the subdirectories added to
  #  * filenames if the "-R" option is used.
  #
  NF >= 9 {
    if ( dir ~ /^$/ ) {
      printf ( "%11s%4s%8s%9s%12s%4s%3s%6s%s%s%s%s%s%s\n", \
   	        $1,$2,$3,$4,$5,$6,$7,$8," ",$9," ",$10," ",$11 )
    }
    else {
      printf ( "%11s%4s%8s%9s%12s%4s%3s%6s%s%s%s%s%s%s%s%s\n", \
   	        $1,$2,$3,$4,$5,$6,$7,$8," ",dir,"/",$9," ",$10," ",$11 )
    }
  }'   |   

  #  * Pipe output from "awk" to "sort" to sort the listing by user and 
  #  * then by filename. Finally pipe the output from "sort" to "awk"
  #  * again to format the listing and calculate the file usage statistics.

  # sort +2 -3 +8 -9 | $AWK '
  sort -k3,3 -k9,9 | $AWK '
  #
  #  * Define variables containing username.
  #
  { olduser = user
    user = $3
  }
  NF >= 9 {
    #
    #  * Print out totals at the end of each section and add to grand
    #  * total for the entire directory.
    #
    if ( $3 != olduser && flag == "true" ) {
      printf ("%s\n"," ")
      # print " Total: ", sum, "bytes (" num " files)"
      printf ( "%8s%15s%30s\n", \
               " ", "Total Files", "Total Disk Space (bytes)" ) 
      # printf ( "%8s%15d%30d%26s\n", \
      # printf ( "%8s%15d%30.0f%26s\n", \
      printf ( "%8s%15.0f%30.0f%26s\n", \
                olduser, num, sum, "*" ) 
      printf ("%s\n"," ")
      user = $3
      olduser = user
      bigsum += sum
      bignum += num
      sum = 0
      num = 0
      flag = "false"
      dir = ""
    }
    #
    #  * Sum up the sizes of the individual files.
    #
    if ( $1 ~ /^[Mm-]/ ) {
      sum += $5
      ++num
    }
    #
    #  * Print out column headings at the beginning of the listing of 
    #  * each section.
    #
    if ( flag != "true" ) {
      print " ------------------------------------------------------------------------------"
      print " "
      print " USER: ", user
      print " "
      printf ( "%-11s%10s%10s%12s%11s%7s\n", \
               " PERMISSIONS","USER","GROUP","BYTES","MODIFIED","NAME" )
      print " "
    }
    flag = "true"
    print 
  }
  #
  #  * Print out grand total disk space used for the directory.
  #
  END { 
    bigsum += sum
    bignum += num
    printf ("%s\n"," ")
    #print " Total: ", sum, "bytes (" num " files)"
    printf ( "%8s%15s%30s\n", \
             " ", "Total Files", "Total Disk Space (bytes)" ) 
    # printf ( "%8s%15d%30d%26s\n", \
    # printf ( "%8s%15d%30.0f%26s\n", \
    printf ( "%8s%15.0f%30.0f%26s\n", \
              olduser, num, sum, "*" ) 
    printf ("%s\n"," ")
    print " ------------------------------------------------------------------------------"
    printf ("%s\n"," ")
    print "\t", "       Grand Total: ", bigsum, "bytes (" bignum " files)"
  }' | $AWK -v expand="$expand" -v LXdir="$LXdir" -v LXdirp="$ARCDIR/active/" '
   BEGIN { gsub(/\/home\/cfs_ccrd\//,LXdirp,LXdir) } 
   {
    { print $0 }
    if ( expand == "yes" ) {
     origline = $0
     if ( $0 ~ /^.*_arc *$/ ) {
      arcfile = $NF
      sub (/.*\//,"",arcfile)
      arc_lst_cmd = "arc_lst indvdlst=yes nostrip=yes " arcfile " " LXdir " 2>>/dev/null"
      stat = arc_lst_cmd | getline List
      while ( stat == 1 ) {
       printf "%58s%-40s\n", " ^^^^>>>> ", List
       stat = arc_lst_cmd | getline List
      }

      close ( arc_lst_cmd )

     }
    }
   }' 2>>/dev/null

fi

if [ "$shorterm" = 'yes' ] ; then
 echo "\n ==============================================================================\n"
 echo " Reminder: A 90 days expiry date is currently imposed on datasets residing "
 echo "           under the short term dataclass on CFS."
 echo "\n =============================================================================="
fi
 
exit
