#!/usr/local/bin/perl -s


#   note from UB :  use this with perl4 only !!!!!
#  

$Usage =<<EOM;
wbi

USAGE

	wbi [-if] inputFile inDict outFile outDict [language]

SYNOPSIS

	Translates input file into output file using
	dictionary file. Also outputs new dictionary file.

	If the -if switch is defined, the input file is
	old style two language file. In this case the inDict 
	is not used.

AUTHOR

	Ziga Turk, ziga.turkfagg.uni-lj.si
EOM

($inFile,$inDict,$outFile,$outDict,$ll) = @ARGV;

# parameters

$inFile  = $inFile  || "wbuk1.pl";
$inDict  = $inDict  || "uk2si.dct";
$outFile = $outFile || "wbsi1.pl";
$outDict = $outDict || "uk2si.dct";
$ll      = $ll || "LL";

# defaults

$LLAB ="#UK\n";
$RS = '=' x 10  . "\n";
$FS = '-' x 10  . "\n";
$WARN = "N O T  T R A N S L A T E D";

if ($if) {
    do getLines($inFile);
    do extractIFIF($outFile);
    do printDict($outDict);
} else {
    do getLines($inFile);
    do getDict($inDict);
    do translateLang($outFile);
    do printDict($outDict);
}

print "Done $outFile\n\n";

sub keyIt {

	local ($x) = $_[0];
	$* = 1;
	$x =~ s/\s//g;
	$* = 0;
	return $x;
}

sub getLines {

	local ($file) = @_;

	open (h,$file) || die "Cannot open $pl file\n";
	@lines = <h>;
	close (h);
	$i=$#lines;
	print "Read $i lines from $file.\n";
}

# extracts translations from 'if $SI' 'if $UK'
sub extractIFIF {

    open (h,">$_[0]");

    $first = 0;
    for ($i=0;$i<=$#lines;$i++) {
	$_=$lines[$i];
	$pr = '';
	if (m/(.*)if \$UK;\s*$/) {
	    if (m/\<\<EOM/) {
		$i++;
		$x='';
		while (! ($lines[$i] =~ m/^EOM/)) {
		    $x .= $lines[$i++];
		}
		$pr = "$1;$LLAB" . $x . "EOM\n";
	    } else {
		$x = "$1;\n";
		$pr = "$1;$LLAB";
	    }
	    push (@uk,$x);
	} elsif	(m/(.*)if \$SI;\s*$/) {
	    if (m/\<\<EOM/) {
		$i++;
		$x='';
		while (! ($lines[$i] =~ m/^EOM/)) {
		    $x .= $lines[$i++];
		}
	    } else {
		$x = "$1;\n";
	    }
	    push (@si,$x);
	} else {
	    $pr = $_;
	}
	print h $pr;
    }

    close h;
    print "Wrote $_[0].\n";
}

# translates #UK into the other language
# known @lines and %trans
# prints translation on stdout and missing translations in out

sub translateLang {

    open (h,">$_[0]");

    $notall = 0;

    $first = 0;
    for ($i=0;$i<=$#lines;$i++) {
	$_=$lines[$i];
	$original = '';
	$print='';
	if (m/^(.*)$LLAB\s*$/) {
	    if (m/<<EOM/) {
		$print=$1;
		$i++;
		while (! ($lines[$i] =~ m/^EOM\n$/)) {
		    $original .= $lines[$i++];
		}
	    } else {
		$print='';
		$original = "$1\n";
    	    }
	    $key = &keyIt($original);

	    if ($translation = $trans{$key}) {	# translation exists
		$comment = $ll;
		$trall++;
                push (@uk,$original);
                push (@si,$translation);
	    } else {
		$comment = $WARN;
		$notall++;
		$translation = $original;
                unshift (@uk,$original);
                unshift (@si,"$WARN:\n$original");
	    }

	    if ($print ne '') {	# eom
		$print .= " #$comment\n" . $translation . "EOM\n";
	    } else {
		$print = $translation;
		$print =~ s/\n//;
		$print .= "#$comment\n";
	    }

	    print h $print;

	} else {

	    print h $_;

	}
    } # each line

    print "$trall strings translated, $notall strings not translated into $_[0].\n";

    if ($notall) {
	print "TRANSLATION FAILED\n";
    }
}

# prints language file
sub printDict {

    open (x,">$_[0]");
    $i=0;
    foreach $uk (@uk) {
	print x $RS;
	print x $uk[$i];
	print x $FS;
	print x $si[$i];
	$i++;
    }

    close (x);
    $i = $#uk;
    print "$i strings into $_[0]\n";
}

# reads language file
sub getDict {

	open (h,$_[0]);
	@rows = <h>;
	$in = 'uk';
	$uk = $si = '';
	foreach $row (@rows) {
	    if ($row eq $RS) {
		$key = &keyIt($uk);
		if ($si =~ m/^$WARN/) {
		    $notrans++;
		} else {
		    $trans{$key}=$si;
		}
	        $uk=$si='';
		$in = 'uk';
	    } elsif ($row eq $FS) {
		$in = 'si';
	    } else {
		$uk .= $row if $in eq 'uk';
		$si .= $row if $in eq 'si';
	    }
	}

	if ( $uk && $si ) {
	    $key = &keyIt($uk);
	    $trans{$key}=$si;
	}

	$key = &keyIt($uk) if $uk && $si;

	close (h);

	@x = keys(%trans);
	$i = $#x + 1;
	print "Read $i translations from $_[0]; $notrans were bad.\n";
}	

