#!/usr/bin/perl

# a lame attempt at xgettext for scheme

# adapted from pxgettext as found in the plug-ins/perl directory

# There are rumors that perl version 5.005_02 has a bug
# resulting in an endless loop and a memory leak in the 
# regex machinery. 
#
# It seems to work however. I'll leave this check commented out until
# people report problems.
#
# die ("Your version of Perl (5.005_02) is broken!\nCan't extract the strings from the scripts.\nA lot of messages will be missing from your gimp-script-fu.pot file.\n\n") if $] eq 5.005_02;

undef $/;

my $line;
my $file = "";
my $fileposition;
my $e;
my $s;

while (<>) {
    $file = $ARGV;
    $file =~ s/\.\.\///;
    $line = 0;

    while (/_\(?"((?:[^"\\]+|\\.)*)"\)?/sg) {
       $line++;
       my $s = $1;
       if ($s =~ /\n/) {
          $e = "msgid \"\"\n";
          for (split /\n/, $s) {
             $e .= "\"$_\\n\"\n";
          }
       } else {
          $e = "msgid \"$s\"\n";
       }
       $e .= "msgstr \"\"\n";

       $fileposition = "#: $file:$line\n";

       push @{$entry{$e}}, $fileposition;
    }
}

foreach $e (sort keys %entry) {
    print @{$entry{$e}};
    print $e;
    print "\n";
}

