#! /bin/csh -f
#
#/*****************************************************************************
#                Copyright Carnegie Mellon University 1992
#
#                      All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of CMU not be
# used in advertising or publicity pertaining to distribution of the
# software without specific, written prior permission.
#
# CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
# CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
#*****************************************************************************/
#
#
# Written by Allan Heydon for the Miro project at Carnegie Mellon
#
# SYNTAX
#   searchpath [path]
#
# SYNOPSIS
#   Prints the permissions, owner, and group-owner of all files/dirs from the
#   first component of 'path' down to the last component of 'path'. The
#   default value for 'path' is the current working directory. If 'path' is a
#   relative pathname, then the current working directory is prepended onto
#   'path' before processing it.
#
# EXAMPLE
#   Running "searchpath /usr1/heydon/test/a" produces:
#
#   drwxr-xr-x root     0        //
#   drwxr-xr-x root     0        /usr1/
#   drwxr-xr-x heydon   theory   /usr1/heydon/
#   drwxr-xrwx heydon   theory   /usr1/heydon/test/
#   drwxrwxrwx heydon   theory   /usr1/heydon/test/a/
#
# SEE ALSO
#   searchfs(1)

# handle case where 'path' not specified
if ( $#argv < 1 ) then
  set argv = ( $cwd )
endif

# Test if $1 is absolute or relative.
#
# This works by repeatedly taking the :h component of the path until doing
# so has no effect. At the completion of this process, $head will be empty
# for absolute paths and non-empty for relative ones.
set oldhead = $1
set head = $oldhead:h
while ( $head != $oldhead )
  set oldhead = $head
  set head = $oldhead:h
end

# prepend $cwd if necessary and set $pathname
if ( $head == "" ) then
  # absolute pathname in $1
  set pathname = $1
else
  # relative pathname in $1
  set pathname = "$cwd/$1"
endif

# initialize $tempfile
set tempfile = /tmp/searchpath-$$.txt
rm -f $tempfile

# write the components to $tempfile in reverse order
set newpathname = $pathname:h
while ( $newpathname != $pathname )
  # handle special case where we are at the front of the path
  if ( $pathname == "" ) then
    set currpath = "/"
  else
    set currpath = $pathname
  endif

  # append 'ls $currpath' to $tempfile
  if ( -d "$currpath" ) then
    ls -ldgF $currpath >> $tempfile
  else
    ls -lgF $currpath >> $tempfile
  endif

  # update variables for next iteration
  set pathname = $newpathname
  set newpathname = $pathname:h
end

# reverse the input and filter out the unnecessary columns using awk
cat -n $tempfile | sort -rn | awk '{printf("%s %-8s %-8s %s\n",$2,$4,$5,$10);}'
rm -f $tempfile
