#!/usr/bin/perl -w 
#
#  Script that translates .sgml files using the .po files generated from
#  the script update_po.pl
#
#  Copyright (C) 2001 H�ctor Garc�a �lvarez.
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This script is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this library; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
#  Authors: H�ctor Garc�a �lvarez <hector@scouts-es.org>

## Loaded modules
use File::Basename;
use Getopt::Long;

my $LANG = $ARGV[0];

my %string;
my $texto_original="";
my $texto_traducido="";

if (! $LANG){
    print "Usage: update_translation.pl LANGCODE\n";
    exit;
}

chdir ("./C");

## Reading the po file
#print "Loading ".$LANG.".po\n";
#&load_translated_strings ($LANG.".po");

## Checking for the lang dir
if ( !(-d "../".$LANG) ) { mkdir ("../".$LANG, 0755) ; }

open FILES, "<POTFILES.in" ;
while (<FILES>) {
	undef %string;
	s/\n//g;
	$Original_file = $_ ;
	s/.\///g;
	$Translated_file = "../".$LANG."/".$_;
#	print $Original_file."\n";
#	print $Translated_file."\n";
	&load_translated_strings ("../".$LANG.".po/".$_.".po");
	print "Translating ".$Original_file ;
	system "rm -f $Translated_file";
	&translate_file ($Translated_file , $Original_file);
	print ".\n";
}
close FILES;							    

exit 0;




sub load_translated_strings () 
{
	my $FILE=$_[0];
	open (IN, "<$FILE") || die "I can't find $FILE";

	while (<IN>) {
	        if ( /#: /) {
			&original;
			&traduccion;
#               print "Original \n##".$texto_original."##\n";
#               print "Traducci�n \n##".$texto_traducido."##\n";
	                $string{$texto_original} = $texto_traducido;
		}
	}
	close (IN);
}

sub translate_file ()
{
	my $OUTFILE=$_[0];
	my $INFILE=$_[1];
	
	open OUT, ">>$OUTFILE";
	open (IN, "<$INFILE") || die "can't open $INFILE: $!";

	while (<IN>) {
		my $imprimir = 0;
		if ( /<!--/ ) {
			$Salida =  $_ ;
			if ( !(/-->/) ) {
				while (<IN>) {
					$Salida .=  $_ ;
					if ( /-->/ ) { last ; }
				}
			}
			$imprimir = 1;
		}
		elsif ( /<para>/ ) {
			my $number_of_para = 1;
			$Salida = $_ ;
			if ( !(/<\/para>/) ) {
				while (<IN>) {
					if ( /<para>/ ) { $number_of_para++; }
					$Salida .=  $_ ;
					if ( /<\/para>/ ) {
						$number_of_para--;
						if ( $number_of_para==0) {last ; }
					}
				}
			}
			$imprimir = 1;
		}
		elsif ( /<title>/ ) {
			$Salida = $_ ;
			if ( !(/<\/title>/) ) {
				while (<IN>) {
					$Salida .=  $_ ;
					if ( /<\/title>/ ) { last ; }
				}
			}
			$imprimir = 1;
		}
		elsif ( /<glossterm>/ ) {
			$Salida = $_ ;
			if ( !(/<\/glossterm>/) ) {
				while (<IN>) {
					$Salida .=  $_ ;
					if ( /<\/glossterm>/ ) { last ; }
				}
			}
			$imprimir = 1;
		}
		elsif ( /<guilabel>/ ) {
			$Salida = $_ ;
			if ( !(/<\/guilabel>/) ) {
				while (<IN>) {
					$Salida .=  $_ ;
					if ( /<\/guilabel>/ ) { last ; }
				}
			}
			$imprimir = 1;
		}
	        if ( $imprimir == 0 ) {  print (OUT $_); }
		else { 
			my $impreso=0;
			foreach my $theMessage (sort keys %string) {
				if (!($theMessage cmp $Salida)) {
					my $tag = $string{$Salida} ;
					
					if ( $tag cmp "") {
						$tag =~ s/\\"/"/mg ;
						print (OUT $tag);
					}
					else {
						print (OUT $Salida);
					}
					$impreso=1;
				}
			}
			if ( $impreso == 0) {
				print "No lo encuentro\n##".$Salida."##\n";
				$impreso=0;
			} 
			$imprimir = 0;
		}
	}
	close IN;
	close OUT;
}
#exit 0;

sub original () 
{
	my $tmp = "";
	while (<IN>) {
		if ( !(/^#: /) ) {
			if ( /msgid ""/) { s/msgid ""\n//; }
			if ( /msgstr/) {
				$tmp =~ s/\\n/\n/sg ;
				$tmp =~ s/\\t/\t/sg ;
				$tmp =~ s/\\"/"/sg ;
				$texto_original = $tmp;
				last ;
			}
			s/msgid "//;
			s/\s*"// ;
			s/"\n// ;
			s/\n// ;
			$tmp .=  $_;
		}
	}
}
		
sub traduccion ()
{
        my $tmp = "";
	my $first = 0;
	if ( /msgstr "/) {
		if ( /msgstr ""/) {
			$tmp = "";
			$first = 1;
		} else {
			$tmp = $_;
			$tmp =~ s/msgstr "//;
			$tmp =~ s/"\n// ;
		}
	}
	while (<IN>) {
	
		if ( !($_ cmp "\n") )  {
			$tmp =~ s/\\n/\n/sg ;
			$tmp =~ s/\\t/\t/sg ;
			$tmp =~ s/\"/"/sg ;
			if ( $first == 1 ) { $texto_traducido = "" ; }
			else { $texto_traducido = $tmp; }
			last ;
		}
		$first = 0;
		s/msgstr "//;
		s/"\n// ;
		s/\s*"// ;
		$tmp .=  $_;	
	}
	if ( eof IN ) {
		$tmp =~ s/\\n/\n/sg ;
		$tmp =~ s/\\t/\t/sg ;
		$tmp =~ s/\"/"/sg ;
		$texto_traducido = $tmp; 
	}
}
</td></tr>
<tr><td class='sub'>FreeBSD GNOME current development ports (https://github.com/freebsd/freebsd-ports-gnome)</td><td class='sub right'></td></tr></table>
<table class='tabs'><tr><td>
<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/about/?h=gstreamer'>about</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/?h=gstreamer'>summary</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/refs/?h=gstreamer&amp;id=db9a6e2020b23ef7df4dc62e4aa8be1c53d0c131'>refs</a><a class='active' href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/mail/smx?h=gstreamer'>log</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/tree/mail/smx?h=gstreamer&amp;id=db9a6e2020b23ef7df4dc62e4aa8be1c53d0c131'>tree</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/mail/smx?h=gstreamer&amp;id=db9a6e2020b23ef7df4dc62e4aa8be1c53d0c131'>commit</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/diff/mail/smx?h=gstreamer&amp;id=db9a6e2020b23ef7df4dc62e4aa8be1c53d0c131'>diff</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/stats/mail/smx?h=gstreamer'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/mail/smx'>
<input type='hidden' name='h' value='gstreamer'/><input type='hidden' name='id' value='db9a6e2020b23ef7df4dc62e4aa8be1c53d0c131'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/?h=gstreamer&amp;id=db9a6e2020b23ef7df4dc62e4aa8be1c53d0c131'>root</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/mail?h=gstreamer&amp;id=db9a6e2020b23ef7df4dc62e4aa8be1c53d0c131'>mail</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/mail/smx?h=gstreamer&amp;id=db9a6e2020b23ef7df4dc62e4aa8be1c53d0c131'>smx</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th></th><th class='left'>Commit message (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/mail/smx?h=gstreamer&amp;id=db9a6e2020b23ef7df4dc62e4aa8be1c53d0c131&amp;showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Age</th><th class='left'>Files</th><th class='left'>Lines</th></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/mail/smx?h=gstreamer&amp;id=acecbb0648e67d5365bd314575bb441a4b6084c6'>- update to USE_BDB</a></td><td>dinoex</td><td><span title='2006-04-24 01:43:41 +0800'>2006-04-24</span></td><td>1</td><td><span class='deletions'>-25</span>/<span class='insertions'>+18</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/mail/smx?h=gstreamer&amp;id=e639821c54d358a7ccad8cf25ad5ed06059ef417'>- improve setup</a></td><td>dinoex</td><td><span title='2005-12-04 01:35:27 +0800'>2005-12-04</span></td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/mail/smx?h=gstreamer&amp;id=f7ba9d82d6312453c5d87821668a30203506314b'>- ignore ia64</a></td><td>dinoex</td><td><span title='2005-11-14 05:27:22 +0800'>2005-11-14</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/mail/smx?h=gstreamer&amp;id=20ae5d21784290841c477193a9c33b1732557dd5'>- add SHA checksum</a></td><td>dinoex</td><td><span title='2005-11-13 17:26:50 +0800'>2005-11-13</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/mail/smx?h=gstreamer&amp;id=7b3914ca12c677470c5272074c3e4e386d010f92'>- fix build with csh</a></td><td>dinoex</td><td><span title='2005-11-08 18:30:52 +0800'>2005-11-08</span></td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>