#!/usr/bin/perl

# Dictionary of the Gargish language
#
# This script has several options to display the dictionary database
#
# no args displays the whole database in three colums,
#   Gargish script, Gargish in english alphabet, English.
#
# ?english=word displays all matches of <word> in the 
#   database.
#
# ?gargish=word displays all matches of <word> in the
#   database. This tolerates several ways to write gargish words, 
#   extends x, q, w to their counterparts, ignores spaces between
#   gargish syllables and of course accepts kl, ch, zh etc.
#
# ?add&gargish=gword&english=eword adds a suggested phrase
#   to the suggestion file.
#
#
#
# special syllables have extra characters
# 9 == gl
# 5 == sh
# G == ng
# Z == zh
# C == ch
# K == kl

$dictfile = 'gargdict.txt';
%extrachars = (
	'9',	'<i>gl</i>',
	'5',	'<i>sh</i>',
	'G',	'<i>ng</i>',
	'Z',	'<i>zh</i>',
	'C',	'<i>ch</i>',
	'K',	'<i>kl</i>',
);
%gifs = (
'a','<IMG SRC="a.gif" HEIGHT=26 WIDTH=17>',
'b','<IMG SRC="b.gif" HEIGHT=26 WIDTH=17>',
'c','<IMG SRC="b.gif" HEIGHT=26 WIDTH=17>',
'C','<IMG SRC="ch.gif" HEIGHT=26 WIDTH=16>',
'd','<IMG SRC="d.gif" HEIGHT=26 WIDTH=17>',
'e','<IMG SRC="e.gif" HEIGHT=26 WIDTH=17>',
'f','<IMG SRC="f.gif" HEIGHT=26 WIDTH=17>',
'g','<IMG SRC="g.gif" HEIGHT=26 WIDTH=17>',
'9','<IMG SRC="gl.gif" HEIGHT=26 WIDTH=17>',
'h','<IMG SRC="h.gif" HEIGHT=26 WIDTH=17>',
'H','<IMG SRC="hl.gif" HEIGHT=26 WIDTH=17>',
'i','<IMG SRC="i.gif" HEIGHT=26 WIDTH=15>',
'j','<IMG SRC="j.gif" HEIGHT=26 WIDTH=16>',
'k','<IMG SRC="k.gif" HEIGHT=26 WIDTH=17>',
'K','<IMG SRC="kl.gif" HEIGHT=26 WIDTH=17>',
'l','<IMG SRC="l.gif" HEIGHT=26 WIDTH=17>',
'm','<IMG SRC="m.gif" HEIGHT=26 WIDTH=17>',
'n','<IMG SRC="n.gif" HEIGHT=26 WIDTH=17>',
'G','<IMG SRC="ng.gif" HEIGHT=26 WIDTH=17>',
'N','<IMG SRC="nl.gif" HEIGHT=26 WIDTH=17>',
'Y','<IMG SRC="ny.gif" HEIGHT=26 WIDTH=16>',
'o','<IMG SRC="o.gif" HEIGHT=26 WIDTH=17>',
'p','<IMG SRC="p.gif" HEIGHT=26 WIDTH=17>',
'r','<IMG SRC="r.gif" HEIGHT=26 WIDTH=17>',
's','<IMG SRC="s.gif" HEIGHT=26 WIDTH=17>',
'5','<IMG SRC="sh.gif" HEIGHT=26 WIDTH=16>',
't','<IMG SRC="t.gif" HEIGHT=26 WIDTH=17>',
'u','<IMG SRC="u.gif" HEIGHT=26 WIDTH=17>',
'v','<IMG SRC="v.gif" HEIGHT=26 WIDTH=17>',
'z','<IMG SRC="z.gif" HEIGHT=26 WIDTH=17>',
'Z','<IMG SRC="zh.gif" HEIGHT=26 WIDTH=16>',
':','<IMG SRC="dpunkt.gif" HEIGHT=26 WIDTH=13>',
'/','<IMG SRC="minus.gif" HEIGHT=26 WIDTH=15>',
'.','<IMG SRC="punkt.gif" HEIGHT=26 WIDTH=14>',
'-','<IMG SRC="space.gif" HEIGHT=26 WIDTH=10>',
' ','<IMG SRC="space.gif" HEIGHT=26 WIDTH=10>',
);

$query_string = $ENV{'QUERY_STRING'};
$path_info = $ENV{'PATH_INFO'};
$script_name = $ENV{'SCRIPT_NAME'};
$hostname = $ENV{'HTTP_HOST'};
&parse_form_data(*OPTIONS);

print "Content-type: text/html","\n\n";

print "<HTML>\n";



print "<HEAD><TITLE>Gargish Dictionary</TITLE></HEAD>\n";

print qq|<BODY BGCOLOR="#ffffff">\n|;

if (!$OPTIONS{'noscript'}) {
	&printjavascript();
}

print "<H1>",&makerunes("gar9-liG-kodeks"),"</H1>\n";
print "<H1>Gargish Dictionary</H1>\n";



# load dictionary from file
if (open(DICT, "<" . $dictfile)) {
	print "<table>\n";
	print qq|<tr><td>|,&makerunes("gar9-liG"),qq|<td><b>gar<i>gl</i>-li<i>ng</i></b><td><b>English</b>\n|;
	while (<DICT>) {
		next if /^#/;
		($gargish, $english) = split(/\t/,$_);
		$runes = &makerunes($gargish);
		$gargish =~ s/'([auieo])/\&$1acute;/g;
		$gargish =~ s/([95GZCK])/$extrachars{$1}/g;
		$english =~ s/;/; /g;
		print "<tr><td>$runes<td>$gargish<td>$english\n";
	}
	print "</table>\n";
	close(DICT);
}
#print "</body></html>\n";
&print_footer();

exit(0);

#------------------------------------------------------------------

sub makerunes
{
	($garg) = @_;
	local($rune, $c, $ex);
	$rune = '';
	if ($OPTIONS{'noscript'}) {
		while ($garg) {
			$c = chop($garg);
			next if $c =~/'/;
			$ex = '???' unless $ex = $gifs{$c};
			$rune = qq|$ex$rune|;
		}
	}
	else {
		$rune = qq|<SCRIPT language="JavaScript">\nprintgarg("$garg");\n</SCRIPT>|;
	}
	return $rune;
}

sub printjavascript
{
	print <<End_of_Script;
<SCRIPT language="JavaScript">
function printgarg(text)
{
	var len = text.length;
	var inword = 0;
	for (i = 0; i < len; ++i) {
		c = text.substring(i, i+1);
		width = 17;
		switch (c) {
			case "9": c = "gl"; break;
			case "C": c = "ch"; width = 16; break;
			case "H": c = "hl"; break;
			case "i": width = 15; break;
			case "K": c = "kl"; break;
			case "G": c = "ng"; break;
			case "N": c = "nl"; break;
			case "Y": c = "ny"; width = 16; break;
			case "5": c = "sh"; width = 16; break;
			case "Z": c = "zh"; width = 16; break;
			case ":": c = "dpunkt"; width = 13; break;
			case "/": c = "minus"; width = 15; break;
			case ".": c = "punkt"; width = 14; break;
			case "-": 
			case " ": c = "space"; width = 10; break;
		}
		if (c == "space") {
			if (inword) {
				document.write("</nobr>\\n");
				inword = 0;
			}
		}
		else if (c == "\\'") {
			continue;
		}
		else {
			if (!inword) {
				document.write("<nobr>");
				inword = 1;
			}
		}
		document.write("<IMG SRC=\\""+c+".gif\\" HEIGHT=26 WIDTH="+width+">");
	}
}
</SCRIPT>
<NOSCRIPT>
<p>Your browser must support JavaScript to see the page properly!</p>
</NOSCRIPT>
End_of_Script
}

#------------------------------------------------------------------

sub print_footer
{
	print "<hr>\n";
	@crtime = localtime;
	$ctime = &mydtime(@crtime);
	print "Automatically generated ", $ctime,
			qq| <a href="mailto:Martin\@Brenner.de">Martin\@Brenner.de</a><p>\n|;
	@starr = stat($dictfile);
	@filetime = localtime($starr[9]);
	$ctime = &mydtime(@filetime);
	print "Dictionary last updated ", $ctime, ".</p>\n";
	print "</BODY></HTML>\n";
}

#------------------------------------------------------------------
# myctime() return ctime() style date string from date/time array
sub myctime
{
	($mysec,$mymin,$myhour,$mymday,$mymon,$myyear, $mywday) = @_;
	return sprintf(
		('Sun','Mon','Tue','Wed','Thu','Fri','Sat','Sun')[$mywday]." ".
		('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep',
		'Oct','Nov','Dec')[$mymon]." %d %02d:%02d:%02d 19%02d",
		$mymday, $myhour, $mymin, $mysec, $myyear);
}

sub mydtime
{
	($mysec,$mymin,$myhour,$mymday,$mymon,$myyear, $mywday) = @_;
	return sprintf("%02d-".
		('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep',
		'Oct','Nov','Dec')[$mymon]."-%02d %02d:%02d:%02d",
		$mymday, $myyear, $myhour, $mymin, $mysec);
}


#------------------------------------------------------------------

sub parse_form_data
{
	local (*FORM_DATA) = @_;
	local ($request_method, $query_string, @key_value_pairs);
	local ($key_value, $key, $value);
	$request_method = $ENV{'REQUEST_METHOD'};
	if ($request_method eq "GET") {
		$query_string = $ENV{'QUERY_STRING'};
	} elsif ($request_method eq "POST") {
		read(STDIN, $query_string, $ENV{'CONTENT_LENGTH'});
	} else {
		&return_error(500, $error, "Unknown method used.");
	}
	@key_value_pairs = split(/&/, $query_string);
	foreach $key_value (@key_value_pairs) {
		($key, $value) = split(/=/, $key_value);
		$value =~ tr/+/ /;
		$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex($1))/eg;
		if (defined($FORM_DATA{$key})) {
			$FORM_DATA{$key} = join("\0", $FORM_DATA{$key}, $value);
		} else {
			$FORM_DATA{$key} = $value;
		}
	}
}

sub return_error
{
	local ($status, $keyword, $message) = @_;
	print "Content-type: text/html\n";
	print "Status: ", $status, " ", $keyword, "\n\n";
	print <<End_of_Error;

<HTML><HEAD>
<TITLE>CGI programm unexpected error</TITLE>
</HEAD>
<BODY>
<H1>$keyword</H1>
<hr>$message</hr>
Further information from $webmaster.
</BODY></HTML>
End_of_Error

	exit(1);
}


