#!/usr/bin/env perl
=head
    Generate HTML translation reports.
    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 Locale::Language;
use HTML::Template;
use English qw( -no_match_vars );
use Carp;

my $book
    = `grep -m1 DOCNAME ../../Makefile | sed -e 's/^.*=\\s*//g' | sed -e 's/_/ /g'`;
my $system
    = `grep -m1 SYSTEM ../../Makefile | sed -e 's/^.*=\\s*//g' | sed -e 's/_/ /g'`;
my $src_lang = `grep -m1 XML_LANG ../../Makefile | sed -e 's/^.*=\\s*//g'`;
chomp($src_lang);

my $GUI = 0;

if ( !$book ) {
    $book
        = `grep -m1 PROGNAME ../../Makefile | sed -e 's/^.*=\\s*//g' | sed -e 's/_/ /g'`;
    if ($book) {
        $GUI = 1;
    }
}

chomp($book);
chomp($system);

my $config = {
    INCLUDE_PATH => '/usr/share/publican/templates',  # or list ref
    DEBUG        => 1,
};

# create Template object
#my $template = Template->new($config) or croak Template->error();

report();

sub report {

    my @locales;
    my $title = "$system $book Report: " . `date +"%d %b %y"`;
    chomp $title;

    foreach my $file (`ls *.txt | sort`) {

        chomp($file);
        $file =~ /^(.*)-(.*)\.txt/;
        my $lang     = "$1-$2";
        my $language = code2language($1);
        if ( $language eq 'Chinese' ) {
            $language .= " $lang";
        }

        my $report_type = 'Translation';
        if ( $lang eq $src_lang ) {
            $report_type = 'Completion';
        }

        open my $IN_FILE, '<', $file
            or croak "could not open $file: $OS_ERROR";

        my @results;
        my $hours_fuzzy          = '0';
        my $hours_remaining      = '0';
        my $hours_untranslated   = '0';
        my $words_fuzzy          = '0';
        my $words_total          = '0';
        my $words_translated     = '0';
        my $words_translated_pct = '0';
        my $words_fuzzy_pct      = '0';

        while ( my $line = <$IN_FILE> ) {
            chomp($line);
            if ( $line !~ /\S/ ) { next; }
            my @strings = split( " ", $line );

            # MUNGE!
            if ( $line =~ /\.po:/ ) {
                my $po_file = $strings[0];
                chop $po_file;    # remove :

                my $complete       = 0;
                my $total          = 0;
                my $translated_pct = 0;
                my $fuzzy_pct      = 0;
                my $fuzzy          = 0;
                my $translated     = 0;

                # Skip percentages for files with no entries
                if ( $line !~ /warning: no words found/ ) {

                    # get number of translated words
                    $strings[1] =~ /^(\d*)\/(\d*)$/;
                    $complete = $1 || 0;
                    $total    = $2 || 0;

                    $strings[4] =~ /[(](.*)[%][)]/;
                    $translated_pct = "$1";

                    # fuzzy comes before translated, may not be any fuzzy.
                    if ( $strings[2] eq 'fuzzy' ) {
                        $fuzzy     = $complete;
                        $fuzzy_pct = $translated_pct;

                        if ( $strings[6] eq 'translated' ) {
                            $strings[5] =~ /^(\d*)\/(\d*)$/;
                            $translated = $1;
                            $strings[8] =~ /[(](.*)[%][)]/;
                            $translated_pct = "$1";
                        }
                    }
                    elsif ( $strings[2] eq 'translated' ) {
                        $translated = $complete;
                    }
                }

                push @results,
                    {
                    'fuzzy'          => $fuzzy,
                    'translated'     => $translated,
                    'total'          => $total,
                    'translated_pct' => $translated_pct,
                    'fuzzy_pct'      => $fuzzy_pct
                    };

            }
            elsif ( $strings[0] eq "total:" ) {
                if ( $strings[3] eq "hours," ) {
                    $hours_fuzzy        = $strings[1];
                    $hours_remaining    = $strings[7] || 0;
                    $hours_untranslated = $strings[4] || 0;

                    if ( $strings[2] ne 'fuzzy' ) {
                        $hours_fuzzy        = 0;
                        $hours_remaining    = $strings[4];
                        $hours_untranslated = $strings[1];
                    }
                }
                elsif ( $strings[3] eq 'words' ) {
                    $strings[1] =~ /^(\d*)\/(\d*)$/;

                    $words_translated = $1;
                    $words_total      = $2;

                    $strings[4] =~ /[(](.*)[%][)]/;
                    $words_translated_pct = $1;

                    $words_fuzzy_pct = '0.0';
                    $words_fuzzy     = 0;

                    if ( $strings[2] eq 'fuzzy' ) {
                        $words_fuzzy_pct = $words_translated_pct;
                        $words_fuzzy     = $words_translated;

                        $strings[5] =~ /^(\d*)\/(\d*)$/;

                        $words_translated = $1;

                        $strings[8] =~ /[(](.*)[%][)]/;
                        $words_translated_pct = $1;
                    }
                }
            }
        }

        close $IN_FILE or croak "Can't close $file after reading: $OS_ERROR";

        push @locales,
            {
            'lang'                 => $lang,
            'name'                 => $language,
            'hours_translated_pct' => $words_translated_pct,
            'hours_fuzzy_pct'      => $words_fuzzy_pct,
            'hours_fuzzy'          => $hours_fuzzy,
            'hours_untranslated'   => $hours_untranslated,
            'hours_remaining'      => $hours_remaining,
            };

        my $template = HTML::Template->new(
            filename => 'language.tmpl',
            path     => ['/usr/share/publican/templates']
        ) or croak "Template->error";

        $template->param( title                => $title );
        $template->param( words_translated_pct => $words_translated_pct );
        $template->param( words_fuzzy_pct      => $words_fuzzy_pct );
        $template->param( words_fuzzy          => $words_fuzzy );
        $template->param( words_translated     => $words_translated );
        $template->param( words_total          => $words_total );
        $template->param( hours_translated_pct => $words_translated_pct );
        $template->param( hours_fuzzy_pct      => $words_fuzzy_pct );
        $template->param( hours_fuzzy          => $hours_fuzzy );
        $template->param( hours_untranslated   => $hours_untranslated );
        $template->param( hours_remaining      => $hours_remaining );
        $template->param( files                => \@results );

        open( OUTFILE, '>', "../Reports/$lang.html" )
            || carp "Can't open file: $!";
        print OUTFILE $template->output();
        close OUTFILE;
    }

    my $template = HTML::Template->new(
        filename => 'project.tmpl',
        path     => ['/usr/share/publican/templates']
    ) or croak "Template->error";

    $template->param( title   => $title );
    $template->param( locales => \@locales );

    open( OUTFILE, '>', "../Reports/index.html" )
        || carp "Can't open file: $!";
    print OUTFILE $template->output();
    close OUTFILE;

}

