#!/bin/sh
#
# findrepo - find a string in current dir while ignoring repository metadata
#
# note:
#    - one can either supply a regular expression or fixed string
#    - if just a fixed string is supplied then we specify the -F option
#      to grep so that it uses fast string matching, otherwise we default
#      to an extended regular expression
#    - the match type auto selection is disabled if a specific grep
#      match type is specified as part of the optional grep options
#
# ---
# source: https://github.com/pixelb/scripts

# shellcheck disable=SC2250

progname=$(basename "$0")

if command -v gxargs >/dev/null 2>&1; then
  xargs() { gxargs "$@"; }
fi

usage() {
  printf %s "\
  ${progname}
    is a simple script to find a string in current directory while ignoring repo metadata

  usage: [grep options] [search_expr] [file wildcard]

  examples:
    ${progname} 'main' '*.[ch]'          -> fixed string search
    ${progname} '(main|mane)' '*.[ch]'   -> regular expression search
    ${progname} '-F' 'main(' '*.[ch]'    -> force fixed string search
    ${progname} '-L -F' 'main(' '*.[ch]' -> extra grep options
"
}

if [ "$#" -eq 0 ] || [ "$#" -gt 3 ]; then
  usage
  exit 1
fi

# enable search highlighting if supported by grep
echo | grep --color=auto "" >/dev/null 2>&1 && colour="--color=auto"

if [ "$#" -eq 1 ]; then
  case "$1" in
    -h | --help)
      usage
      exit 0
      ;;
    *) ;;
  esac
  # common shortcut to avoid needing to specify all files
  glob='*'
elif [ "$#" -eq 2 ]; then
  # if $1 begins with a '-' then we might be hitting the
  # ambiguous case where grep options are being specified,
  # so warn with info about (avoiding) the ambiguity
  if ! test "${1##-*}"; then
    echo "\
Warning: treating '$1' as the grep pattern, not extra options.
If this is intended, avoid the warning with an empty first parameter.
If this is not intended, please specify the file wildcard." >&2
  fi
  glob="$2"
elif [ "$#" -eq 3 ]; then
  grep_options="$1"
  shift
  glob="$2"
fi

# default to -E or -F as appropriate, not -G
if ! printf '%s\n' "${grep_options}" \
  | grep -E -- "-([EFGP]|regexp|fixed)" >/dev/null 2>&1; then
  # used fixed string matching for speed unless an ERE metacharacter used
  echo "$1" | grep '[.[\()*+?{|^$]' >/dev/null && type="-E" || type="-F"
  grep_options="${grep_options} ${type}"
fi

# parallel processing can help if one is I/O bound
# and random access to the files is efficient.
# generally -P is not related to the number of CPUs
# in this use case as I/O bound.
# TODO: maybe support -j to enable this
# num_proc='-P8'
# max_args='-n200'

repodirs=".git .svn CVS .hg .bzr _darcs .comp"
for dir in ${repodirs}; do
  repo_ign="${repo_ign}${repo_ign+" -o "}-name ${dir}"
done

# shellcheck disable=SC2086,SC2154
#LANG=C is to work around grep multibyte inefficiencies
#GREP_COLORS="mt=1;37;47" is bright yellow on black bg

find . \( -type d -a \( $repo_ign \) \) -prune -o \
  \( -type f -name "$glob" -print0 \) \
  | GREP_COLORS="mt=1;33;40" LANG=C \
    xargs -r0 $max_args $num_proc grep $colour $grep_options -- "$1"
