#! /usr/bin/perl --					# -*-Perl-*-
eval "exec /opt/local/bin/perl -S $0 $*"
    if $running_under_some_shell;

# Combine a new .po file and an old translated .po file to a new translation.
# Copyright (C) 1995 Free Software Foundation, Inc.
# Ulrich Drepper <drepper@gnu.ai.mit.edu>, May 1995.
#
# I have almost rewritten this file to only care about comments, not about
# entries.

# We take exactly two arguments.
#
#
if ($#ARGV != 1)
{
    printf "if you want to merge an existing translation with a new one\n";
    printf "use :\n\n";
    printf "msgmerge [yourTraslation.po] [kmpg.po]\n";
    printf "\n\n";
    printf "It is very unlikley that you are looking for this program :-)\n";
    die "syntax: ./commentmerge [new.po] [old.po]\n";
}

unless (open(OLD, "$ARGV[1]"))
{
    die "Cannot open old translated .po file '$ARGV[1]'\n";
}

$last_comment = "";

while (<OLD>)
{
  $last_comment = "";

  if (/^#/)
      {
       while ( $_ !~ m/^\s*msgid\s*\"/)
        {
	  $last_comment .= $_;
	  $_ = <OLD>;
        }
    }
    if (/^\s*msgid/)
    {
	$_ =~ s/^\s*msgid\s*//;
	$msgid = $_;

	$_ = <OLD>;

        while (/^\s*\"/)
        {
	    $msgid .= $_;
            $_ = <OLD>;
        }
    } else {
      $msgid = "";
    }

    $comment{$msgid} = $last_comment if ($last_comment && $msgid);
}

close (OLD);

unless (open (NEW, "$ARGV[0]"))
{
    die "Cannot open new .po file '$ARGV[0]'\n";
}

my $first = 1;

while (<NEW>)
{
  $last_comment = "";

  while (/^#\~/ || /^\s*$/) {
	 print STDOUT "$_";
	 $_ = <NEW>;
	 last if (! $_);
       }

  if (/^#/) {

      while ( $_ !~ m/^\s*msgid\s*\"/) {
	if ($_ =~ /^#,\s*fuzzy/ || $_ =~ /^#\s*,\s*c-format/ || $first == 1) {
	    $last_comment .= $_;
	  }

	$_ = <NEW>;
	last if (! $_);
	last if (/^#\~/);
      }
    }
  if (/^\s*msgid/) {
    $_ =~ s/^\s*msgid\s*//;
    $msgid = $_;
    
    $_ = <NEW>;
    
    while (/^\s*\"/)
      {
	$msgid .= $_;
	$_ = <NEW>;
      }

    if ($last_comment =~ /c-format/ && $comment{$msgid} =~ /c-format/) {
       $comment{$msgid} =~ s/# *, c-format//;
       $comment{$msgid} =~ s/\n\n/\n/;
    }
      
    if ($first == 0) {
      print STDOUT $comment{$msgid};
    }
    
    print STDOUT $last_comment;

    print STDOUT "msgid $msgid";
    
  } else {
    print STDOUT $last_comment;
    $msgid = "";
  }
      
  $first = 0;


      print STDOUT $_;
      
}

  


exit 0;
