#!/bin/sh -e

usage() {
  printf %s "\
$(basename "$0")
  a simple quick grep script - based on ripgrep

commands:

  - Recursively search the current folder for a regex pattern:

  gr {{pattern}}

  - Search for a pattern only in a certain filetype (e.g., html, css, etc.):

  gr -t {{file type}} {{pattern}}

  - Search for a pattern in files matchingr a glob (e.g., README.*):

  gr -g {{glob}} {{pattern}}

  - Match only at word boundaries:

  gr -w {{pattern}}

  - Case-sensitive search:

  gr -s {{pattern}}

  - Search for a pattern only in a file or folder:

  gr {{pattern}} {{file or folder}}

extended:

  - Replace a pattern (will only print results, use wg to do actual replace):

  gr {{pattern}} {{filename}} --replace {{replacement}}

more information:
  https://github.com/BurntSushi/ripgrep or [help rg]
"
  exit 0
}

[ "$1" = "-h" ] || [ "$1" = "--help" ] && { usage; }

if command -v rg >/dev/null 2>&1; then
  # see tilde/ripgreprc for default options
  # rg --hidden --json --context=2 "$@" | delta
  rg --follow --hidden --smart-case --json --context=2 "$@" 2>/dev/null \
    | delta --diff-highlight
elif command -v ack >/dev/null 2>&1; then
  ack -ri "$@"
else
  grep --recursive --ignore-case "$@"
fi
