#!/usr/bin/perl -w

#counter.cgi
#	outputs an image for the given number
#

#setup environment
use CGI::Pretty;
use Getopt::Long;

#init cgi object to get environment variables
my $q=new CGI::Pretty;

#get variables from environment or sert default values
my $fgcolor=$q->param('fgcolor') || 'green';
my $bgcolor=$q->param('bgcolor') || 'black';
my $num=$q->param('num') ||'0001';
my $width=$q->param('width');
my $height=$q->param('height');
my $font=$q->param('font') || 'medium';
my $fsize=$q->param('fsize') || '';
my $padding=$q->param('padding') || 5;
my $border=$q->param('border') || 0;
my $bordercolor=$q->param('bordercolor') || 'white';
my $borderstyle=$q->param('borderstyle') || 'solid';
my $standAlone=$q->param('standAlone') || '0';

GetOptions ('fgcolor=s' => \$fgcolor,
			'bgcolor=s' => \$bgcolor,
			'num=s' => \$num,
			'width=s' => \$width,
			'height=s' => \$height,
			'font=s' => \$font,
			'fsize=s' => \$fsize,
			'padding=s' => \$padding,
			'border=s' => \$border,
			'bordercolor=s' => \$bordercolor,
			'borderstyle=s' => \$borderstyle,
			'standAlone=s' => \$standAlone
			);


#setup font info
$fsize='xx-large' if (lc($font) eq 'giant');
$fsize='large' if (lc($font) eq 'large');
$fsize='medium' if (lc($font) eq 'medium');
$fsize='small' if (lc($font) eq 'small');
$fsize='xx-small' if (lc($font) eq 'tiny');

my $fheight=$lineheight || 0;
$fheight=24 if ($fsize eq 'xx-large');
$fheight=16 if ($fsize eq 'large');
$fheight=12 if ($fsize eq 'medium');
$fheight=10 if ($fsize eq 'small');
$fheight=8 if ($fsize eq 'xx-small');
$fheight=$lineheight if $lineheight;


$width=length($num)+($padding*2)+($border*2) unless $width;
$height=$fheight+($padding*2)+($border*2) unless $height;


print $q->header() if $standAlone;

#foreach $k ($q->param){
#	print ' '.$k.' = '.$q->param($k);
#}


$bgcolor=myColor2web($bgcolor);
$fgcolor=myColor2web($fgcolor);
$bordercolor=myColor2web($bordercolor);


my $out= "<span style=\'";
	$out.="background-color:$bgcolor;";
	$out.="padding-top:$padding"."px;";
	$out.="padding-right:$padding"."px;";
	$out.="padding-bottom:$padding"."px;";
	$out.="padding-left:$padding"."px;";
	$out.="font-size:$fsize;";
	$out.="font-family:$font;";
	$out.="color:$fgcolor;";
	$out.="width:$width".'ex;';
	$out.="height:$height".'em;' if ($height);
	$out.="border-top:$border"."px $borderstyle $bordercolor;";
	$out.="border-left:$border"."px $borderstyle $bordercolor;";
	$out.="border-right:$border"."px $borderstyle $bordercolor;";
	$out.="border-bottom:$border"."px $borderstyle $bordercolor;";
$out.="\'>$num</span>";
print $out;

sub myColor2web{
	my ($web)=@_;
	if ($web=~/^0x([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])/){
		return '#'.$1.$2.$3;
	}
	return $web;
}
sub handleNoArg{
	#foreach $i (@_){print $i;}

}