#!/usr/bin/perl

##  Copyright (c) 1997-2001 Ivan Kurmanov. All rights reserved.
##
##  This program is free software; you can redistribute it and/or modify it
##  under the same terms as Perl itself.

$VERSION = "0.1"; ###  $Id$



BEGIN {

#    $ProgrammName =        "rere";
#    $ProgrammVersion =     "2.02";
#    $ProgrammDescription = "Filter and display .rdf (ReDIF) files";
#    $ProgrammAuthor =      "Ivan Kurmanov";
#    $ProgrammAuthorEmail = "kurmanov\@openlib.org";

#    use ReDIF::init;
#    ReDIF::initialize( { silent_init => '1' } );


#    $RedifDir = $ReDIF::CONFIG{redif_home};

}


##########################################################################

##########################################################################
# Why not insert 'use' statements here.
#
# Standard Library

use Getopt::Std;
use strict;
use vars qw( $opt_c $opt_u $opt_m $opt_x $opt_w $opt_e $opt_d $opt_I $opt_C );

# other libraries


# Own-written

use ReDIF::Parser qw( &redif_open_file 
		      &redif_get_next_template 
		      &redif_get_next_template_good_or_bad
		      ) ;

##############################################################################

getopts( 'ucm:xewdIC' ) || die "bad options given to the program. please see rere manpage";


my $file;

my $x_attributes_mode = $opt_x;
my $unicode_mode = $opt_u;

my $checker_mode = 0;
my $message_threshold = 5;   ### performance 

if( $opt_e ) {
    $opt_m = 3;
} elsif ( $opt_w ) {
    $opt_m = 2;
} elsif ( $opt_d ) {
    $opt_m = 0;
}

if ( $opt_c ) {
    my $ml = defined ( $opt_m ) ? $opt_m : 2 ;
    $message_threshold = $ml;
    $checker_mode = 1;
} 

if( $opt_I ) {  $ReDIF::Parser::Input::DEBUG = 1; } 
if( $opt_C ) {  $ReDIF::Parser::Core::DEBUG = 1;  } 

my %Options = (
	       'build_template_hash' => 0,
	       'build_good_template_text' => ( not $checker_mode ),
	       'quote_source'  => ($checker_mode) ? 1 : 0 ,
	       'x_attributes' => $x_attributes_mode,
	       'utf8_output' => $unicode_mode,
	       'message_threshold' => $message_threshold,
	       'use_parser_input' => 1,
);

ReDIF::Parser::redif_set_parser_options( %Options ) ;

##########################################################################
##########################################################################


foreach $file ( @ARGV ) {

    if ( not redif_open_file ( $file ) ) {
	print "ERROR: can't open file '$file'\n";
	next ;
    } else {
	print "\n";
    } 
    

    if( $checker_mode ) {
	print $file , ":\n";
	my $t = 1;
	my @data ;
	while ( $t ) {
	    $t = redif_get_next_template_good_or_bad ();
	    if( $t and  $t->{MESSAGES} ) {
		print $t->{REPORT}, "\n";
	    }
	}

    } else {
	my $t = 1;
	my @data ;
	while ( $t ) {
	    $t = redif_get_next_template ();
	    if( $t ) {
		print $t->{TEXT} , "\n";
	    }
	}

    }
    
}

1;

#############################################################

#############################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#############################################################

__END__

=head1 NAME

rere -- ReDIF reading tool, a ReDIF templates filter

=head1 SYNOPSIS 

rere [-u] [-c] [-e | -w | -d | -m level] filename1 [filename2 ...]

=head1 DESCRIPTION

rere is a simple ReDIF processing tool, which is not used widely 
these days, but still may be useful.  

It simply reads the ReDIF file (or files), whose name(s) you give on
the command line, and outputs its content to the standard output in a
kind of canonical ReDIF form.  This first of all means that the ReDIF
templates in the file are checked during this processing and only
those templates which have no errors are printed out.

If rere can't open a specified file, it prints out an error message
starting with "Error:" string.

The checking that is done to the data is based upon special ReDIF
specification file and is equivalent to the B<rech> checking. 

In addition to checking & filtering out errorneous templates, 
the data is reformatted with the following rules applied: 

=over 4

=item * new-line is printed before each template

=item * attribute name is converted to lowercase

=item * multi-line values are joined into a string without line-break characters

=item * each attribute-value pair printed as 'attribute: value'

=item * values of some attributes are automatically changed

Some value types, defined in ReDIF, accept values in a number of 
formats, but the ReDIF parser converts all valid formats to a standard
form, e.g. "Creation-Date: 199909" will be printed out as 
"creation-date: 1999-09"

=back

In other words, with rere you can get a look of a set of templates as
it would get to an application using ReDIF-perl suite (rr.pm). 
(Although you can't see the exact structure in which it would
be delivered to the application - clusters and so on.)

=head2 Command-line options

You can combine together options on the command line, e.g. "-uc"

=over 4

=item -u

This option would make rere to output the data in utf-8 encoding,
which makes it usable for Unicode ReDIF files.

=item -c

This makes rere run in a special "checker" mode.  This mode is a rech
replacement, it actually hides (doesn't print out) valid templates,
instead printing out the error messages and template context of the
errors, if any.

In this mode, before opening file X rere will print "X:" and a
newline.  (By default, even if given several filenames on the command
line, it doesn't print anything else than the templates themselves.
So you can pipe rere's output to a new "clean" ReDIF file.)

=item -m level 

This option makes sense only with the -c option, and allows user to
specify so called, minimal message level.  By default, rere in
"checker" mode would display both errors and warnings (which
corresponds to the level value 2).   With this option you can make
rere only output errors: just give it the "-m 3" option, i.e. set the
minimal message level to 3.  Other levels may only be useful for
debugging purposes.

=item -w 

equivalent to '-m 2' option

=item -e

equivalent to '-m 3' option

=item -d 

equivalent to '-m 0' option.  Will produce a lot of debugging messages.

=back

=head1 AUTHOR

Ivan Kurmanov, for the RePEc project

=cut
