#!/usr/bin/env perl

use 5.008;

use strict;
use warnings;

use Getopt::Long 2.33 qw{ :config auto_version };
use Pod::Usage;
use Test::More 0.88;
use Test::Pod::LinkCheck::Lite qw{ :const };

our $VERSION = '0.008';

my %opt = (
    ignore_url	=> [],
);
my $dump;

GetOptions( \%opt,
    qw{
	allow_redirect_to_index|allow-redirect-to-index!
	check_url|check-url! ignore_url|ignore-url=s@ man!
	prohibit_redirect|prohibit-redirect!
    },
    'dump!'	=> \$dump,
    help	=> sub { pod2usage( { -verbose => 2 } ) },
) and 1 >= @ARGV or pod2usage( { -verbose => 0 } );

foreach ( @{ $opt{ignore_url} } ) {
    m| \A < ( .* ) > ( [[:lower:]]* ) \z |smx
	or next;
    $_ = eval "qr$_"
	or die $@;
}

if ( delete $opt{allow_redirect_to_index} ) {
    $opt{prohibit_redirect} = ALLOW_REDIRECT_TO_INDEX;
}

my $tp = Test::Pod::LinkCheck::Lite->new( %opt );

if ( ! @ARGV || -d $ARGV[0] ) {
    $tp->all_pod_files_ok( @ARGV );
} elsif ( -f _ ) {
    $tp->pod_file_ok( $ARGV[0] );
} else {
    $tp->pod_file_ok( \"=pod\n\n$ARGV[0]" );
}

if ( $dump ) {
    # The following require() is strictly pro forma, since
    # Test::Pod::LinkCheck::Lite has already loaded it.
    # The encapsulation violations in the following are UNSUPPORTED.
    require Test::Builder;
    my $TEST = Test::Builder->new();
    $TEST->diag( '' );
    $TEST->diag( 'Sections' );
    $TEST->diag( "    $_" ) for sort keys %{ $tp->{_section} };
    $TEST->diag( '' );
    $TEST->diag( 'Links' );
    require YAML;
    $TEST->diag( YAML::Dump( [ $tp->{_links} ] ) );
}

done_testing;

__END__

=head1 TITLE

test-pod-links - Test a POD file, or literal POD.

=head1 SYNOPSIS

 test-pod-links
 test-pod-links fubar.pm
 test-pod-links 'L<Foobar|Foo/bar>'
 test-pod-links -help
 test-pod-links -version

=head1 OPTIONS

=head2 -allow-redirect-to-index

This Boolean option implies L<-prohibit-redirect|/-prohibit-redirect>,
but sets the corresponding attribute to
L<ALLOW_REDIRECT_TO_INDEX|Test::Pod::LinkCheck::Lite/ALLOW_REDIRECT_TO_INDEX>.

=head2 -check-url

This Boolean option specifies that World Wide Web access is allowed. If
disallowed, URL links are  not checked, and the CPAN Meta database is
not considered when trying to check links to ininstalled modules.

The default is C<-check_url>, but this option can be negated by specifying
C<-nocheck-url>.

=head2 -dump

If asserted, this Boolean option dumps selected object state after the
test has been run. This option is unsupported, and may be changed or
retracted without notice.

As of this writing, it displays all the sections in the last file
parsed, in ASCIIbetical order.

=head2 -help

This option displays the documentation for this script. The script then
exits.

=head2 -ignore-url

 -ignore-url http://www.google.com/
 -ignore-url <//www.google.com/>i

This option specifies a URL to ignore. It can be specified more than
once to ignore more than one URL. If the value begins with an open angle
bracket and ends with a close angle bracket followed by zero or more
lower-case letters, it is stringy-eval-ed into a Regexp.

=head2 -man

If this Boolean option is asserted, C<man> links will be tested if the
OS is capable of supporting this. If it is negated (as C<-noman>), such
links will not be tested.

The default is true if the host system has a C<man> command, and false
otherwise.

=head2 -prohibit-redirect

This Boolean option fails URL links that redirect to a different URL.

=head2 -version

This option displays the version of this script. The script then exits.

=head1 DETAILS

This Perl script tests the POD specified as its first and only command
line argument. This can be either the name of a file, or literal POD. If
the argument is the name of a directory, all Perl files in that
directory are tested. If no argument is specified, all Perl files in
F<blib/> are tested.

=head1 AUTHOR

Thomas R. Wyant, III F<wyant at cpan dot org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2019-2021 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the directory LICENSES.

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.

=cut

# ex: set textwidth=72 :