#!/bin/sh
#< Highlighted grep
# Modified sed on 10/01/04 so it will accept quoted regexes as $1,
# and doesn't break
# 20/10/04 - Added support so hgrep can be used in a pipe
# Make sure forward slashes are backslash-escaped!

thiscmd=`basename $0`

if [ "$#" -lt "1" -o "$#" -gt "3" ]; then
   echo "USAGE: $thiscmd [-n] searchterm [file]" && exit 1
fi

grep_n=""
case $1 in
  "-n" ) grep_n="-n"; shift ;;
  *   ) ;;  #do nothing
esac

searchterm="$1"
file_to_search="$2"

if [ -z "$2" ]; then
  # assume pipe mode
  grep $grep_n "$1" | sed "s/\($1\)/$(tput smso)\1$(tput rmso)/g"
  exit 0
fi

if [ ! -e "$2" ]; then
  echo "Error: ${2}: File not found"
  exit 1
fi

grep $grep_n "$1" "$2" | sed "s/\($1\)/$(tput smso)\1$(tput rmso)/g"

exit 0