#!/usr/bin/perl -w
use strict;

#--------------------------------------[ Program Description
# fungrep.pl revision five. jan 28 2001. Joey Comeau
#
# Program for providing basic string matching in multiple
# files. Prints filename and line number or matched string.
# enables  the equivalent of grep -n with neat-o ansi 
# highlighting of your string
#
# DEC/10/00 Updated to highlight all occurances in the string.
# 	    Thanks to Nick Pilon and Christian Gloor
#
# Jan/28/01 (1901???)  Updated once more to allow multiple strings with
#			separate colors for each one. The files appear
#			AFTER the -f flag. So, it'd be like
#			fungrep "hi there" joey "!!DE!" -f file1 file2
#
#
#			
#
#-------------------------------------------------------------



#-------------------------------------[ Variable definitions

my $program_name;       # The program's name in case it is renamed by
                        # the sysadmin. Taken from $0.

my $temp = 0;		# temporary switch.

my $printline;
my @strings;
my $string;
my @files;
my $counter=0;          # counter variable.
my $filename;           # filename
my $i;
my $j = 42;
my $lineh;
my $s_s;
my %colors;

my @ful;                # 
my @match;              # 
                        # 
my @filearray;          # 
#-------------------------------------------------------------


# Function prototypes
#

#-----------------------------[ Sub; error()
# prints usage line

sub error(){    
     print "\e[34mUSAGE:\e[0m " . $program_name .
           " \"search pattern\"'s -f files\n"; 
     exit;

}               # end sub error()

##
#
#
# THE PROGRAM
#
#
#


#
# Get the passed parameters.
#
#



foreach (@ARGV){


	if ($_ eq "-f"){
		$temp = $_;
		$temp = 1;
		next;
	}

	if ($temp == 0){
		push (@strings, $_);        # Their search string.
	} else {
		push (@files, $_);
	}

}



$program_name=$0;

if( $#files < 0 ){      # If there are less than 0 elements 
                        # in the list of files
    error();            # call the error() function
                        # which will print the usage line
}                       # with neat ANSI escape sequences


for (@strings){
$colors{lc($_)} = $j++;
}


for $filename (@files){ # for each filename


#-------------------------------------------[ Check if textfile
# The next bit of code checks if the current file is a text file
# with -T. If it is, it opens the file to the file descriptor FD
# if not it calls the printinf function and skips to the next
# file.

-T $filename and open( FD, $filename ) 
           or print( "$filename -> non-textfile\n" ) and next;

#-------------------------------------------[ /check textfile

$counter=1;                     # nothing is ever on line 0
                                # (lines as *we* read them)

#--------------------------[ Copy the file
# Copy the file into an array
#

undef @filearray;
for(<FD>){push @filearray,$_};
close(FD);                      
$i=0;

#--------------------------[ /Copy the file

#--------------------------[ File parsing 
# for each line of the file.
# if it contains the string


for ( @filearray ){                     
  $temp = 0;
  $printline = $_;
  for $s_s (@strings){

    if ( $_ =~ /$s_s/ig ){
	$temp = 1;
  	$printline =~ s/$&/\e[$colors{lc($&)}m$&\e[0m/ig;
    }                           # end if(match)

  }				# end for @strings
	if ($temp == 1){
		$temp = 0;
		print"\n------------\n";
		print  	"Line:".($counter-1).
		     	"\t\tFilename:\e[35m$filename\e[0m\n";
		print $printline . "\n\n";
	}
        $counter++;             

}                               # end for ( <FD> )


#-----------------------[ /File parsing


}                               # end for(@ARGV)





