#!/usr/bin/env perl

# Copyright (C) 2005  Joshua Hoblitt
#
# $Id: mdc-dump,v 1.3 2007-11-10 00:40:08 jhoblitt Exp $

use strict;
use warnings FATAL => qw( all );

use vars qw( $VERSION );
$VERSION = '0.02';

use Data::Dumper qw( Dumper );
use PS::IPP::Metadata::Config;

use Getopt::Long qw( GetOptions :config auto_help auto_version );
use Pod::Usage qw( pod2usage );

my ( $file, $overwrite, $verbose );
GetOptions(
    'overwrite' => \$overwrite,
    'verbose'   => \$verbose,
) || pod2usage( 2 );

$file = shift;

pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;

# open STDIN if no file was specified
$file = '-' unless $file;

if (defined $verbose) {
    $::RD_TRACE = 1;
    $::RD_HINT = 1;
}
my $md_parser = PS::IPP::Metadata::Config->new;

if ( $overwrite ) {
   $md_parser->overwrite( 1 );
} else {
   $md_parser->overwrite( 0 );
}

open( my $md, $file ) or die "can't open file: $!";

my $data = $md_parser->parse( do { local $/; <$md> } );

close( $md );

if ( $data ) {
    print Dumper( $data );
} else {
    warn "Invalid Syntax, Parse failed.\n";
    exit 1;
}

__END__

=pod

=head1 NAME

mddump.pl - validate psMetadataConfig files

=head1 SYNOPSIS

    mddump.pl [--overwrite] [--verbose] <filename>

    or

    cat <filename> | mddump.pl [--overwrite] [--verbose]

    or

    mddump.pl [--help | -h | -? | --version]

=head1 DESCRIPTION

Parses psMetadataConfig formatted text either from a file or STDIN and prints a
Perl data structure that represents the metadata.

=head1 OPTIONS

=over 4

=item * <filename>

Accepts the name of a file to use as input.  If this option is omitted the
STDIN will be read from.

=item * --overwrite | -o

A flag that turns L<PS::IPP::Metadata::Config>'s overwrite behavior on.
Overwrite is always off unless this flag is specified.

=item * --verbose | -v

A flag that turns L<Parse::RecDescent>'s C<RD_TRACE> and C<RD_HINT> tracing
flags.  verbose is always off unless this flag is specified.

=item * --help | -h | -?

Prints usage information.

=item * --version

Prints the version.

=back

=head1 CREDITS

Just me, myself, and I.

=head1 SUPPORT

Please contact the author directly via e-mail.

=head1 AUTHOR

Joshua Hoblitt <jhoblitt@cpan.org>

=head1 COPYRIGHT

Copyright (C) 2004  Joshua Hoblitt.  All rights reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA  02111-1307, USA.

The full text of the license can be found in the L<perlgpl> Pod as supplied
with Perl 5.8.1 and later.

=head1 SEE ALSO

L<PS::IPP::Metadata::Config>

=cut
