#!/usr/bin/perl
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
	if $running_under_some_shell;
#!/usr/bin/perl

use strict;

sub process_file($)
{
    my $file = shift;
    my $out = "";

    open (FILE, "$file") ||
	die "can't open \"$file\" for reading: $!\n";

    my $fuzzy=0;
    while (my $line = <FILE>) {
	chomp $line;

	if ($line =~ m/^#, fuzzy/) {
	    $fuzzy=1;
	} elsif ($line =~ m/^\s*$/) {
	    $fuzzy=0;
	} if ($line =~ m/^#~ msgid/ ||
	      $line =~ m/^#~ msgstr/ ||
	      $line =~ m/^#~ msgctxt/ ||
	      $line =~ m/^#~ \"/) {
	  # ignore these lines if they are not prefixed by the #, fuzzy comment
	  # otherwise msgcat is confused
	  print "$line\n" unless ($fuzzy);
	  next unless ($fuzzy);
	  
	}
	
	$out .= "$line\n";
    }

    close(FILE);

    return $out;
}

sub write_file($$)
{
    my ($file, $content) = @_;

    open (FILE, '>', "$file") ||
	show_error("Can't open \"$file\" for writing: $!");

    print FILE $content;

    close (FILE);
}


sub cleanup_file($)
{
    my $file = shift;

    my $out = process_file($file);
    write_file($file, $out);
}


########################################################################
# help

sub usage()
{
    print "This tool cleans the given .po files\n\n" .

          "Usage:\n".
          "\tpo-cleanup [--help] file.po...\n";
}


#######################################################################
#######################################################################
# MAIN
#######################################################################
#######################################################################

for my $arg (@ARGV) {
    if ($arg eq '--help' || $arg eq '-h') {
	usage;
	exit 0;
    } elsif ($arg =~ m/^-/) {
	die "Error: Unkown option: $arg\n";
    } else {
	if (-f $arg) {
	    cleanup_file($arg);
	} else {
	    die "Error: Is not a file: $arg\n";
	}
    }
}

