#!/usr/bin/perl

my $proj = "/data/proj/languages";
my $flags = "$proj/flags";
#my $langs = "$proj/iso-639-1";
#my $langs = "$proj/iso-639-2";
#my $langs = "/data/proj/album/Language/LIST";
my $langs = "$proj/all_languages";

#########################
# Read the language codes
#########################
open(LANGS,"<$langs") || die("Couldn't read $langs");
print "READING: $langs\n";
my $curr;
while (<LANGS>) {
	# iso-639-2
	#$lang{$1} = $2 if /^(...); (.+?)(; |$)/;

	# iso-639-1
	#$lang{$1} = $2 if /^(..)\s+(.+?)(; |$)/;

	# Language/LIST
	$lang{$1} = $2 if /^(..)=(.+)/;

	# all_languages";
	$curr=$1 if /^\[(\S+)\]$/;
	$lang{$curr}=$1 if /^Name\[$curr\]=(.*)/;
}
$lang{en} = "English";
close LANGS;

#########################
# What other languages do we have docs for?
#########################
opendir(DOC,".") || die("Couldn't opendir(.)");
my @lang = grep(!/^\.{1,2}$/ && -d $_ && !-l $_ && $lang{$_}, readdir(DOC));
@lang = grep($_ ne "Pics", @lang);
closedir(DOC);

#########################
# What txt_ files do we need?
#########################
my @txt = <txt_*>;

#########################
# Create the language menu
#########################
sub url {
	my ($from, $to) = @_;
	return undef if $from eq $to;
	my $url = $to eq "en" ? "" : $to;
	$url = "../$url" unless $from eq "en";
	return $url;
}
sub href {
	my ($from, $to) = @_;
	my $url = url($from,$to);
	return $url ? "<a href=\"$url\">$lang{$to}</a>" : $lang{$to};
}
sub flag {
	my ($from, $to) = @_;
	my $p = url($from,$to);
	return $p ? "$p/flag.png" : "flag.png";
}
sub langline {
	my ($from,$to) = @_;
	my $iso639_1 = iso639_1($to);
	my $flag = flag($from,$to);
	system("cp $flags/$to.png $flag ; chmod ugo+r $flag") if -f "$flags/$to.png" && ! -f $flag;
	$flag = -f $flag ? "<img src=\"$flag\">" : "-";
	my $href=href($from,$to);
	my $url=url($from,$to);

	print LANGMENU "<br><span lang='$iso639_1'>&nbsp;&nbsp; $flag &nbsp;&nbsp;$href</span>\n";

	# We should use translations of "album documentation" here:
	my $urlOUT = $url;
	$urlOUT .= '/' unless $url =~ /\/$/;
	$urlOUT .= '$OUT';
	print LANGHTML "\t<link rel='alternate' type='text/html' href='$urlOUT' hreflang='$iso639_1' lang='$iso639_1' title='album documentation'>\n"
	  unless $from eq $to;
	print LANGHTML "\t<meta http-equiv='Content-Language' content='$iso639_1'>\n"
		if $from eq $to;
}

sub iso639_1 {
	my ($iso639_2) = @_;
	# This substr is hokey bullshit.  ISO 639-1 is obsolete, but it's
	# what the web uses!  List: http://www.loc.gov/standards/iso639-2/englangn.html
	substr($iso639_2,0,2);
}

sub langmenu {
	my ($from) = @_;
	open(LANGMENU,">langmenu") || die("Can't write langmenu");
	open(LANGHTML,">langhtml") || die("Can't write langhtml");

	my $iso639_1 = iso639_1($from);
	#print LANGHTML "<html lang='$iso639_1'>\n<head>\n";
	print LANGHTML "<html>\n<head>\n";
	print LANGHTML <<STYLE if $from ne "en";
	<style>
	<!--
	:lang($iso639_1) { color: green; }
	-->
	</style>
STYLE

	foreach my $to ( "en", @lang ) {
		langline($from,$to);
	}
	close LANGMENU;
}

#########################
# Make docs for english
#########################
langmenu("en");
system("make_faq");

#########################
# Make docs for other languages
#########################
foreach my $lang ( @lang ) {
	print "Language: $lang - $lang{$lang}\n";
	chdir $lang;
	foreach my $txt ( @txt ) {
		symlink("../$txt",$txt);
	}
	symlink("../conf","conf");
	langmenu($lang);
	system("make_faq");
	chdir "..";
}

unlink("langmenu");
unlink("langhtml");
