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

# Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org)
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of GPL (see COPYING).


=head1 NAME

msgsearch - Extract some messages of a po file based on several criteron

=head1 SYNOPSIS

msgsearch [-dhvV] -i E<lt>inputE<gt> -o E<lt>outputE<gt> E<lt>filterE<gt>

=head1 DESCRIPTION

msgsearch is an handy tool to extract some messages of a po file. Selection
depend on the file the message where extracted from, on the flags attached
to the message or even on their actual content. It can be seen as a
generalization of msggrep allowing you to use OR between different
categories.

The used filters are very similar to the LDAP ones:

=over

=item * (E<lt>fieldE<gt>=argument)
   
The EQ operator returns true (and thus select the message for
extraction) if and only if the value of the E<lt>fieldE<gt> is equal to the
string used as argument. The argument are Perl regular expression, so if 
you want to get an equality (and not a substring search), you have to use ^value$.

Existing fields are:

=over

=item - msgid

The actual content of the message.

=item - msgstr

The actual content of the translation.

=item - reference

The location of the message in the file where it was extracted from. It is a
space separated list of positions, each ones being of the syntax
E<gt>fileE<lt>:E<gt>line numberE<lt>.

=item - comment

Comment added manually (by the translators). 

=item - automatic

Comment automatically added by the string extraction program. See the
I<--add-comments> option of the B<xgettext> program for more information.

=item - flags

space-separated list of all defined flags for this entry.

Valid flags are: c-text, python-text, lisp-text, elisp-text, librep-text,
smalltalk-text, java-text, awk-text, object-pascal-text, ycp-text,
tcl-text, wrap, no-wrap and fuzzy.

See the gettext documentation for their meaning.

=back

=item * (!E<lt>expressionE<gt>)

The NOT operator returns true if the sub-E<lt>expressionE<gt> return false.

=item * (&E<lt>expression1E<gt>E<lt>expression2E<gt>...E<lt>expressionNE<gt>)

The AND operator returns true when all the sub-expression return true. It
return false if at least one of them return false.

=item * (|E<lt>expression1E<gt>E<lt>expression2E<gt>...E<lt>expressionNE<gt>)

The OR operator returns true when at least one of the sub-expression return
true. It return false if all of them return false. 

=head1 OPTIONS

=over 4

=item -h, --help

Show a short help message.

=item -V, --version

Displays the version of the script and exits.

=item -i, --input

Input file. By default, files are taken from the standard input.

=item -o, --output

Output file. By default, files are sent on the standard output.

=item -v, --verbose

Increase the verbosity of the program.

=back

=head1 SEE ALSO

This tool is part of the po4a project, even if it can well prove useful in
other contexts. See L<po4a(7)> for more information about the motivations of
the po4a project, and the other tools developed in that framework.

=head1 AUTHORS

 Martin Quinson (mquinson#debian.org)

=head1 COPYRIGHT AND LICENSE

Copyright 2004 by SPI, inc.

This program is free software; you may redistribute it and/or modify it
under the terms of GPL (see COPYING file).

=cut

use 5.006;
use strict;
use warnings;

use Getopt::Long qw(GetOptions);

use Locale::Po4a::Po;
use Locale::Po4a::Common;

use Pod::Usage qw(pod2usage);

textdomain('po4a');

sub show_version {
    Locale::Po4a::Common::show_version("msgsearch");
    exit 0;
}

my ($help,@verbose,$input,$output);
$help = 0;
@verbose = ();
$input = '-';
$output = '-';

Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
GetOptions(
	'input|i=s'	=> \$input,
	'output|o=s'	=> \$output,
        'help|h'        => \$help,
        'verbose|v'     => \@verbose,
        'version|V'     => \&show_version
) || pod2usage(1);
$input eq '-' || -e $input || die sprintf(gettext("msgsearch: File '%s' does not exist")."\n",$input);
my $verbose = scalar @verbose;

# Argument check
$help && pod2usage (0);
my $filter = shift(@ARGV) || pod2usage(1);


my $poin = Locale::Po4a::Po->new();
# All following function croak on problem
print STDERR gettext("Read $input")."\n" if $verbose;
$poin->read($input); 

print STDERR gettext("Filter")."\n" if $verbose;
my $poout = $poin->filter($filter);

print STDERR gettext("Write result to $output")."\n" if $verbose;
$poout->write($output);
0;
