#!/usr/bin/perl -w

#######################################################################
# Junkbuster Colorizer v0.1.0                                         #
# by Paul Duncan <pabs@pablotron.org>                                 #
# Usage:                                                              #
#  tail -f /var/log/junkbuster/logfile | junkcolor.pl                 #
#######################################################################

##########################################################
# Color Escape Sequences                                 #
# don't touch these unless you know what you're doing :) #
##########################################################
%color = ('BLACK'         => "\033[0;30m",
          'BLUE'          => "\033[0;34m",
          'GREEN'         => "\033[0;32m",
          'CYAN'          => "\033[0;36m",
          'RED'           => "\033[0;31m",
          'PURPLE'        => "\033[0;35m",
          'BROWN'         => "\033[0;33m",
          'LIGHT_GRAY'    => "\033[0;37m",
          'DARK_GRAY'     => "\033[1;30m",
          'LIGHT_BLUE'    => "\033[1;34m",
          'LIGHT_GREEN'   => "\033[1;32m",
          'LIGHT_CYAN'    => "\033[1;36m",
          'LIGHT_RED'     => "\033[1;31m",
          'LIGHT_PURPLE'  => "\033[1;35m",
          'YELLOW'        => "\033[1;33m",
          'WHITE'         => "\033[1;37m",
          'NOTHING'       => "\033[0m"
         );

##################################################################
# Junkbuster Path                                                #
# I only use this for hiliting; it doesn't get touched otherwise #
##################################################################
$junkbuster_path="/usr/sbin/junkbuster";

###################################################################
# Item Colors                                                     #
# I use debug level 7, so the options below should cover any      #
# combination of levels 1, 2, and 4.                              #
###################################################################
%item = ('path'         => $color{'GREEN'},
         'connect'      => $color{'CYAN'},
         'accept'       => $color{'CYAN'},
         'url'          => $color{'YELLOW'},
         'gpc'          => $color{'CYAN'},
         'gpc_url'      => $color{'YELLOW'},
         'crunch_gpc'   => $color{'CYAN'},
         'crunch_url'   => $color{'LIGHT_RED'},
         'crunch_desc'  => $color{'LIGHT_CYAN'},
         'ok'           => $color{'CYAN'}
        );

##############################
# The Mythical, Magical Code #
##############################
while (<>) {
   s/($junkbuster_path)/$item{'path'}$1$color{'NOTHING'}/g;
   s/(connect to):\s+(.*) .../$item{'connect'}$1$color{'NOTHING'}: $item{'url'}$2$color{'NOTHING'} .../g;
   s/(accept connection)/$item{'accept'}$1$color{'NOTHING'}/g;
   s/(GPC)[\t ]+([^ ]*)$/$item{'gpc'}$1$color{'NOTHING'} $item{'gpc_url'}$2$color{'NOTHING'}/g;
   s/(GPC)[\t ]+([^\t ]+)[\t ]+((?:image )?crunch!)$/$item{'crunch_gpc'}$1$color{'NOTHING'} $item{'crunch_url'}$2$color{'NOTHING'} $item{'crunch_desc'}$3$color{'NOTHING'}/g;
   s/(OK)$/ $item{'ok'}$1$color{'NOTHING'}/;

   print;
}

