#!/usr/bin/env bash

# 02/10 Converted to bash to handle arguments that are quoted with contained spaces.
# Still need to do about all of for arg interior and stuff beyond that.

# 02/10 Updated for Linux options and to better handle options that
# are followed by an associated argument.

# 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.)

debug=2

set -f # noglob

fullprogname=$0
fullprogname=`echo $fullprogname | sed 's+^\([^/]\)+./\1+'`
progdir=${fullprogname%/*}
progname=${fullprogname##*/}

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

if [ $debug -ge 1 ]; then
  echo "${progname}: Word too long may result from debug > 1."
fi

opts=""
nonopts=""
postopts=0

for arg; do
  if [ "$arg" !~ -* || $postopts ]; then
    nonopts=($nonopts "$arg")
  else
    if ("$arg" == "-") then
      set postopts = 1
    else
      set opts = ($opts $arg)
      foreach specopt (A B C D d e f m) # expanded from just e and f for Linux
        if ("$arg" == "-$specopt") then
          set opts = ($opts $2)
          shift
        endif
      end
      shift
    endif
  endif
done

exit

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|^-:' | sed 's+:[ 	].*++'`)
  if ($debug) echo "posttextonly numfiles: $#files"
endif
if ($#files == 0) set files = /dev/null

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