#!/bin/tcsh -f

# 06/06 Updating for Linux version of grep by using -a; rest as before.
# -a placed at end so won't break old grep.

# This can be called as supergrep, superegrep, or superfgrep to behave
# like grep, egrep, or fgrep respectively.  A couple new options are
# added as indicated below.  Normally returns same exit status as
# grep/egrep/fgrep.

# -R: Search files recursively in directories encountered by using
#     "find" (without the "-follow" option) so that symbolic links
#     will not lead to excessively many files to search.  Always
#     displays results as if multiple files are being searched (i.e.,
#     with file name); also, output is safeguarded by piping to "cat -v".

# -t: Search only in text/script files as indicated by "file" command.
#     Displays file names whenever there are multiple file name arguments,
#     whether they are text/script files or not.

# Options I am about to add:

# -T: For each file in my teaching directory (actually any file
#     containing "/teaching/"), consider all corresponding files for the
#     same term.

# -N: For each file in my teaching directory (actually any file
#     containing "/teaching/"), consider all corresponding files for the
#     same course number.

# When the new options are used, an appearance of "-" as one of the
# file names (to make standard input one of the places searched) is
# ignored but indicates that whatever follows is a pattern or file
# name and not an option.  In fact, with the -R option, all file names
# starting with "-" are ignored (but file names that are discovered
# through recursive exploration may begin with "-").  It is still
# possible to search standard input when no file arguments are given;
# in that case, the new options are ignored.

# (Options already used in some version of grep, egrep, fgrep:
# AB<num>CEFGVbchilnqsvwxef.)  (Pre-Linux; now different.)

set debug = 0

set noglob

set fullprogname = $0
set fullprogname = `echo $fullprogname | sed 's+^\([^/]\)+./\1+'`
set progdir = $fullprogname:h
set progname = $fullprogname:t

set greptype = `echo $progname | sed 's+super++'`

if ($debug > 1) echo "${progname}: Word too long may result from debug > 1."

set opts = ""
set nonopts = ""
set postopts = 0
while ($#argv>0)
  if ("$1" !~ -* || $postopts) then
    set nonopts = ($nonopts $1)
    shift
  else
    if ("$1" == "-") then
      set postopts = 1
    else
      set opts = ($opts $1)
      foreach specopt (A B C D d e f m) # expanded from just e and f for Linux
        if ("$1" == "-$specopt") then
          set opts = ($opts $2)
          shift
        endif
      end
      shift
    endif
  endif
end

set pattern = $nonopts[1]

if ($debug) then
   echo "progname: $progname, greptype: $greptype"
   echo "opts|$opts|stpo"
   echo "pattern|$pattern|nrettap"
endif

set recursive=""
set textonly=""
set revisedopts=(`echo "$opts" | sed -n 's+\(-[^ ]*\)R+\1+gp'`)
if ("$revisedopts" != "") then
  set opts="$revisedopts"
  set recursive="-R"
endif
set revisedopts = (`echo "$opts" | sed -n 's+\(-[^ ]*\)t+\1+gp'`)
if ("$revisedopts" != "") then
  set opts = "$revisedopts"
  set textonly="-t"
endif
set opts = (`echo "$opts " | sed 's+- ++g'`)

if ($debug) then
  echo "recursive:|$recursive|evisrucer"
  echo "textonly:|$textonly|ylnotxet"
  echo "revised opts|$opts|stpo desiver"
  echo "numotherargs: $#argv"
endif

if ($#nonopts == 1) then # grep in standard input; no effect for -t and -R.
  $greptype $opts "$pattern"
  exit $status
endif

if ($#nonopts == 0) then
  echo "usage: $progname <$greptype arguments with R and t as new options>"
  $greptype $opts
  exit $status
endif

set files = ($nonopts[2-])
if ($debug) then
 echo "numfiles: $#files"
 if ($debug > 1) then
   echo "files|$files|selif"
 endif
endif

if ("$recursive" != "") then
  set files = (`echo " $files" | sed 's+ -[^ ]*++g'`)
  if ($#files == 0) set files = /dev/null
  find $files ! -type d -exec $progname $opts -H $textonly -a $pattern {} \; | cat -v
  exit $status
endif
if ("$textonly" != "") then
  set files = (`file $files -L | egrep 'text|script|No such|^-:' | grep -v '[Pp]ost[Ss]cript' | sed 's+:[ 	].*++'`)
  if ($debug) echo "posttextonly numfiles: $#files"
endif
if ($#files == 0) set files = /dev/null

$greptype $opts "$pattern" $files -a
