MAJOR=13
MINOR=69
DEVNAME=input/event5
  6   ˆl–Ü4ý()”u( µÕÀ‚ãm)‹Fÿ�þYaY°¢Hë€¯ùÂ„ŸußÁÀ¿ 2±    #!/usr/bin/perl -w

use strict;
use File::Temp;
use Getopt::Long;

sub usage {
        my $rc = shift;
        print "Usage: podebconf-display-po [-h] [-f frontend] file.po\n";
        exit($rc);
}

my $help = 0;
my $frontend = '';
Getopt::Long::Configure('bundling');
Getopt::Long::GetOptions(
        "h|help" => \$help,
        "f|frontend=s" => \$frontend,
) || usage(1);
usage(0) if $help;
usage(1) if $#ARGV != 0;

sub unescapechar {
        my $char = shift;
        if ($char eq 'n') {
                # Add a space to conform to debconf templates format
                return "\n ";
        } elsif ($char eq 't') {
                return "\t";
        } elsif ($char eq 'r') {
                return "\r";
        } else {
                #  Fallback also works for \\ and \"
                return $char;
        }
}
sub unescapestr {
        my $text = shift;
        my $out = '';
        my $last = 0;
        while ($text =~ m/\G([^\\]*)\\([ntr"\\])/g) {
                $last = pos($text);
                $out .= $1 . unescapechar($2);
        }
        $out .= substr($text, $last);
        return $out;
}
#  Format 1: No "Type:" in comments
#  Format 2: Only one reference per string and file, line numbers
#            match the ones in templates files
#  Format 3: All references are written into PO files; first template
#            is numbered from 1001, 2nd from 2001 etc.
sub guessFormat {
        my $text = shift;
        return 1 unless $text =~ m/^#\. Type:/;
        while ($text =~ s/#:\s*([^:\s]+):(\d+)/#:/) {
                my ($file,$line) = ($1, $2);
                return 2 if $line < 1000;
        }
        return 3;
}

#  1.  Read the PO file and write a fake templates file

my $fields = {};
my $rfc822Format = 0;

#  Recode PO files into UTF-8 to avoid stateful encodings
{
        open(PO, "msgconv -t UTF-8 $ARGV[0] |")
                or die "'msgconv -t UTF-8 $ARGV[0]' failed: $!\n";
        local $/ = "\n\n";
        while (<PO>) {
                next unless m/^msgid/m;
                s/"\n"//g;
                #  Skip header entry
                next if m/^msgid ""$/m;

                my $type = '';
                my $field = '';
                m/^msgid "(.*)"/m;
                my $msgid = unescapestr($1);
                m/^msgstr "(.*)"/m;
                my $msgstr = unescapestr($1);
                #  Escape dollar signs to prevent variable substitutions
                $msgid =~ s/\$/\${dollarsign}/g;
                $msgstr =~ s/\$/\${dollarsign}/g;
                $rfc822Format = guessFormat($_) if $rfc822Format == 0;
                while (s/#:\s*([^:\s]+):(\d+)/#:/) {
                        my ($file,$line) = ($1, $2);
                        my $tmp = $_;
                        if ($rfc822Format == 1) {
                                #  Old format
                                1 while ($tmp =~ s/^#\.\s+(.*)//m);
                                $field = $1;
                        } else {
                                #  New format
                                if ($tmp =~ s/^#\. Type:\s*(\S+)\n//) {
                                        $type = lc($1);
                                }
                                #  The entropy-text type is a plugin
                                $type = "text" if $type eq "entropy-text";
                                if ($tmp =~ s/^#\. (\S+)\n//) {
                                        $field = $1;
                                }
                        }
                        $field = ucfirst($field);
                        $fields->{$file} = {} unless defined($fields->{$file});
                        if (defined $fields->{$file}->{$line}) {
                                if ($field eq 'Choices') {
                                        if ($msgstr =~ m/,/) {
                                            warn "The translation of Choices ".
                                                 "should not contain commas