#!/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 classifier image generation");
print "<h2>Greenstone classifier image generation</h2>";
print hr();

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

# Macro name (ASCII characters only)
my $macro_name = param("macro");
$macro_name =~ tr/a-zA-Z0-9//cd;

# Image filename (ASCII characters only)
my $image_name = param("image");
$image_name =~ tr/a-zA-Z0-9//cd;

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

    my $language_modifier = "";
    my $language_modifier_one = "";
    my $image_directory = "";
    my $translate_this = "";
    unless ($language_code eq "en") {
	$language_modifier = "[l=$language_code]";
	$language_modifier_one = "l=$language_code,";
	$image_directory = "$language_code/";
	$translate_this = "# <b>Translate this!</b>";
    }

    # Write a temporary macrofile containing the text for the images
    $text_unicode_raw = $text_unicode;  # May contain "\n" to split the green_title text
    $text_unicode =~ s/\\n/ /g;  # Replace any "\n" with " " for standard use
    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 . "\" \#\# nav_bar_button \#\# t$image_name \#\#\n\n";
    print MACROFILE "\#\# \"" . $text_unicode_raw . "\" \#\# green_title \#\# h_$image_name \#\#\n\n";
    close(MACROFILE);

    # Use ImageMagick via image generation scripts to make the images
    `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.*`;

    # Get width of nav_bar_buttons
    my $nav_bar_button_width = 87;
    my $nav_bar_button_identification = `identify $images_directory/t${image_name}on.gif 2>&1`;
    if ($nav_bar_button_identification =~ /t${image_name}on.gif \w+ (\d+)x(\d+)/) {
	$nav_bar_button_width = $1;
    }

    # Get width of green_titles
    my $green_title_width = 200;
    my $green_title_identification = `identify $images_directory/h_$image_name.gif 2>&1`;
    if ($green_title_identification =~ /h_$image_name.gif \w+ (\d+)x(\d+)/) {
	$green_title_width = $1;
    }

    # Output result HTML
    print "<p>Greenstone classifier images with text \"" . $text_unicode_raw . "\":";
    print "<p><a href=\"$images_directory_url/t${image_name}on.gif\"><img src=\"$images_directory_url/t${image_name}on.gif\" border=\"0\" width=\"$nav_bar_button_width\" height=\"17\"></a>";
    print "<p><a href=\"$images_directory_url/t${image_name}of.gif\"><img src=\"$images_directory_url/t${image_name}of.gif\" border=\"0\" width=\"$nav_bar_button_width\" height=\"17\"></a>";
    print "<p><a href=\"$images_directory_url/t${image_name}gr.gif\"><img src=\"$images_directory_url/t${image_name}gr.gif\" border=\"0\" width=\"$nav_bar_button_width\" height=\"17\"></a>";
    print "<p><a href=\"$images_directory_url/h_$image_name.gif\" style=\"color: #000000\"><img src=\"$images_directory_url/h_$image_name.gif\" border=\"1\" width=\"$green_title_width\" height=\"57\"></a>";
    print "<p>\n";
    print "These images should be put in the images/$image_directory folder of your Greenstone installation.\n";
    print "<p>\n";
    print "To get Greenstone to use the new images, some macros must be defined. Copy all the following text and paste it into the extra.dm file in Greenstone's \"macros\" directory:\n";
    print "<p>\n";
    print "<tt>\n";

    # base.dm macros
    print "# <i>The following macros usually live in base.dm</i>\n";
    print "<br>package Global\n";
    print "<br>\n";
    print "<br>_" . $macro_name . "width_ $language_modifier {_widtht" . $image_name . "x_ }\n";
    print "<br>_image" . $macro_name . "_ $language_modifier {_gsimage_(_httpbrowse" . $macro_name . "_,_httpicont" . $image_name . "of_,_httpicont" . $image_name . "on_,$text_unicode,_textimage" . $macro_name . "_)}\n";
    print "<br>_icontab" . $macro_name . "green_ $language_modifier {&lt;img src=\"_httpicont" . $image_name . "gr_\" width=_widtht" . $image_name . "x_ border=0&gt;}\n";
    print "<br>_icontab" . $macro_name . "green_ [$language_modifier_one" . "v=1] {_texticontab" . $macro_name . "green_ }\n";
    print "<p>\n";

    # document.dm macros
    print "# <i>The following macros usually live in document.dm</i>\n";
    print "<br>package document\n";
    print "<br>\n";
    print "<br>_text" . $macro_name . "page_ $language_modifier {_texticonh" . $image_name . "_}\n";
    print "<br>_icon" . $macro_name . "page_ $language_modifier {&lt;img src=\"_httpiconh" . $image_name . "_\" width=\"_widthh" . $image_name . "_\" height=\"_heighth" . $image_name . "_\"&gt;}\n";
    print "<br>_icon" . $macro_name . "page_ [$language_modifier_one" . "v=1] {&lt;h2&gt;_texticonh" . $image_name . "_&lt;/h2&gt;}\n";
    print "<p>\n";

    # <language>.dm macros
    print "# <i>The following macros usually live in $macrofile_name.dm</i>\n";
    print "<br>package Global\n";
    print "<br>\n";
    print "<br>_textimage" . $macro_name . "_ $language_modifier {Browse by " . $text_unicode . "} $translate_this\n";
    print "<br>_texticontab" . $macro_name . "green_ $language_modifier {\u$text_unicode}\n";
    print "<br>\n";
    print "<br>## \"$text_unicode\" ## nav_bar_button ## t" . $image_name . " ##\n";
    print "<br>_httpicont" . $image_name . "gr_ $language_modifier {_httpimg_/$image_directory" . "t" . $image_name . "gr.gif}\n";
    print "<br>_httpicont" . $image_name . "of_ $language_modifier {_httpimg_/$image_directory" . "t" . $image_name . "of.gif}\n";
    print "<br>_httpicont" . $image_name . "on_ $language_modifier {_httpimg_/$image_directory" . "t" . $image_name . "on.gif}\n";
    print "<br>_widtht" . $image_name . "x_ $language_modifier {$nav_bar_button_width}\n";

    print "<br>\n";
    print "<br>package document\n";
    print "<br>\n";
    print "<br>_texticonh" . $image_name . "_ $language_modifier {\u$text_unicode}\n";
    print "<br>\n";
    print "<br>## \"$text_unicode\" ## green_title ## h_" . $image_name . " ##\n";
    print "<br>_httpiconh" . $image_name . "_ $language_modifier {_httpimg_/$image_directory" . "h_" . $image_name . ".gif}\n";
    print "<br>_widthh" . $image_name . "_ $language_modifier {$green_title_width}\n";
    print "<br>_heighth" . $image_name . "_ $language_modifier {57}\n";

    print "<br>\n";
    print "<br>package help\n";
    print "<br>\n";
    print "<br>_text" . $macro_name . "short_ $language_modifier {access publications by $text_unicode} $translate_this\n";
    print "<br>_text" . $macro_name . "long_ $language_modifier {&lt;p&gt;You can &lt;i&gt;access publications by $text_unicode&lt;/i&gt; by pressing the &lt;i&gt;" . $text_unicode . "&lt;/i&gt; button. This brings up a list of " . $text_unicode . ". } $translate_this\n";
    print "<br>\n";

    print "</tt>\n";
}
else {
    print "<p>Error: parameter not defined.";
}

print end_html();
