#!/usr/bin/perl

use GD;
require "../cgi-lib.pl";
ReadParse(*params);

$scale_area = $params{scale} || 50;
my(%groups);
foreach my $group (split /\0/,$params{groups}) {
    if($params{"$group\_cnt"}) {
	$groups{$group}{cnt} = $params{"$group\_cnt"};
	$groups{$group}{area} = $params{"$group\_cnt"}*$scale_area;
	$groups{$group}{radius} = int sqrt($groups{$group}{area}/3.14159);
	$groups{$group}{name} = $params{"$group\_name"} || "?";
	#$groups{$group}{name} = $groups{$group}{radius};
	$groups{$group}{width} =
	    (5*length($groups{$group}{name}) > 2*$groups{$group}{radius}) ?
	    5*length($groups{$group}{name}) :
	    2*$groups{$group}{radius};
	$groups{$group}{height} = 2*$groups{$group}{radius} + 10;
    }
}
my($width) = 3;
my($height) = 0;
foreach my $group (keys %groups) {
    $width += 3;
    $width += $groups{$group}{width};
    if($groups{$group}{height} > $height) { $height = $groups{$group}{height}; }
}
$height += 6;
if($width > 2000 || $height > 2000) {
    error("Sorry, that's too big!");
}
$img = new GD::Image($width,$height,1);
$img->fill(1,1,color(223,223,223));
$xpos = 2;
foreach my $group (sort {$groups{$a}{cnt} <=> $groups{$b}{cnt}} (keys %groups)) {
    $grp = $groups{$group};
    $grp->{color} = color($params{"$group\_r"},$params{"$group\_g"},$params{"$group\_b"});
    if($params{filled}) {
	$grp->{inv} = inv_color($params{"$group\_r"},$params{"$group\_g"},$params{"$group\_b"});
    } else {
	#sneaky!
	$grp->{inv} = $grp->{color};
    }
    if(2.5*length($grp->{name}) > $grp->{radius}) {
	$img->string(gdTinyFont,$xpos,3,$grp->{name},$grp->{color});
	if($params{filled}) {
	    $img->filledArc($xpos+int(2.5*length($grp->{name})),12+$grp->{radius},2*$grp->{radius},2*$grp->{radius},0,360,$grp->{color});
	} else {
	    $img->arc($xpos+int(2.5*length($grp->{name})),12+$grp->{radius},2*$grp->{radius},2*$grp->{radius},0,360,$grp->{color});
	}
    } else {
	if($params{filled}) {
	    $img->filledArc($xpos+$grp->{radius},12+$grp->{radius},2*$grp->{radius},2*$grp->{radius},0,360,$grp->{color});
	} else {
	    $img->arc($xpos+$grp->{radius},12+$grp->{radius},2*$grp->{radius},2*$grp->{radius},0,360,$grp->{color});
	}
	if(5*length($grp->{name})+6 < 2*$grp->{radius}) {
	    $img->string(gdTinyFont,$xpos+$grp->{radius}-(5*length($grp->{name})/2),8+$grp->{radius},$grp->{name},$grp->{inv});
	} else {
	    $img->string(gdTinyFont,$xpos+$grp->{radius}-(5*length($grp->{name})/2),3,$grp->{name},$grp->{color});
	}
    }
    $xpos+=$grp->{width}+3;
}

print "Content-Type: image/png\n\n";
binmode STDOUT;
print $img->png;

sub color {
    my($r,$g,$b) = @_;
    $r||=0;
    $g||=0;
    $b||=0;
    if(!defined($pal{"$r\_$g\_$b"})) {
        $pal{"$r\_$g\_$b"} = $img->colorAllocate($r,$g,$b);
    }
    return $pal{"$r\_$g\_$b"};
}

sub inv_color {
    my($r,$g,$b) = @_;
    $r += 127;
    $g += 127;
    $b += 127;
    $r = $r % 255;
    $g = $g % 255;
    $b = $b % 255;
    return color($r,$g,$b);
}

sub error {
    my($str) = @_;
    if(!$img) {
	$img = new GD::Image(6+5*length($str),23,1);
	$img->fill(1,1,color(223,223,223));
    }
    $img->string(gdTinyFont,3,4,"Error:",color(255,0,0));
    $img->string(gdTinyFont,3,12,$str,color(0,0,0));
    print "Content-Type: image/png\n\n";
    binmode STDOUT;
    print $img->png;
    exit(0);
}
