#!/usr/bin/perl -s

#use strict;
#use warnings;

use XML::TMX::Reader;
use XML::DT;

our ($toTrados, $clean, $join, $cat);

# here we must take care that only one of the options is being used.

=head1 NAME

tmx2tmx - utility to convert and filter TMX files

=head1 SYNOPSYS

  tmx2tmx -cat file1.tmx ... filen.tmx > file.tmx

  tmx2tmx -toTrados file1.tmx > file2.tmx

  tmx2tmx -clean file1.tmx > file2.tmx

=cut

#  tmx2tmx -join file1.tmx ... filen.tmx > file.tmx

=head1 DESCRIPTION

This utility processes TMX documents and return TMX documents. Tasks
done with this utility include conversion between TMX versions and TMX
cleaning.

=head2 TRADOS conversion

As you maybe know, TRADOS is a company producing computer software for
translators. It includes WorkBench which imports TMX
files. Unfortunately, the version I used do not import TMX version
1.4.

This process is done using the switch C<-toTrados>:

  tmx2tmx -toTrados file.tmx > trados.tmx

=cut

if ($toTrados) {
  $/ = "</tu>";

  my $header = <>;

  # <header/> -> <header></header>
  $header =~ s!<header([^/>]+)/>!<header$1></header>!;

  # <body version...> -> <body>
  $header =~ s!<body[^>]+>!<body>!;

  # tmx version -> <tmx version="1.1">
  $header =~ s!<tmx version[^>]+>!<tmx version="1.1">!;

  # srclang -> srclang="foo"
  if ($header =~ m!(xml:)?lang=(["'])([^"']+)\2!) {
    my $lang = $3;
    $header =~ s!srclang=(["'])[^"']+\1!srclang="$lang"!;
  }

  $header =~ s/xml:lang/lang/g;

  # xml:lang -> lang
  print $header;

  while(<>) {
    s/xml:lang/lang/g;
    print
  }

  # ugly, but prevents us from forgetting an else somewhere.
  exit;
}

=head2 TMX Cleaning

Specially when translation memories are created from anotated text, or
extracted directly from the Internet using any automatic method. This
switch is used to remove junk in translation units.

This option tries to remove junk from TMX files, like empty pairs
where one of the sides is empty, or removing other junk type.

Use it this way:

   tmx2tmx -clean file.tmx > file2.tmx

=cut

if ($clean) {
  my $file;
  my $reader;
  if ($file = shift) {
    # isto tv fosse mais rpido em sax...
    print dt( $file,
	      -default => sub{ toxml() },
	      seg => sub{
		$c = trim($c); toxml
	      },
	      tu  => sub{
		# remove entries with junk, only
		return "" if $c =~ m!<seg>(\s|[\|\!@#\$%^&\-])*</seg>!;
		toxml()
	      },
	    );


  } else {
    die "At the moment, we do not handle files from stdin";
    # TODO...  aqui fazia jeito que o XML::TMX::Reader lsse de um
    # filehandle j aberto (STDIN, por exemplo);
  }
}



#=head2 Joining TMX
#
#   tmx2tmx -join file1.tmx ... filen.tmx > file.tmx
#
#=cut

# para juntar temos de saber que linguas cada tmx tem:
#
#  1- s podemos juntar se tiverem lnguas em comum?
#  2- temos de ter cuidado com segmentos repetidos
#     2.1- manter tudo em memria?
#

=head2 Concatenating TMX

   tmx2tmx -cat file1.tmx ... filen.tmx > file.tmx
   ls | grep '.tmx$' | tmx2tmx -cat > file.tmx

=cut

if ($cat) {
  my @files ;
  if(@ARGV){ @files = @ARGV; }
  else { @files = map { chomp; $_ } <> }

  my $file = shift @files;
  open F, $file or die "Cannot open file: $file\n";
  while(<F>) {
      print;
      last if m!<body!;
  }
  print "<!-- FILE: $file -->\n";
  while(<F>) {
    s!</\s*(body|tmx)\s*>!!;
    print;
  }
  close F;

  for $file (@files) {
    open F, $file or die "Cannot open file: $file\n";
    print "<!-- FILE: $file -->\n";
    while(<F>) {
      last if m!<body!;
    }
    s/<body[^>]*>//g;
    print;
    while(<F>) {
      s!</\s*(body|tmx)\s*>!!;
      print;
    }
    close F;
  }
  print "</body>\n";
  print "</tmx>\n";
}


=head1 SEE ALSO

tmx2html, po2tmx, XML::TMX

=head1 AUTHOR

Alberto Simes, E<lt>albie@alfarrabio.di.uminho.ptE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2004 by Projecto Natura

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut



#--( AUX FUNCS )-----------
sub trim {
  my $x = shift;
  for ($x) {
    s!^\s+!!;
    s!\s+$!!
  }
  $x
}
