#!/usr/bin/perl -w

use strict;
use warnings;
use File::Find;

my $prefix = 'I18N_OPENXPKI_UI_';

my @MANIFEST;

my %tags = ();
my $basedir = '';

foreach my $dir (@ARGV) {
    $basedir = $dir;
    @MANIFEST = ();
    if (-e "$dir/MANIFEST") {
        open my $MAN, '<', "$dir/MANIFEST";
        @MANIFEST = <$MAN>;
        close $MAN;
        foreach my $man (@MANIFEST) {
            chomp $man;
        }
    }
    find(\&extract_tags, $dir);
}

sub extract_tags {
    my $fn = $_;
    my $rel_name = $File::Find::name;
    my $dir_name = $File::Find::topdir;
    $rel_name =~ s/$basedir\///;
    if ($File::Find::name !~ m{ \.svn }xms) {
        if (scalar @MANIFEST > 0 && ! grep {$_ eq $rel_name} @MANIFEST) {
            # if we have a MANIFEST file, the file needs to be in it
            # to be searched for tags
            return;
        }

        # skip
        return unless -e $fn;                     # non-existing symlink source
        return if $fn =~ m{ \A \. }xms;           # hidden file
        return if $fn =~ m{ ~ \z }xms;            # backup file
        return if $fn =~ m{ \A openxpki\.po }xms; # po file (config/contrib/)
        return if $fn eq 'Makefile';              # Makefile

        open my $FILE, '<', $fn;
        while (my $line = <$FILE>) {
            while ($line =~ s{ ($prefix [A-Z0-9\_]+[A-Z0-9]) }{}xms) {
                $tags{$1} = 1;
            }
        }
        close $FILE;
    }
}

print <<'XEOF';

# OpenXPKI i18n language files - these files are autogenerated by make!
# Copyright (C) 2019 OpenXPKI Foundation
# This file is distributed under the same license as the OpenXPKI core.

msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2004-09-08 14:02+0200\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"

XEOF

foreach my $tag (sort keys %tags) {
    print qq{msgid "$tag"\n} . qq{msgstr ""\n};
}
