#!/bin/csh -f
# dirr
# -----------------------------------------------------------------------------
# Shell script to search the current directory and all subdirectories for a 
# file with a specified name.
# -----------------------------------------------------------------------------
# Revision History:
#   $Log$
# -----------------------------------------------------------------------------

if ("$1" == "-h" || "$1" == "--help") then
   echo "Usage: $0:t [options] [filename_pattern]"
   echo "       where options are:"
   echo "         -i     = Ignore case"
   echo "         -l     = Long format (like ls -l), not just names"
   echo "         -v     = Verbose.  Show the generated pipe of commands"
   echo "         -h     = Show this help info"
   echo "         --help = Show this help info"
   echo "Example: $0:t"
   echo "Example: $0:t -l"
   echo "Example: $0:t file.txt"
   echo "Example: $0:t -i file.txt"
   echo -n "Example: $0:t " ; echo '"*.txt"'
   echo -n "Example: $0:t -l " ; echo '"*.txt"'
   echo -n "Example: $0:t -i -l " ; echo '"*.txt"'
   echo -n "Example: $0:t -l -i " ; echo '"*.txt"'
   echo "It is also useful to invoke $0:t from within back ticks as:"
   echo -n "Example: " ; echo -n 'rm  `' ; echo -n "$0:t " ; echo '"*.txt"`'
   echo -n "Example: " ; echo -n 'cat `' ; echo -n "$0:t " ; echo '"*.txt"`'
   echo -n "Example: $0:t -l " ; echo '"*.txt"'
   exit 1
endif

# Collect command line options
set name_or_iname_option = "-name"
set sort_case_option     = 
set print_or_ls_option   = "-print"
set sort_key_option      = 
set verbose_option       = "false"
while ($#argv > 0)
    if ("$1" == "-i") then
        # Ignore case
        shift
        set name_or_iname_option = -iname
        set sort_case_option     = -f
    else if ("$1" == "-l") then
        # Long format, sorting by the 11th field of the format, which is
        # the filename.  The find -ls lines have format:
        # 1       2 3          4 5    6     7   8   9  10    11
        # 2203712 8 -rwxr-xr-x 1 fred staff 154 Jun 11 19:33 ./dirrls
        shift
        set print_or_ls_option = -ls
        set sort_key_option    = "-k 11"
    else if ("$1" == "-v") then
        # Verbose option
        shift
        set verbose_option = "true"
    else
        # Not a recognized option.  Assume it's the filename pattern.
        break
    endif
end
if ($#argv == 0) then
    # No -name or -iname option since no filename_pattern was specified
    # for case to be applied to.
    set name_or_iname_option = 
endif

if ($verbose_option == "true") then
    echo "find . $name_or_iname_option $*:q $print_or_ls_option | sort $sort_case_option $sort_key_option"
endif
          find . $name_or_iname_option $*:q $print_or_ls_option | sort $sort_case_option $sort_key_option

exit $status