#!/usr/bin/perl 

use strict;
use warnings;

use Pod::Manual;
use Getopt::Long;
use Pod::Usage;
use List::MoreUtils qw/ none /;

my $VERSION = '0.08';

my @allowed_formats = qw/ latex docbook pdf /;

my $title;
my $output;
my $format      = 'pdf';
my $silent      = 0;
my $opt_help    = 0;
my $opt_man     = 0;
my $pdf_gen     = 'latex';

GetOptions(
    'title=s'        => \$title,
    'output=s'       => \$output,
    'format=s'       => \$format,
    'silent!'        => \$silent,
    'pdfgenerator=s' => \$pdf_gen,
    'man'            => \$opt_man,
    'help'           => \$opt_help,
) or pod2usage(1);

pod2usage(1) if $opt_help;
pod2usage( -verbose => 2 ) if $opt_man;

$format = lc $format;

if( none { $_ eq $format } @allowed_formats ) {
    die "format $format not allowed\n",
        "valid formats: ", join " ", @allowed_formats, "\n";
}

my $manual = Pod::Manual->new(
    title         => $title,
    pdf_generator => $pdf_gen
);

# the chapters are either passed as arguments,
# or taken from STDIN
$manual->add_chapters( @ARGV ? @ARGV : split /\r?\n/, join q{}, <> );

# figure out the output file
$output ||= 'manual.'.$format;

my $output_fh;

if ( $output eq 'stdout' ) {
    die "can't output pdf to stdout\n" if $format eq 'pdf';
    open $output_fh, '>-' or die "can't pipe to stdout: $!\n";
}
else {
    if ( $format eq 'pdf' ) {
        $output_fh = $output;   # for the sake of the common interface
    }
    else {
        open $output_fh, '>', $output or die "can't write to $output: $!\n";
    }
}

my %generator = (
    pdf     => sub { $_[0]->save_as_pdf( $_[1] ) },
    latex   => sub { print {$_[1]} $_[0]->as_latex },
    docbook => sub { print {$_[1]} $_[0]->as_docbook },
);
    
$generator{$format}->( $manual, $output_fh );

warn "document '$output' created\n" unless $silent;

exit;

__END__

=head1 NAME

podmanual - converts pods into docbook or pdf manual

=head1 SYNOPSIS

podmanual [ OPTIONS ] [ module names | pod files ] 
	
=head1 DESCRIPTION

Take the pods given as arguments and generate
a manual out of them. 

The pods can be given as module names or file names. If no
pods are passed as arguments, C<podmanual> will read them from
STDIN, assuming a format of one module name per line.

=head2 OPTIONS

=over

=item -format [ pdf | docbook | latex ]  

Output format. 'pdf' is the default.

=item -pdfgenerator [ latex | prince ]

Specifies with pdf generator to use (defaults to 'latex').

=item -output I<filename>

Filename of the generated manual. Defaults to 'manual.pdf' or
'manual.docbook', depending on the choosen format. 

If the format is 'pdf', the suffix '.pdf' will be appended to 
I<filename>, if not already present. 

If the format is 'docbook' or 'latex', the suffix '.docbook' or '.tex'
will be appended to I<filename> if no suffix is already present.  Also, the 
special filename 'stdout' can be given to have the manual printed
to STDOUT.

=item -title I<manual title>

Set the manual title.  If the option is not invoked, 
the name of the first module will be used as the title of
the manual.

=item -silent

Don't output any status message.

=item -man

Output C<podmanual>'s manpage and exit.

=item -help

Output C<podmanual>'s usage and exit.

=back

=head1 VERSION

This document describes podmanual version 0.08

=head1 SEE ALSO

L<Pod::Manual>

=head1 BUGS

Please send bug reports to <bug-pod-manual@rt.cpan.org>,
or via the web interface at 
http://rt.cpan.org/Public/Dist/Display.html?Name=Pod-Manual.

=head1 LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=head1 AUTHOR

Yanick Champoux, <yanick@cpan.org>

=cut

