#!/usr/bin/env perl

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/../thirdparty/perl5";

use PPI;
use PPI::Dumper;
use File::Find;
use Data::Dumper;
use Locale::PO;
use Encode;

my %idStr;

my $root = shift @ARGV;
my $podir = shift @ARGV;

die "$0 <lib-directory> <po-share>\n" unless -d $root and -d $podir;

find ({ 
    bydepth => 1,
    follow => 1,
    no_chdir => 1,
    wanted => sub {
        return if not ( /\.pm$/ and -f);
        my $pm = PPI::Document->new($_);
        $pm->index_locations;
        for my $el (@{$pm->find('PPI::Token::Word')}){
            next if not $el->content eq 'trm';
            my $location = $el->location;
            my $next = $el->snext_sibling;            
            next if not $next->isa('PPI::Structure::List');
            my $id = decode('utf8',$next->find_first('PPI::Token::Quote')->string);
            my $filename = $_;
            $filename =~ s{^$root/?}{};
            push @{$idStr{$id}},$filename.':'.$el->line_number;
        }
    }
},$root);

for my $poFile (glob $podir."/*.po"){
    my $po = Locale::PO->load_file_ashash($poFile,'utf8');
    # add new
    for my $id (keys %idStr){
        my $poKey = Locale::PO->quote($id);
        next if $po->{$poKey};
        my $item = $po->{$poKey} //= new Locale::PO(-msgid=>$id, -msgstr=>'');
        $item->reference(join("\n", @{$idStr{$id}}));
    }
    # remove old stuff unless from .cfg file
    for my $poKey (keys %$po){        
        next if $poKey eq '""';
        my $id = Locale::PO->dequote($poKey);
        if (not $idStr{$id}) {
            my $ref = $po->{$poKey}->reference;
            delete $po->{$poKey}
                unless $ref =~ m/\.cfg/;
        }            
    }    
    $po->{'""'} //= new Locale::PO(
            -msgid => '',
            -msgstr => "Project-Id-Version: PACKAGE VERSION\\n" .
            "PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\\n" .
            "Last-Translator: FULL NAME <EMAIL\@ADDRESS>\\n" .
            "Language-Team: LANGUAGE <LL\@li.org>\\n" .
            "MIME-Version: 1.0\\n".
            "Content-Type: text/plain; charset=utf-8\\n".
            "Content-Transfer-Encoding: 8bit\\n"
    );
    Locale::PO->save_file_fromhash($poFile,$po,'utf8');
}

=head1 NAME

callbackery-pobuilder - build po files from trm calls in perl files

=head1 SYNOPSIS

callbackery-pobuilder I<lib-dir> I<po-dir>

=head1 DESCRIPTION

Find all instances of trm in the F<*.pm> files sittuated below
F<lib-dir> and updates all F<*.po> files in F<po-dir>.


=head1 COPYRIGHT

Copyright (c) 2014 by OETIKER+PARTNER AG. All rights reserved.

=head1 LICENSE

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., 675 Mass Ave, Cambridge, MA 02139, USA.

=head1 AUTHOR

S<Tobias Oetiker E<lt>tobi@oetiker.chE<gt>>

=head1 HISTORY

 2014-04-30 to Initial Version

=cut
