#!/usr/bin/perl
#                   -- ; # -*- mode: perl -*- 

=head1 NAME

amfch -- utility to parse and check AMF data files

=head1 SYNOPSIS

amfch [options] filename ...

=head1 DESCRIPTION

Reads AMF files and reports errors in it and how many records extracted from
each file.

=head2 OPTIONS

=head3 C<-d>

Dump every parsed record structure by Data::Dumper.  For debugging and
understanding the AMF::Record structure.

=head3 C<-s>

After parsing a record generate and print out reconstructed AMF/XML
representation of it.  Not guaranteed to be well-formed, not guraranteed to be
equivalent.  May not work for unusual data.  Alpha-quality feature; please
report problems.

=head3 C<-c>

After parsing a record, print out its MD5 checksum.

=head3 C<-o>

After parsing a record, convert it to a ReDIF template hash structure and dump
out the result as a ReDIF template.

*   *   *

Options can be combined into a single switch as in C<"-so">.

=head1 SEE ALSO 

L<AMF::Parser>, L<AMF::Record>, L<AMF::2ReDIF>

AMF is specified at http://amf.openlib.org/doc/amf.html

=head1 AUTHOR

Ivan Kurmanov, http://www.ahinea.com/en/
for ACIS project, http://acis.openlib.org/

=cut


use strict;
use Getopt::Std;
use AMF::Parser;

use vars qw( $opt_d $opt_s $opt_c $opt_o );

getopts( 'dsco' );

$AMF::Parser::parser -> {REPORT} = 1;

if ( $opt_d or $opt_o ) {
  require Data::Dumper;
}

if ( $opt_o ) {
  require AMF::2ReDIF;
}

foreach ( @ARGV ) {
  print "file $_:\n";
  my $open = amf_open_file( $_ );

  if ( not $open ) {
    print "\tfailed to open\n";
    next;
  }

  my $count = 0;
  my $r;
  while ( $r = amf_get_next_record() ) {
    $count++;
    if ( $opt_d ) {
      print Data::Dumper::Dumper( $r );       
    }
    if ( $opt_s ) {
      print $r->stringify, "\n";       
    }
    if ( $opt_c ) {
      print "checksum: ", $r->md5checksum, "\n";       
    }
    if ( $opt_o ) {
      my $t = AMF::2ReDIF::translate( $r );
      print Data::Dumper::Dumper( $t );
    }
  }

  print "\t$count record", ( $count>1 ) ? "s" : "", "\n";
}

