#!/usr/bin/perl
use Image::Magick;

# first see if we're running as a CGI script
# I'm not sure what the proper way of doing this is but this will work for most cases
$cgi = 0;
$cgi = 1 if exists($ENV{SCRIPT_NAME});

$count = 1;
$start = 1;
$size = 5;
$letter_size = 300;
$gap_size = 25;
if ($cgi) {
    $q_string = $ENV{QUERY_STRING};
    # I should _really_ unescape q_string right here but I'm not 
    # using anything that needs to be escaped... and I'm feeling lazy today.
    @args = split('&', $q_string);
} else {
    @args = @ARGV;
}

foreach $arg (@args) {
    if ($arg=~/^seed\=(\d+)$/i)  { srand(int($1)); }
    # don't produce more than one image if we're being run as a cgi script
    if ($arg=~/^count\=(\d+)$/i) { if (!$cgi) {$count = int($1);} }
    if ($arg=~/^start\=(\d+)$/i) { $start = int($1); }
    if ($arg=~/^size\=(\d+)$/i) {
	$size = $1; if ($size != 5 && $size != 4) { $size = 5; }
    }
    if ($arg=~/^scale\=(\d+)$/i) {
	my $scale = int($1);
	$scale = 4 if $scale < 4;
	$scale = 100 if $scale > 100;
	$letter_size = $scale * 3;
	$gap_size = int($scale / 4);
    }
    if ($arg=~/^letter_size\=(\d+)$/i) {
	$letter_size = int($1);
	$letter_size = 0 if ($letter_size < 0);
	$letter_size = 300 if ($letter_size > 300);
    }
    if ($arg=~/^gap_size\=(\d+)$/i) {
	$gap_size = int($1);
	$gap_size = 0 if ($gap_size < 0);
	$gap_size = 25 if ($gap_size > 25);
    }

}

@die5 = (
"hiprry", "ceipst", "aafirs", "adennn", "ddlonr",
"ooottu", "aaafrs", "ceiilt", "ccnstw", "fiprsy",
"aeegmu", "dhlnor", "gorrvw", "dhhlor", "aaeeee",
"ensssu", "ceilpt", "emottt", "aeeeem", "eiiitt",
"afirsy", "dhhnot", "aegmnn", "nootuw", "bjkqxz"
);

@die4 = (
"eegnaa", "soiset", "abbooj", "ldexir",
"wrethv", "wtoota", "muqhni", "wegnhe",
"tstidy", "verldy", "ienues", "zhrlnn",
"fskfap", "rettyl", "iotmcu", "ahspoc"
);


$board = Image::Magick->new;
$letter = Image::Magick->new;

$side_size = $letter_size * $size + $gap_size * ($size + 1);
$img_size = $side_size . "x" . $side_size;
$board->Set(size=>$img_size);
#$board->Set(size=>"1650x1650") if $size == 5;
#$board->Set(size=>"1325x1325") if $size == 4;
$board->ReadImage('xc:black');


for ($bn = $start; $bn < $start + $count; ++$bn) {
    $diceleft = 25 if $size == 5;
    $diceleft = 16 if $size == 4;
    @used = ();

    for ($y = 0; $y < $size; ++$y) {
	for ($x = 0; $x < $size; ++$x) {
	    $die = int(rand($diceleft));
	    $face = int(rand(6));

	    $cur = -1;
	    for ($a = 0; $a <= $die; ++$a) {
		for (++$cur ; $used[$cur]; ++$cur) { }
	    }
	    $die = $cur;
	    $used[$die] = 1;
	    
	    $char = substr($die5[$die], $face, 1) if $size == 5;
	    $char = substr($die4[$die], $face, 1) if $size == 4;

	    $letter->Read("letters/$char.gif");
	    if ($letter_size != 300) {
		$letter->Scale(width=>$letter_size, height=>$letter_size);
	    }

	    $rot = int(rand(4));
	    $letter->Rotate(degrees=> 90) if $rot == 1;
	    $letter->Rotate(degrees=>180) if $rot == 2;
	    $letter->Rotate(degrees=>270) if $rot == 3;

	    $xpos = $x * ($letter_size + $gap_size) + $gap_size;
	    $ypos = $y * ($letter_size + $gap_size) + $gap_size;
	    $board->Composite(image=>$letter, compose=>'Over', 
			      'x'=>$xpos, 
			      'y'=>$ypos );
	    @$letter = ();
	    --$diceleft;
	}
    }
    if ($cgi) {
	print ("Content-type: image/png\n\n");
	$board->write(file=>\*STDOUT, filename=>"board.png");
    } else {
	print ("writing board $bn\n");
	$board->write("board_$bn.png");
    }

}
@$board = ();
