#!/usr/pkg/bin/perl -w

use strict;

use XMLTV::Version '$Id: tv_validate_file.in,v 1.4 2015/07/12 00:46:37 knowledgejunkie Exp $';
use XMLTV::ValidateFile qw/LoadDtd ValidateFile/;
use Getopt::Long;

=pod

=head1 NAME

tv_validate_file - Validate that a file contains valid xmltv data.

=head1 SYNOPSIS

tv_validate_file --help

tv_validate_grabber [--dtd-file <dtdfile>] <file>

=head1 DESCRIPTION

=head1 OPTIONS

B<--dtd <dtdfile>> Use the specified file as the xmltv dtd instead of
downloading it from the web.

=head1 AUTHOR

Mattias Holmlund, mattias -at- holmlund -dot- se.

=cut

my $opt = { "dtd-file" => undef,
            help => 0,
          };

my $res = GetOptions( $opt, qw/
                      dtd-file=s
                      help|h
                      / );

if ((not $res) or $opt->{help} or scalar( @ARGV ) != 1) {
    print << "EOHELP";
Usage: $0 [options] <file>

EOHELP

    exit 1;
}

my( $file ) = @ARGV;

if( defined( $opt->{'dtd-file'}) ) {
    if (not LoadDtd( $opt->{'dtd-file'} )) {
        print STDERR "Failed to load dtd from $opt->{'dtd-file'}.\n" .
          "Use the --dtd-file option to specify another path to the dtd.\n";
    exit 1;
  }
}

my @errors = ValidateFile( $file );

if (scalar @errors) {
    if (grep(/^not(well|valid)$/, @errors)) {
      print "The file did not validate as well-formed XML, so no further\nprocessing was performed.\n";
      exit 1;
    }

    my $errors = scalar @errors;
    print "$errors error" . ($errors > 1 ? "s" : "") . " found.\n";
    exit 1;
}
else {
    print "Validated ok.\n";
    exit 0;
}

=head1 COPYRIGHT

Copyright (C) 2005, 2007 Mattias Holmlund.

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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

=cut

### Setup indentation in Emacs
## Local Variables:
## perl-indent-level: 4
## perl-continued-statement-offset: 4
## perl-continued-brace-offset: 0
## perl-brace-offset: -4
## perl-brace-imaginary-offset: 0
## perl-label-offset: -2
## cperl-indent-level: 4
## cperl-brace-offset: 0
## cperl-continued-brace-offset: 0
## cperl-label-offset: -2
## cperl-extra-newline-before-brace: t
## cperl-merge-trailing-else: nil
## cperl-continued-statement-offset: 2
## indent-tabs-mode: t
## End:
