#!/bin/sh
# hitail
# tails a file with -f, highlights the given strings using sed
# Peter Scheie
# NOTE: This script does NOT work on vsis02 because the sed is too old.  Therefore,
# testing of this script should not be done on vsis02. It runs fine on any Emperor 4.x.
#
# v1 20090131	Initial attempt
# v2 20090717	Added - option to take input from a pipe; increased number of strings 
#		that it will colorize from three to five; changed output font from
#		normal to bold to make it easier to read.
# v3 20100310	Added quotes to arguments given to sed commands so that multi-word
#		phrases can be highlighted (if quoted on the CL).

HELP()
{
echo "
USAGE		hitail <file or - > <string1> [<string2>] [<string3>] [<string4>] [<string5>]

DESCRIPTION	Runs 'tail -f' on file and highlights up to five different strings.
		The first string will be highlighted in red, the second in green,
		the third in blue, the fourth in magenta, and the fifth in yellow.
		Strings can be multi-word phrases if quoted, e.g. \"FTPPut fromUser\".

		If you are piping the output from another command to hitail, use a 
		dash (-) in place of the filename.

"
exit
}

[ ! -f $1 ] && [ "$1" != "-" ] && HELP
[ "$1" = "-" ] && CMD='cat -'
[ -f "$1" ] && CMD="tail -f $1"

case $# in
	6) $CMD |sed 's/'"$2"'/[1;37;41m&[m/g;s/'"$3"'/[1;37;44m&[m/g;s/'"$4"'/[1;37;42m&[m/g;s/'"$5"'/[1;37;45m&[m/g;s/'"$6"'/[1;37;43m&[m/g' ;;
	5) $CMD |sed 's/'"$2"'/[1;37;41m&[m/g;s/'"$3"'/[1;37;44m&[m/g;s/'"$4"'/[1;37;42m&[m/g;s/'"$5"'/[1;37;45m&[m/g' ;;
	4) $CMD |sed 's/'"$2"'/[1;37;41m&[m/g;s/'"$3"'/[1;37;44m&[m/g;s/'"$4"'/[1;37;42m&[m/g' ;;
	3) $CMD |sed 's/'"$2"'/[1;37;41m&[m/g;s/'"$3"'/[1;37;44m&[m/g' ;;
	2) $CMD |sed 's/'"$2"'/[1;37;41m&[m/g' ;;
	1) $CMD ;;
	*) HELP ;;
esac
