#!/usr/bin/perl -w

sub header {
    $handle = shift;
    $title = shift;
    print($handle "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n");
    print($handle "<html>\n  <head>\n    <meta http-equiv=\"content-type\" content=\"charset=iso-8859-2\" />\n    <title>$title</title>\n  </head>\n<body style=\"background-color:#69FFBE\">\n");
}

sub footer {
    $handle = shift;
    print($handle "<hr />\n<p>\n<a href=\"http://validator.w3.org/check?uri=referer\"><img style=\"border:none\" src=\"images/valid-xhtml11.png\" alt=\"Valid XHTML 1.1\" /></a>\n</p>\n");
    print($handle "</body>\n</html>\n");
}

@lines = glob("images/*.png");
@lines = grep(!/valid/, @lines);
open(DESCRIPTIONS, "opisy.txt") or die "Nie udalo sie otworzyc pliku!";
@descr = <DESCRIPTIONS>;
close(DESCRIPTIONS);
$i = 0;
$maxlines = @lines;
for ($i; $i < $maxlines; $i++) {
    $lines[$i] =~ /^images\/([^-]*)-(.*).png$/;
    $number = $1;
    $name = $2;
    if ($i != $maxlines) {
	$lines[$i + 1] =~ /^images\/([^-]*)-(.*).png$/;
	$nextname = "$2.html";
    }
    if ($i != 0) {
	$lines[$i - 1] =~ /^images\/([^-]*)-(.*).png$/;
	$prevname = "$2.html";
    }
    open(HTML, ">$name.html");
    &header(HTML, $name);
    @descrline = grep(/$number-$name/, @descr);
    $desc = $descrline[0];
    $desc =~ /^$number-$name.png:(.*)$/;
    $desc = $1;
    if ($i != ($maxlines - 1)) {
	print(HTML "<p>\n<a href=\"$nextname\">Następny</a><br />\n");
    }
    if ($i != 0) {
	print(HTML "<a href=\"$prevname\">Poprzedni</a><br />\n");
    }
    print(HTML "<a href=\"index.html\">Początek</a>\n</p>\n");
    print(HTML "<p>\n$opis\n</p>\n");
    print(HTML "<hr />");
    print(HTML "<p>\n<img style=\"display:block;margin-left:auto;margin-right:auto\" src=\"$lines[$i]\" alt=\"$name\" />\n</p>\n");
    &footer(HTML);
    close(HTML);
}
