#! /usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;

# pod-updatepo -- Update the po translation of POD data.
# $Id: po4a-updatepo,v 1.27 2004/10/30 10:40:47 jvprat-guest Exp $
#
# Copyright 2002, 2003, 2004 by Martin Quinson <Martin.Quinson@ens-lyon.fr>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of GPL (see COPYING).

my $VERSION=$Locale::Po4a::TransTractor::VERSION;

=head1 NAME

po4a-updatepo - Update the translation (in po format) of documentation.

=head1 SYNOPSIS

po4a-updatepo -f E<lt>fmtE<gt> (-m E<lt>master.docE<gt>)+ (-p E<lt>XX.poE<gt>)+

(XX.po are the output, all others are input)

=head1 DESCRIPTION

The po4a (po for anything) project goal is to ease translations (and more
interestingly, the maintenance of translations) using gettext tools on
areas where they were not expected like documentation.

The C<po4a-updatepo> script is in charge of updating po files to make
them reflect the changes made to the original documentation file. For that,
it converts the documentation file to a pot file, and call L<msgmerge(1)>
on this new pot and on the provided po files.

It is possible to give more than one po file (if you want to update several
languages at once), and several documentation file (if you want to store
the translations of several documents in the same po files).

If the master document has non-ascii characters, it will convert the po files
to utf-8 (if they weren't already), in order to allow non-standard characters
in a culture independent way.

=head1 OPTIONS

=over 4

=item -f, --format

Format type of the documentation you want to handle. Use the --help-format
option to see the list of available formats.

=item -m, --master

File(s) containing the master document to translate.

=item -M, --master-charset

Charset of the files containing the document to translate. Note that all
files must have the same charset.

=item -p, --po

Po file(s) to update. If these files do not exist, they are created by
C<po4a-updatepo>.

=item -o, --option

Pass an extra option to the format plugin. See the documentation of each
plugin for more information about the valid options and their meaning.

=item -h, --help

Show a short help message.

=item --help-format

List the documentation format understood by po4a.

=item -V, --version

Displays the version of the script and exits.

=item -v, --verbose

Increase the verbosity of the program.

=item -d, --debug

Outputs some debugging informations.

=back

=head1 SEE ALSO

L<po4a(7)>, L<po4a-gettextize(1)>, L<po4a-translate(1)>, L<po4a-normalize(1)>.

=head1 AUTHORS

 Denis Barbier <barbier@linuxfr.org>
 Martin Quinson <martin.quinson@tuxfamily.org>

=head1 COPYRIGHT AND LICENSE

Copyright 2002, 2003, 2004 by SPI, inc.

This program is free software; you may redistribute it and/or modify it
under the terms of GPL (see COPYING file).

=cut

use 5.006;
use strict;
use warnings;

use Getopt::Long qw(GetOptions);
use Locale::Po4a::Po;

use Locale::Po4a::Chooser;
use Locale::Po4a::TransTractor;

use Pod::Usage qw(pod2usage);

use File::Temp;

use Locale::gettext;
use POSIX;     # Needed for setlocale()

setlocale(LC_MESSAGES, "");
textdomain('po4a');

sub show_version {
    print sprintf(gettext(
          "%s version %s.\n".
          "written by Martin Quinson and Denis Barbier.\n\n".
          "Copyright (C) 2002, 2003, 2004 Software of Public Interest, Inc.\n".
          "This is free software; see source code for copying\n".
          "conditions. There is NO warranty; not even for\n".
          "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
          ),"po4a-updatepo",$VERSION)."\n";
    exit 0;
}


# init commandline parser
Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');

# Parse our options
my (@masterfiles,@pofiles);
my ($help,$help_fmt,$verbose,$debug,$format,@options);
my $mastchar;
GetOptions('help|h'      => \$help,
  	   'help-format' => \$help_fmt,
	   
	   'master|m=s'  => \@masterfiles,
	   'po|p=s'      => \@pofiles,
	   'format|f=s'  => \$format,

           'master-charset|M=s' => \$mastchar,

           'option|o=s'    => \@options,
    
	   'verbose|v'   => \$verbose,
	   'debug|d'     => \$debug,
	   'version|V'   => \&show_version)
    or pod2usage(1);
	  
$help && pod2usage (0);
$help_fmt && Locale::Po4a::Chooser::list(0);
pod2usage (1) if scalar @masterfiles < 1 || scalar @pofiles < 1;

my %options = (
    "verbose" => $verbose,
    "debug" => $debug);

foreach (@options) {
    if (m/^([^=]*)=(.*)$/) {
	$options{$1}="$2";
    } else {
	$options{$_}=1;
    }
}

# parser
my ($doc)=Locale::Po4a::Chooser::new($format,%options);

map { -e $_ || die sprintf(gettext("File %s does not exist."),$_)."\n" } @masterfiles;
map { die(gettext("po4a-updatepo can't take the input po from stdin."))."\n"
	  if $_ eq '-'  && !-e '-'} @pofiles;

my ($pot_filename);
(undef,$pot_filename)=File::Temp->tempfile("po4a-updatepoXXXX",
					   DIR    => "/tmp",
					   SUFFIX => ".pot",
					   OPEN   => 0,
					   UNLINK => 0)
    or die sprintf(gettext("Can't create a temporary pot file: %s"),$!)."\n";


print STDERR gettext("Parse input files... ") if $verbose;    

$doc->{TT}{utf_mode} = 1;

$doc->process('file_in_name'    => \@masterfiles,
	      'file_in_charset' => $mastchar,
	      'po_out_name'     => $pot_filename,
	      'debug'           => $debug,
	      'verbose'         => $verbose);

print STDERR gettext("done.")."\n" if $verbose;


while (my $po_filename=shift @pofiles) {
    if (-e $po_filename) {
	print STDERR sprintf(gettext("Updating %s:"),$po_filename) 
	    if $verbose;    
	system ("msgmerge","-U",$po_filename,$pot_filename) == 0 ||
	    die sprintf(gettext("Error while running msgmerge: %s"),$!)."\n";
	system "msgfmt --statistics -v -o /dev/null $po_filename" 
	  if $verbose;
    } else {
	print STDERR sprintf(gettext("Creating %s:"),$po_filename) 
	    if $verbose;    
	system ("cp",$pot_filename,$po_filename) == 0 ||
	    die sprintf(gettext("Error while copying the po file: %s"),$!)."\n";
    }
}

unlink($pot_filename);
