#!/usr/bin/perl -w

###
# colorex version 0.2
#
# http://www.linibou.com
#

use strict;
use Getopt::Long;
use File::Basename;

my $scriptname = basename $0;
my($user,$help,$stdin,$line,$bisounours);
my(@blue,@red,@green,@yellow,@magenta,@cyan,@underline,@bold);
my %colors=("blue" => \@blue,
	    "red" => \@red,
	    "green" => \@green,
	    "yellow" => \@yellow,
	    "magenta" => \@magenta,
	    "cyan" => \@cyan,
	    "underline" => \@underline,
	    "bold" => \@bold);
my %colorcode=("blue" => "\33[34m",
	       "red" => "\33[31m",
	       "green" => "\33[32m",
	       "yellow" => "\33[33m",
	       "magenta" => "\33[35m",
	       "cyan" => "\33[36m",
	       "bold" => "\33[1m",
	       "underline" => "\33[4m",
	       "reset" => "\33[0m");
my %colormap=("0" => "blue",
	      "1" => "red",
	      "2" => "green",
	      "3" => "yellow",
	      "4" => "magenta",
	      "5" => "cyan",
	      "6" => "bold");
Getopt::Long::Configure( "bundling" );
GetOptions( "b|blue=s@" => \@blue,
	    "r|red=s@" => \@red,
	    "g|green=s@" => \@green,
	    "y|yellow=s@" => \@yellow,
	    "m|magenta=s@" => \@magenta,
	    "c|cyan=s@" => \@cyan,
	    "B|bold=s@" => \@bold,
	    "u|underline=s@" => \@underline,
	    "n|bisounours" => \$bisounours,
	    "" => \$stdin,
	    "h|help") or print_usage($scriptname) and exit 1;

if($help){
    print_usage($scriptname);
    exit 0;
}
if($stdin){
    while(defined($line = <STDIN>)){
	colorise_line($line);
    }
    exit 0;
}
if (@ARGV){
    foreach(@ARGV){
	if(test_file($_)){
	    print_file($_);
	}else{
	    print "can't read file : $_\n";
	}
    }
}else{
    print_usage($scriptname);
    exit 1;
}

sub print_usage
{
    my ($scriptname) = @_;
        print "
Display files or sdtin with pretty colors for matched patterns. Instead of filename use - for stdin.

usage : $scriptname [options] <file1> <file2> ... or -
options :
-b, --blue <pattern>	: display \"pattern\" in blue, in the same way, use -r or --red <pattern> etc...
			  availables colors are: blue, red, green, yellow, magenta, cyan, underline.
-B, --bold <pattern>
-n, --bisounours	: display with random colors.
-h, --help		: display this message.

you can use regexp, but if you really want to macth a \"[\" or a \"{\" for example you must put a \\ before

e.g. : $scriptname -g foo --yellow foo -c 'bar.[0-9]{2}' --red '\\[2\\]' toto.txt

http://www.linibou.com
";
}
sub test_file
{
    my ($file2test) = @_;
    if( -r $file2test ){
	return 1;
    }else{
	return 0;
    }
}
sub print_file
{
    my ($file) = @_;
    open(FILE,"<$file");
    while(defined($line = <FILE>)){
	colorise_line($line);
    }
    close(FILE);
}
sub colorise_line
{
    my ($line) = @_;
    if($bisounours){
	my $rand = int(rand(7));
	my $color = $colormap{$rand};
	$line =~ s#(.*)#$colorcode{$color}$1$colorcode{reset}#;
    }else{
	foreach my $color (keys(%colors)){
	    foreach my $ref_patern ($colors{$color}){
		foreach my $pattern (@$ref_patern){
		    $line =~ s#($pattern)#$colorcode{$color}$1$colorcode{reset}#g;
		}
	    }
	}
    }
    print $line;
}
