#!/usr/local/bin/perl
require "/home/roger/lib/weblib.lib";
@path_info=split(m#/#,substr($ENV{PATH_INFO},1));
foreach $param(@path_info){
	$param="\L$param";
	($param,$value)=split(/=/,$param,2);
	$param{$param}=$value?$value:"-1";
}
$lang=$param{language} if $param{language} ne "-1";
&ReadParse(*in,*infcn,*inct);
$fanid=getcookie("FanID");
$idincookie=$fanid;
$fanid=$param{"fanid"} if $param{"fanid"}=~/^\d{7}-\d+$/;
$fanid=$in{FanID} if $in{FanID}=~/^\d{7}-\d+$/;
$fanidparam="/fanid=$fanid" unless $idincookie;
($fid,$cs)=split(/-/,$fanid);
if(&checksum($fid) != $cs){$fanid='';}
delete $in{FanID};
dbmopen(%FANDIR,"/home/todd/db/fandir",undef);
if($FANDIR{"$fanid:Admin"}){
	opendir(DIR,"/home/todd/db");
	@languages=grep s/^fandb-(.+)\.db$/$1/,readdir(DIR);
	closedir(DIR);
}
else{
	@languages=split(/,/,$FANDIR{"$fanid:Translator"});
}
%languages=map {$_,$_} @languages;
if(@languages==1){$lang=$languages[0];$param{language}=$lang;}
unless(@languages){
	&htmlopen("The International Todd Rundgren Fan Data Base");
print <<EOF;
You must be registered in the fan data base as a translator to do a
translation<p>
<a href=/todd/fandb>Register in the Database</a><br>
<form action=$weblib::URL method=post>
or Enter Fan ID <input type=text name=FanID value="$fanid">
</form>
EOF
	&htmlclose;
	exit;
}
&htmlopen("Fan Database Translations");
print <<EOF;
Thank you for volunteering to do an International Todd Rundgren Fan Database
web page translation. Below you will find the various labels, phrases and other
text that needs to be translated. If the language you are translating for can
be rendered using standard ASCII script, then just type the translation into
the text field. Otherwise, you should create a graphic that represents the
text in its native type face (e.g. Japanese characters, Russian Cyrillic) then
upload the individual files by using the file upload feature available with
modern browsers.
<p>
<b>Warning! This form is known to fail miserably on Internet Explorer 4.5 for the Macintosh
and may also fail for other versions of Internet Explorer as well. Please use
Netscape until I come up with a workaround for IE. Thanks!</b>
EOF
unless($lang){
	print <<EOF;
<p>
You are authorized to perform the following translations:
<ul>
EOF
	foreach $language(sort @languages){
		print "<li><a href=$weblib::URL/language=$language$fanidparam>\u$language</a>\n";
	}
	print "</ul>\n";
	&htmlclose;
	exit;
}
print <<EOF;
<P>
Text in red indicates that a translation is not yet on file for the English
text.
<P>
<a href=/todd/fandb/$lang>Go to the Database</a><P>
EOF
dbmopen(%LANG,"/home/todd/db/fandb-english",0644);
if($lang eq "english"){
	foreach $key(keys %in){
		next if $key=~/^TextNEW\d+$/;
		if($key=~/^Delete%%(.+)/){
			delete $LANG{$1};
			delete($in{$1});
		}
		elsif($key=~/^NEW\d+$/){
			$LANG{$in{$key}}=$in{"Text$key"} if $in{"Text$key"};
		}
		else{
			$LANG{$key}=$in{$key} if $in{$key};
		}
	}
	print <<EOF;
<form action=$weblib::URL/language=$param{language} enctype="multipart/form-data" method=post>
<input type=hidden name=FanID value="$fanid">
<input type=submit value="Submit Update">
<table border>
EOF
	foreach $key(sort keys %LANG){
		next if $key=~/%%/;
		print "<tr><td>$key<br>\n";
		print "<input type=checkbox name=\"Delete%%$key\">Delete</td>\n";
		print "<td><textarea name=$key rows=10 cols=60 wrap=soft>$LANG{$key}".
			"</textarea></td>\n";
		print "</tr>\n";
	}
	foreach (1..5){
		print "<tr><th><input name=NEW$_><br>New Field Name</th>\n";
		print "<td><textarea name=TextNEW$_ rows=10 cols=60 wrap=soft>".
			"</textarea></td>\n";
		print "</tr>\n";
	}
	print "</table>\n";
	print "<br><input type=submit value=\"Submit Update\">\n";
	print "</form>\n";
}
else{
%ENG=%LANG;
dbmclose(%LANG);
dbmopen(%LANG,"/home/todd/db/fandb-$lang",0644);
foreach $key(keys %in){
	if($in{$key}){
		if($key=~s/FILE%%//){
			$in{$key}=$in{"FILE%%$key"};
			&graphic($key);
			$LANG{$key}="<img src=/~todd/fandb/$lang/$key.gif>";
		}
		else{
			$LANG{$key}=$in{$key};
		}
	}
}
$language=$LANG{"%%Language"};
$translator=$LANG{"%%Translator"};
print <<EOF;
<form action=$weblib::URL/language=$param{language} enctype="multipart/form-data" method=post>
<input type=hidden name=FanID value="$fanid">
<input type=submit value="Submit Translation">
<table border>
<tr><th>English Text</th>
<th>\u$param{language} Translation<br>
"\u$param{language}" in \u$param{language}:
<input name="%%Language" value="$language"><br>
Translator: <input name="%%Translator" value="$translator">
</th></tr>
EOF
foreach $key(sort keys %ENG){
	next if $key=~/^%%/;
	$english=$ENG{$key};
	$english="<font color=red>$english</font>" unless $LANG{$key};
	print "<tr>";
	print "<td>$english</td>\n";
	print "<td>";
	if($LANG{$key} =~ /^<img/){print $LANG{$key};}
	else{
	print "<textarea name=\"$key\" rows=10 cols=60 wrap=soft>$LANG{$key}</textarea>\n";
	}
	print "<br>or graphic with text: <input type=file name=\"FILE%%$key\">\n";
	print "</td></tr>\n";
}
print "</table>\n";
print "<input type=submit value=\"Submit Translation\">\n";
print	"</form>\n";
}
&htmlclose;
sub checksum{
local(@digits);
local($sum,$power,$digit);
@digits=split('',shift);
foreach $digit(@digits){
	$power++;
	$sum+=$digit*2**$power+$digit;
}
return (($sum+62)%100);
}
sub graphic{
	local($key)=shift;
	$dir="/home/todd/www/fandb/$lang";
	if(!-d $dir){mkdir $dir,0666;chmod 0666,$dir;}
	chdir $dir;
	open(FILE,">$key");
	print FILE $in{$key};
	close(FILE);
	chop($identity=`/usr/bin/identify $key`);
	($name,$geometry,$class,$size,$type)=split(/\s+/,$identity);
	if($type eq "GIF"){
		rename($key,"$key.gif");
	}
	else{
		system "/usr/bin/convert $key gif:$key.gif";
		unlink($key);
	}
}
