#!/usr/bin/env perl

=head xlf2po
Description:
	script to convert a set of xliff source files into a set of pot files
	runs output through msguniq

Authors:
	Bernd Groh <bgroh@redhat.com>

	jfearn Thur May 31, 2007
		Modified to get input and output files

Usage:
	xlf2pot --help
	xlf2pot --output=<pot output file> --input=<xlif input file>
=cut
=head
    Copyright (C) 2008 Red Hat, Inc.

    This program 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 program 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 program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

=cut

use strict;
use warnings;
use utf8;
use bytes;
use Getopt::Long;
use XML::TreeBuilder;

my $outfile = undef;
my $infile  = undef;
my $help    = 0;

GetOptions(
    'output=s' => \$outfile,
    'input=s'  => \$infile,
    'h|help'   => \$help,
);

if ($help) {
    Usage();
    exit(0);
}

if ( !$infile || !$outfile ) {
    die( Usage() );
}

sub Usage {
    print( STDERR
            "xlf2pot --output=<pot output file> --input=<xlif input file>\n"
    );
    print( STDERR "All arguments are mandatory.\n" );
}

my $parser = XML::Parser->new(
    ErrorContext     => 2,
    Style            => 'Tree',
    ProtocolEncoding => 'UTF-8'
);

sub convert_xliff_to_pot {
    print $infile, "\n";

    open( POTFILE, ">tmp.raw" );
    print POTFILE "# XLIFF: $infile\n";
    print POTFILE "# Copyright (C) ";
    my $command = `date +%Y`;
    $command =~ s/\s*$//s;
    print POTFILE $command;
    print POTFILE " Red Hat, Inc.\n";
    print POTFILE "# Bernd Groh <bgroh\@redhat.com>, ";
    print POTFILE $command;
    print POTFILE "\n";
    print POTFILE "#\n";
    print POTFILE "#, fuzzy\n";
    print POTFILE "msgid \"\"\n";
    print POTFILE "msgstr \"\"\n";
    print POTFILE "\"Project-Id-Version: $infile\\n\"\n";
    print POTFILE "\"POT-Creation-Date: ";
    $command = `date +%Y-%m-%d`;
    $command =~ s/\s*$//s;
    print POTFILE $command;
    print POTFILE " ";
    $command = `date +%H:%M%z`;
    $command =~ s/\s*$//s;
    print POTFILE $command;
    print POTFILE "\\n\"\n";
    print POTFILE "\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"\n";
    print POTFILE "\"Last-Translator: FULL NAME <EMAIL\@ADDRESS>\\n\"\n";
    print POTFILE "\"Language-Team: LANGUAGE <LL\@li.org>\\n\"\n";
    print POTFILE "\"MIME-Version: 1.0\\n\"\n";
    print POTFILE "\"Content-Type: text/plain; charset=UTF-8\\n\"\n";
    print POTFILE "\"Content-Transfer-Encoding: 8bit\\n\"\n\n";

    my $xml_doc = XML::TreeBuilder->new();
    $xml_doc->parse_file($infile);
    my $transcount = 0;
    foreach my $transobj ( $xml_doc->find_by_tag_name('trans-unit') ) {

        # ignore nodes marked as no translate
        my $note = $transobj->find_by_tag_name('note');
        if ( $note && $note eq 'NO TRANSLATION NECSSARY' ) {
            print( STDERR "skipping " . $transobj->attr("id") . "\n" );
            next;
        }

        $transcount++;
        my $sourceobj = $transobj->find_by_tag_name('source');
        my $source    = $sourceobj->as_text();
        my $id        = $transobj->attr("id");
        if (!(  ( defined $id ) && ( defined $source ) && ( $source =~ /\S/ )
            )
            )
        {
            print STDERR "Error in ", $infile, " at trans-unit ";

            if ( defined $id ) {
                print STDERR '"', $id, '"';
            }
            else {
                print STDERR '#', $transcount;
            }
            print STDERR "\n";
        }
        else {
            print POTFILE '#. ID:"', $id, '"', "\n";
            print POTFILE '#, no-c-format', "\n";
            print POTFILE '#: ', $infile, ":", $transcount, "\n";

            $source =~ s/"/\\"/gs;
            if ( $source =~ /\n/s ) {
                $source =~ s/\s+$//gs;
                $source =~ s/\n/\\n"\n"/gs;
                print POTFILE 'msgid ""', "\n", '"', $source, '"', "\n";
            }
            else {
                print POTFILE 'msgid "', $source, '"', "\n";
            }
            print POTFILE 'msgstr ""', "\n\n";
        }
    }
    foreach my $groupobj ( $xml_doc->find_by_tag_name('group') ) {
        my $id_header = '';
        foreach my $transobj ( $groupobj->find_by_tag_name('trans-unit') ) {
            $transcount++;

            my $sourceobj = $transobj->find_by_tag_name('source');
            my $source    = $sourceobj->as_text();
            my $id        = $transobj->attr("id");
            if (!(     ( defined $id )
                    && ( defined $source )
                    && ( $source =~ /\S/ )
                )
                )
            {
                print STDERR "Error in ", $infile, " at trans-unit ";

                if ( defined $id ) {
                    print STDERR '"', $id, '"';
                }
                else {
                    print STDERR '#', $transcount;
                }
                print STDERR "\n";
            }
            else {
                if ( $id_header ne '' ) {
                    print POTFILE $id_header;
                }
                print POTFILE '#. ID:"', $id, '"', "\n";
                print POTFILE '#, no-c-format', "\n";
                print POTFILE '#: ', $infile, ":", $transcount, "\n";

                $source =~ s/"/\\"/gs;
                if ( $source =~ /\n/s ) {
                    $source =~ s/\s+$//gs;
                    $source =~ s/\n/\\n"\n"/gs;
                    print POTFILE 'msgid ""', "\n", '"', $source, '"', "\n";
                }
                else {
                    print POTFILE 'msgid "', $source, '"', "\n";
                }
                print POTFILE 'msgstr ""', "\n\n";
            }
        }
    }

    close(POTFILE);

    system("msguniq -o $outfile tmp.raw");
    system("rm -f tmp.raw");
}

convert_xliff_to_pot();
exit(0);
