The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -w -s

our($s);

use Data::Dumper;
use strict;
use lib 'lib';
use XML::TMX::FromPO;

my $tmx = new XML::TMX::FromPO(DEBUG => 1);
my %convert = ( 'pt_en' => 'pt en',
                'pt_es' => 'pt es',
                'pt_de' => 'pt de',
                'pt_it' => 'pt it',
                'pt_ru' => 'pt ru',
                'pt_fr' => 'pt fr',
                'pt_br' => 'pt pt_BR',

                'br_en' => 'pt_BR en',
                'br_es' => 'pt_BR es',
                'br_de' => 'pt_BR de',
                'br_it' => 'pt_BR it',
                'br_ru' => 'pt_BR ru',
                'br_fr' => 'pt_BR fr',

                'en_fr' => 'en fr'
              );

if($s){
  my $lang = $s;
  if ($s eq "1") { die("Usage $0 -s=pt DIR\n")}
 
  while(my $dir = shift()) {
      $tmx->rec_get_po($dir, $lang, LANG=>"$lang en");
      open(XML, "| tee _1.aux |xmllint --recover --format - | bzip2 > $lang-en.tmx.bz2");
#      print STDOUT Dumper($tmx); 
      *STDOUT = *XML;
      $tmx->create_tmx(LANG => "$lang en");
      close(XML);
      $tmx->clean_tmx()
  }
}
else{

  my $lang = '';
  for my $c (keys %convert) {
     $lang = $lang . " $convert{$c}";
  }

  while(my $dir = shift()) {
      if(-d "$dir/po" and <$dir/po/*.po>){ $tmx->parse_dir("$dir/po", LANG => $lang) ; }
      if(-d "$dir"    and <$dir/*.po>)   { $tmx->parse_dir("$dir",    LANG => $lang) ; }
      $dir =~ m/([a-z0-9_\.-]+)\/*$/i;
      for my $conv (keys %convert) {
         open(XML, "|xmllint --format - | bzip2 > $1_$conv.tmx.bz2");
         *STDOUT = *XML;
         $tmx->create_tmx(LANG => "$convert{$conv}");
         close(XML);
      }
      $tmx->clean_tmx()
  }
}

=encoding UTF-8

=head1 NAME

po2tmx - creates TMX files from PO files

=head1 SYNOPSIS

   po2tmx  POdir   # 
   po2tmx  -s=fr   #single PO files en-fr (does not join multiple PO-files)

=head1 DESCRIPTION

read several POs, and joinds their translation units to build several 
TMX pairs.

=head1 SEE ALSO

XML::TMX

=cut