#! /usr/bin/perl

# remove a text from *.po files

sub drop;

die "usage: rm_text id\n" if @ARGV != 1;

$id = shift;
$id = "txt_$id" unless $id =~ /^txt_/;

mkdir old, 0755;

for $f ("bootloader.pot", <*.po>) {
  if(open F, $f) {
    @f = <F>;
    close F;

    ( $new, $old ) = drop @f;
    if(open F, ">>old/$f") {
      print F @$old;
      close F;

      open F, ">$f";
      print F @$new;
      close F;
    }
  }
}


sub drop
{
  local $_;
  my (@f, @g, $drop_it, @d);

  for (@_) {
    push @g, $_;
    $drop_it = 1 if /^#\.\s*${id}\s*$/;
    if(/^\s*$/) {
      if($drop_it) {
        push @d, @g;
      }
      else {
        push @f, @g;
      }
      undef $drop_it;
      undef @g;
    }
  }

  if(@g) {
    if($drop_it) {
      push @d, @g;
    }
    else {
      push @f, @g;
    }
  }

  return ( \@f, \@d );
}

