#!perl
#
# Generate human-readable documentation from package metadata.
#
# This is the driver program for the DocKnot documentation generation system.
# As much logic as possible should live in App::DocKnot rather than in this
# script.  It should be a thin wrapper.

use 5.018;
use autodie;
use warnings;

use App::DocKnot;
use Getopt::Long qw(GetOptions :config bundling);
use Pod::Usage qw(pod2usage);

# Parse options.
my @options = qw(help|h metadata|m=s output|o=s template|t=s);
my %options;
GetOptions(\%options, @options) or exit 1;
if ($options{help}) {
    pod2usage(0);
}
for my $required (qw(metadata output template)) {
    if (!exists $options{$required}) {
        die "$0: missing required option --$required\n";
    }
}

# Initialize and run DocKnot.
my $docknot = App::DocKnot->new({ metadata => $options{metadata} });
open(my $outfh, '>', $options{output});
print {$outfh} $docknot->generate($options{template})
  or die "$0: cannot write to $options{output}: $!\n";
close($outfh);

__END__

=for stopwords
Allbery DocKnot docknot MERCHANTABILITY NONINFRINGEMENT sublicense

=head1 NAME

docknot - Generate human-readable documentation from package metadata

=head1 SYNOPSIS

B<docknot> [B<-h>] B<-m> I<metadata> B<-o> I<output> B<-t> I<template>

=head1 DESCRIPTION

B<docknot> uses metadata files and templates to generate human-readable
documentation files for a package.  It is a small driver script around
App::DocKnot.  See L<App::DocKnot> for documentation on the metadata format,
configuration files, and paths.

=head1 OPTIONS

=over 4

=item B<-h>, B<--help>

Print out usage information and exit.

=item B<-m> I<metadata>, B<--metadata>=I<metadata>

The path to the metadata files for the package whose documentation is being
generated.  This should be a directory containing all the package metadata
files required by App::DocKnot.  This option is required.

=item B<-o> I<output>, B<--output>=I<output>

The path to the output file to generate.  This option is required.

=item B<-t> I<template>, B<--template>=I<template>

The template to use when generating the output file.  This option is required.

=back

=head1 DIAGNOSTICS

If B<docknot> fails with errors, see L<App::DocKnot> for information about
what those errors might mean.  Internally, it can also produce the following
diagnostics:

=over 4

=item cannot write to %s: %s

(F) The output file specified with B<-o> could not be written to.

=item missing required option %s

(F) One of the required command-line options was not given.

=back

In addition, other L<Getopt::Long> error messages may result from invalid
command-line options.

=head1 AUTHOR

Russ Allbery <rra@cpan.org>

=head1 COPYRIGHT AND LICENSE

Copyright 2016 Russ Allbery <rra@cpan.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=head1 SEE ALSO

L<App::DocKnot>

This program is part of the DocKnot distribution.  DocKnot is available from
CPAN as App::DocKnot, or directly from its web site at
L<https://www.eyrie.org/~eagle/software/docknot/>.

=cut
