#!/bin/bash
#===============================================================================
#
#          FILE: highlight
#
#         USAGE: highlight <File name or stdin(-)> <Pattern1> [Pattern2] ... [Pattern21]
#
#   DESCRIPTION: Highlight text with patterns
#
#       OPTIONS: <File name or stdin(-)> <Pattern1> [Pattern2] ... [Pattern21]
#
#  REQUIREMENTS: gawk
#
#         NOTES: 
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Fri Aug 19 07:08:54 EDT 2011
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                      # Enable job control

[ "$1" == "" ] && exec echo "Usage: $0 <File name or stdin(-)> <Pattern1> [Pattern2] ... [Pattern21]"

TEXT_SOURCE="$1"
shift
FG_COLOR="31,32,33,35,36,32,33,34,35,31,33,35,36,31,32,35,36,31,32,33,36"
BG_COLOR="40,40,40,40,40,41,41,41,41,42,42,42,42,44,44,44,44,45,45,45,45"

awk \
-vstr1="$1" \
-vstr2="$2" \
-vstr3="$3" \
-vstr4="$4" \
-vstr5="$5" \
-vstr6="$6" \
-vstr7="$7" \
-vstr8="$8" \
-vstr9="$9" \
-vstr10="${10}" \
-vstr11="${11}" \
-vstr12="${12}" \
-vstr13="${13}" \
-vstr14="${14}" \
-vstr15="${15}" \
-vstr16="${16}" \
-vstr17="${17}" \
-vstr18="${18}" \
-vstr19="${19}" \
-vstr20="${20}" \
-vstr21="${21}" \
'
BEGIN{
repltext1=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 1)';'$(echo "$BG_COLOR" | cut -d , -f 1)'m&%c[0m", 0x1B,0x1B);
repltext2=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 2)';'$(echo "$BG_COLOR" | cut -d , -f 2)'m&%c[0m", 0x1B,0x1B);
repltext3=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 3)';'$(echo "$BG_COLOR" | cut -d , -f 3)'m&%c[0m", 0x1B,0x1B);
repltext4=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 4)';'$(echo "$BG_COLOR" | cut -d , -f 4)'m&%c[0m", 0x1B,0x1B);
repltext5=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 5)';'$(echo "$BG_COLOR" | cut -d , -f 5)'m&%c[0m", 0x1B,0x1B);
repltext6=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 6)';'$(echo "$BG_COLOR" | cut -d , -f 6)'m&%c[0m", 0x1B,0x1B);
repltext7=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 7)';'$(echo "$BG_COLOR" | cut -d , -f 7)'m&%c[0m", 0x1B,0x1B);
repltext8=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 8)';'$(echo "$BG_COLOR" | cut -d , -f 8)'m&%c[0m", 0x1B,0x1B);
repltext9=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 9)';'$(echo "$BG_COLOR" | cut -d , -f 9)'m&%c[0m", 0x1B,0x1B);
repltext10=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 10)';'$(echo "$BG_COLOR" | cut -d , -f 10)'m&%c[0m", 0x1B,0x1B);
repltext11=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 11)';'$(echo "$BG_COLOR" | cut -d , -f 11)'m&%c[0m", 0x1B,0x1B);
repltext12=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 12)';'$(echo "$BG_COLOR" | cut -d , -f 12)'m&%c[0m", 0x1B,0x1B);
repltext13=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 13)';'$(echo "$BG_COLOR" | cut -d , -f 13)'m&%c[0m", 0x1B,0x1B);
repltext14=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 14)';'$(echo "$BG_COLOR" | cut -d , -f 14)'m&%c[0m", 0x1B,0x1B);
repltext15=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 15)';'$(echo "$BG_COLOR" | cut -d , -f 15)'m&%c[0m", 0x1B,0x1B);
repltext16=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 16)';'$(echo "$BG_COLOR" | cut -d , -f 16)'m&%c[0m", 0x1B,0x1B);
repltext17=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 17)';'$(echo "$BG_COLOR" | cut -d , -f 17)'m&%c[0m", 0x1B,0x1B);
repltext18=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 18)';'$(echo "$BG_COLOR" | cut -d , -f 18)'m&%c[0m", 0x1B,0x1B);
repltext19=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 19)';'$(echo "$BG_COLOR" | cut -d , -f 19)'m&%c[0m", 0x1B,0x1B);
repltext20=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 20)';'$(echo "$BG_COLOR" | cut -d , -f 20)'m&%c[0m", 0x1B,0x1B);
repltext21=sprintf("%c[1;'$(echo "$FG_COLOR" | cut -d , -f 21)';'$(echo "$BG_COLOR" | cut -d , -f 21)'m&%c[0m", 0x1B,0x1B);
}{
if(str1 != "") gsub(str1,repltext1);
if(str2 != "") gsub(str2,repltext2);
if(str3 != "") gsub(str3,repltext3);
if(str4 != "") gsub(str4,repltext4);
if(str5 != "") gsub(str5,repltext5);
if(str6 != "") gsub(str6,repltext6);
if(str7 != "") gsub(str7,repltext7);
if(str8 != "") gsub(str8,repltext8);
if(str9 != "") gsub(str9,repltext9);
if(str10 != "") gsub(str10,repltext10);
if(str11 != "") gsub(str11,repltext11);
if(str12 != "") gsub(str12,repltext12);
if(str13 != "") gsub(str13,repltext13);
if(str14 != "") gsub(str14,repltext14);
if(str15 != "") gsub(str15,repltext15);
if(str16 != "") gsub(str16,repltext16);
if(str17 != "") gsub(str17,repltext17);
if(str18 != "") gsub(str18,repltext18);
if(str19 != "") gsub(str19,repltext19);
if(str20 != "") gsub(str20,repltext20);
if(str21 != "") gsub(str21,repltext21);
print
}' "$TEXT_SOURCE" 

