#!/usr/bin/perl

# a lame attempt at xgettext for perl
# no line numbers, yet

# 5.005_02 in particular seems to have a BIG BUG
# resulting in an endless loop and a memory leak in the 
# regex machinery :(

## updated by salsaman for LiVES

exit 0 unless ( $] >= 5.005_03 || $] <= 5.005 );

undef $/;

use File::Basename;

$prefix=$ARGV[0];
shift (@ARGV);
$outfile=$ARGV[0];
shift (@ARGV);

while (<>) {
    print STDERR ".";
    $file = $ARGV;
    $file =~ s/\.\.\///;
    $basename = basename($file);
    
    while (/(?:N|print) \(?"((?:[^"\\]+|\\.)*)"\)?/sg) {
       my $s = $1;
       if($s=~/\|/){
         @temp=split(/\|/, $s);
       } else {
         $s =~ s/\\n//;
         @temp[0]=$s;
       } if ($temp[0]) {
         $e = "msgid \"$temp[0]\"";
         $fileposition = "#: $prefix/share/lives/plugins/effects/rendered/$basename:0\n";
         unless (&exists($e)) {
          $e.="\nmsgstr \"\"\n";
          push @{$entry{$e}}, $fileposition;              
         }
       }
       if($temp[1] and not $temp[1]=~/[0-9]/) {
         $e = "msgid \"$temp[1]\"";
         unless (&exists($e)) {
          $e.="\nmsgstr \"\"\n";
          push @{$entry{$e}}, $fileposition;              
        }
      }
    }
}
foreach $e (sort keys %entry) {
    if ($e=~/layout/ or $e=~/hseparator/) {} else {
    print @{$entry{$e}};
    print $e;
    print "\n";
    }
}
print STDERR " done.\n";

#########################################################

sub exists {
# stop duplicate entries. TODO - add the msgid after the original entry
    my ($ex)=@_;
    chomp($ex);
    my $exi=`grep '$ex' $outfile`;
    if ($exi eq "") {return 0;}
    return 1;
}
