#!/usr/bin/perl -w
# (c) 2007 by Rhonda D'Vine <rhonda@deb.at>
# Licenced under BSD
#
# fixes po files with --previous support that were b0rked by poedit
# usage: ./pofix.pl < original.po > fixed.po

use strict;

$0=~s,.*/,,;

my $prev = '';
my $inBlock = 0;

while (my $line = <>) {
	if      ($line =~ /^#:/) {
		$inBlock = 1;

	} elsif ($line =~ /^#\|/ && ! $inBlock) {
		$prev .= $line;
		next;

	} elsif ($line =~ /^#\|/ && $inBlock && $prev) {
		print STDERR "Double previous msgid found - ignoring wrongly placed one\n";
		$prev = '';

	} elsif ($line =~ /^m/) {
		$inBlock = 0;
		print $prev;
		$prev = '';
	}
	print $line;
}
