#!/usr/bin/perl
#
# Copyright (C) by Stefano Falsetto
# e-mail contact ....: mailto:stefko5@inwind.it
#
#    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# This script merge one old .po file and a new version of file generated
# with "bash --dump-po-strings"
#
#

print STDERR "Merging @ARGV[0] (SRC) and @ARGV[1] (OLD)\n";
open(SRC,"<@ARGV[0]");
@new=<SRC>;
close(SRC);

open(OLD,"<@ARGV[1]");
@old=<OLD>;
close(OLD);

$i=0;
while (!(substr(@old[$i],0,2)=~ /#:/)) {
  print "@old[$i++]";
}


# USARE splice????

$nrighe=scalar(@new);
for($j=0;$j<$nrighe;$j++) {
  if (substr(@new[$j],0,2)=~ /#:/) {
    print "@new[$j]";
    $j++;
    print "@new[$j]";
    $trovato=0;
    # This while cicle is for multi-line strings. Bash don't allow use of this.
    while (!(substr(@new[$j],0,2)=~ /#:/)) {
      for($indice=$i;$indice<scalar(@old); $indice++) {
        $n="@new[$j]";
        $v="@old[$indice]";
        if (index($v,$n)==0) {
          $trovato=1;
          last;
        }
      }
      if ($trovato eq 0) {
        print "@new[$j+1]";
      }
      else {
        print "@old[$indice+1]";
      }
      $j++;
      if ($j eq $nrighe) { last; }
    }
    $j--;
  }
}

