#!/usr/bin/perl -w

use CGI qw(:standard);

my $image_generation_code_directory = "/research/mdewsnip/greenstone/image-generation";
my $images_directory = "/home/mdewsnip/public_html/greenstone/temp-images";
my $images_directory_url = "http://www.cs.waikato.ac.nz/~mdewsnip/greenstone/temp-images";

print "Content-Type: text/html; charset=utf-8\n\n";
print start_html("Greenstone collection image generation");
print "<h2>Greenstone collection image generation</h2>";
print hr();

# Language code and macrofile name
my $language = param("l") || "en.english";

# Collection name (ASCII characters only)
my $collection_name = param("collection");
$collection_name =~ tr/a-zA-Z0-9//cd;

my $text_unicode = param("text");
if (defined $text_unicode && $text_unicode ne "") {
    my $language_code = (split(/\./, $language))[0];

    my $language_modifier = "";
    my $image_directory = "";
    unless ($language_code eq "en") {
	$language_modifier = "[l=$language_code]";
	$image_directory = "$language_code/";
    }

    # Write a temporary macrofile containing the text for the images
    $text_unicode_raw = $text_unicode;  # May contain "\n" to split the green_title text
    open(MACROFILE, ">$image_generation_code_directory/web-images.dm") || (print "Error: Could not open web-images.dm for writing.", end_html() && die);
    print MACROFILE "\#\# \"" . $text_unicode_raw . "\" \#\# collection_icon \#\# $collection_name \#\#\n\n";
    close(MACROFILE);

    `cd ..; source setup.bash; cd $image_generation_code_directory; make-images.pl -language_symbol $language_code -image_dir $images_directory web-images.dm; rm web-images.*`;

    print "<p>Greenstone collection images with text \"" . $text_unicode_raw . "\":";
    print "<p><a href=\"$images_directory_url/${collection_name}_sm.gif\" style=\"color: #000000\"><img src=\"$images_directory_url/${collection_name}_sm.gif\" width=\"150\" height=\"44\" border=\"1\"></a>";
    print "<p><a href=\"$images_directory_url/$collection_name.gif\" style=\"color: #000000\"><img src=\"$images_directory_url/$collection_name.gif\" width=\"225\" height=\"66\" border=\"1\"></a>";
    print "<p>\n";
    print "These images should be put in the collect/$collection_name/images/$image_directory folder of your Greenstone installation.\n";
    print "<p>\n";
    print "To get Greenstone to use the new images, you need to specify them in your collection configuration. If you're using the GLI, open your collection, switch to the Design -> General view, and use the Browse buttons to select your images. The 'about page' image is $collection_name.gif, and the 'home page' image is ${collection_name}_sm.gif.\n";
    print "<p>\n";
    print "Alternatively, edit your collection's collect.cfg file manually and add:\n";
    print "<p>\n";
    print "<tt>\n";
    print "collectionmeta iconcollectionsmall $language_modifier \"_httpprefix_/collect/$collection_name/images/$image_directory" . "${collection_name}_sm.gif\"\n<br>";
    print "collectionmeta iconcollection $language_modifier \"_httpprefix_/collect/$collection_name/images/$image_directory" . "$collection_name.gif\"\n";
    print "</tt>\n";
}
else {
    print "<p>Error: parameter not defined.";
}

print end_html();
