#!/usr/bin/perl
#
# gwhiz - GWhiz. This will whiz files or output streams. Perl.
#
# Use this command like the more command. It works on any platform
#  that has Perl.
#
# 17-Jan-2005   ver 1.12
#
# USAGE: gwhiz [-abchHmsvw] [filename]
#    eg,
#       gwhiz /etc/release
#       df -k | gwhiz
#
#       gwhiz -a     # all bells and whistles
#       gwhiz -b     # bells
#       gwhiz -c     # colour (some terms only)
#       gwhiz -H     # output in HTML 
#       gwhiz -m     # more colour
#       gwhiz -s     # slow
#       gwhiz -v     # verbose
#       gwhiz -w     # wide
#
# COPYRIGHT: Copyright (c) 2004 Brendan Gregg.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#  (http://www.gnu.org/copyleft/gpl.html)
#
# CONTRIBUTION LIST:
#       Boyd Adamson
#       Gunther Feuereisen
#
# 17-Jan-2005   Brendan Gregg   Created this.
# 18-Jan-2005   Boyd Adamson	Wasted too much time adding more colour & HTML

use Getopt::Std;
$| = 1;

#
#  Command Line Arguments
#
&usage() if $ARGV[0] eq "--help";
getopts('abchHmsvw') || &usage();
&usage() if $opt_h;
$COLOUR = 1 if $opt_c;
$MORECOLOUR = 1 if $opt_m || $opt_a;
$HTML = 1 if $opt_H;
$BELL = 1 if $opt_b || $opt_a;
$SLOW = 1 if $opt_s || $opt_a;
$WIDE = 1 if $opt_w || $opt_a;
$VERBOSE = 1 if $opt_v || $opt_a;
$colour_min = 31;
$colour_max = 36;
$colour_num = 31;
@Superlatives = ("Gee Whiz","Wow","Cor Blimey","Fairdinkum",
 "Crikey","Wowzer","Gosh","Good Grief");
@Funky_attrib = qw( em strong strike u blink );

#
#  Whiz Data
#
print "<pre><b>" if $HTML;
while ($line = <>) {
	chomp($line);
	whiz($line);
	print "$line\n";
	sleep(1) if $SLOW;
}
print "</b></pre>\n" if $HTML;


#################
#  Subroutines
#

# whiz - Whiz the input variable.
#       This function will write to the input variable,
#       similar to chomp.
#
sub whiz {
	$line = shift;
	$line =~ tr/a-z/A-Z/;
	if ($VERBOSE) {
		if ( int(rand(3)) == 0) {
			$index = int(rand(@Superlatives));
			$line .= " $Superlatives[$index]";
		}
	}
	$line .= "!!!";
	if ($WIDE) {
		$line .= "!" x int(rand(16));
	}
	$line = colourise($line);
	if ($BELL && !$HTML) {
		$line .= "\a";
	}
}

# colourise - return a colourful version of a string
#
sub colourise {
		my ($line) = shift;
		if ($MORECOLOUR) {
			if ($HTML) {
				$line =~ s/./html_funkyise($&)/ge;
			} else {
				$line =~ s/./ansi_colourise($&)/ge;
			}
		} elsif ($COLOUR) {
			if ($HTML) {
				$line = html_funkyise($line);
			} else {
				$line = ansi_colourise($line, $colour_num);
			}
			$colour_num++;
			$colour_num = $colour_min if $colour_num > $colour_max;
		} 
		return $line;
}

# ansi_colourise - return a string made colourful using ansi sequences
#       Second argument is the colour number or 0 for a random one.
#
sub ansi_colourise {
		my ($string, $colour) = @_;
		if (! $colour) {
			$colour = (int(rand($colour_max-$colour_min))
			 + $colour_min);
		}
		my $res = "\e[";
		$res .= $colour;
		$res .= ";1m";
		$res .= $string;
		$res .= "\e[0m";
		return $res;
}

# html_funkyise - return a string made more interesting using HTML
#
sub html_funkyise {
		my ($string) = shift;

		return $string if $string eq ' ';
		$attrib = 0;
		if ( int(rand(500)) == 0) {
			$attrib = $Funky_attrib[int(rand(@Funky_attrib))];
		}
		my $res = '<font color="';
		$res .= random_html_colour();
		$res .= '">';
		$res .= "<$attrib>" if $attrib;
		$res .= $string;
		$res .= "</$attrib>" if $attrib;
		$res .= '</font>';
		return $res;
}

# random_html_colour - return a string for a random html color
#
sub random_html_colour {
	my ($red, $green, $blue, $brightness);
	$brightness = 500000;
	# Choose a dark enough colour
	while ( $brightness > 150) {
		$red   = int(rand(255));
		$green = int(rand(255));
		$blue  = int(rand(255));
		$brightness = (299 * $red + 587 * $green + 114 * $blue) / 1000;
	}
	my $res = join( "", 
	 unpack("h2h2h2", pack("CCC", $red, $green, $blue )));
	$string = "#" . $res;
}


# usage - print a usage and exit.
#
sub usage {
	print "USAGE: gwhiz [-abchHmsvw] [filename]\n";
	print "   eg,\n";
	print "      gwhiz /etc/release\n";
	print "      date | gwhiz\n\n";
	print "      gwhiz -a     # all bells and whistles\n";
	print "      gwhiz -b     # bells\n";
	print "      gwhiz -c     # colour (some terms only)\n";
	print "      gwhiz -H     # output in HTML\n"; 
	print "      gwhiz -m     # more colour\n";
	print "      gwhiz -s     # slow\n";
	print "      gwhiz -v     # verbose\n";
	print "      gwhiz -w     # wide\n";
	exit 1;
}
