#!/usr/bin/perl

use DBI;
use CGI qw/escape/;
use String::CRC32;
use strict;
my $query = new CGI; 

my $dbh = DBI->connect("DBI:mysql:database=wuukiee_play;host=localhost", "mcc", "kQ89Ef2f49Gvc2FQq");
unless ($dbh) { print $query->redirect("mysql.gif"); return; }
my $sth;
# $sth = $dbh->prepare(qq[update usruser set hits=hits+1, last=now() where name="$name"]); $sth->execute(); $sth->finish();

my $pickb;

if (int($query->param('a'))) {
	$pickb = "where id=" . int($query->param('a'));
} else {
	$pickb = "order by rand() limit 1";
}

$sth = $dbh->prepare(qq[select id, content, music, bcolor, tcolor, vcolor from mprsee $pickb;]); $sth->execute();

my @permidl = qw(a b c d);
my @permid;

my ($mid, $content, $music, $mbcolor, $mtcolor, $mvcolor) = $sth->fetchrow_array;
$mid = int($mid); 
my @bcolor = unpack "CCC", pack "H6", $mbcolor;
my @vcolor = unpack "CCC", pack "H6", $mvcolor;
push(@permid, $mid);

$sth->finish;

my $pickt;
$pickt = join(" or ", map { int($_) ? ("id=" . int($_)) : () } ($query->param('b'), $query->param('c'), $query->param('d'))); 
$pickt = "where $pickt" if ($pickt);
$pickt = "order by rand() limit 3" unless ($pickt);

$sth = $dbh->prepare(qq[select id, content, minsize from mpread $pickt;]); $sth->execute();
my $readers;
my $blocks; 

for my $i (1..4) {
my ($id, $content, $mins, $size, $xsize, $hsize, $psize, $psize2, @colorl);
	$psize = 10 + int(rand(80)); my $psize2 = 10 + int(rand(80)); 
my ($lrsize, $udsize) = (10,20);
my @result = $sth->fetchrow_array; 
if (@result) { 
	($id, $content, $mins) = @result;
	$readers .= " or (mpruser.id=mprlink.user and mprlink.thing=$id and mprlink.isread=1)"; 
	$size = $mins?$mins:150+int(rand(100))*2; $hsize=$size/2+20; 
	$xsize = 20; 

	push(@permid, $id);
} else {
	$size = 60; $hsize=30; $xsize = 0; 
	($lrsize,$udsize) = (0,0);
}
if (@result || 4 >= $i) { 
	for(0..2) { 
		$colorl[$_] = $bcolor[$_] + rand($vcolor[$_]) * (rand()>0.5?-1:1);
		$colorl[$_] = 255 if ($colorl[$_] > 255); 
		$colorl[$_] = 0 if ($colorl[$_] < 0);
	}
	my $color = sprintf("%02x%02x%02x", @colorl);

	my $sum = sprintf("%8X",crc32($content));
	$sum =~ tr/A-F/a-f/;

	$content =~ s/$/<br>/mg;
	$content =~ s/  /\&nbsp;\&nbsp;/sg;

	$blocks .= <<EOF;
<div style="background-color:#${color}; width:${size}px; left:${psize}%; top:${psize2}%; margin-left:-${hsize}px; padding-left:${lrsize}px; padding-right:${lrsize}px; padding-top:${udsize}px; padding-bottom:${udsize}px; position:absolute; color:#${mtcolor}; font-family:monospace; -moz-user-select:None">
EOF
	if (4 > $i) {
	$blocks .= <<EOF;
<b>File ${sum}\\\\\\\\ 
<a style="position: absolute; right:$xsize; color:#$mtcolor;" OnClick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);" title="CLOSE" href="javascript:void();">X</a>
</b>
<br><br>
$content
<br><br>
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
</div>
EOF
} else {
	my $z = int(rand(1000000000));
	$blocks .= <<EOF;
<a href="http://themostpowerfulrobotever.com/?z=$z"><img alt="reload" border=0 src="reload.gif"></a>
</div>
EOF
} 
}
}

$sth->finish;

my $contrib;

$sth = $dbh->prepare(qq[select distinct mpruser.name,mpruser.url from (mprlink,mpruser) where (mpruser.id=mprlink.user and mprlink.thing=$mid and mprlink.isread=0) $readers;]); $sth->execute();

while (my @result = $sth->fetchrow_array) {
	my ($name, $url) = @result;
	$contrib .= "$name\n        $url\n";
}

my $link;

for (0..(@permid-1)) {
	$permid[$_] = $permidl[$_] . '=' . $permid[$_];
}
$link = "http://themostpowerfulrobotever.com/?" . join("&",@permid);

print <<EOF;
Content-type:text/html

<html>
<!--

November 30 2006, 2:43 AM

Permalink:
$link

Programmers:

$contrib 
Architecture by awk

-->
<head>
<title>THE MOST POWERFUL ROBOT EVER</title> 
</head>

$content

$blocks

$music 

</body>
</html>
EOF
