#!/usr/local/bin/perl

use strict;
use warnings;

our $VERSION = '0.01';

use Getopt::Long;
use Pod::2::DocBook;
use Pod::Usage;
use List::MoreUtils 'none';

exit main();

sub main {
    my $doctype = 'section';
    my $spaces  = 2;
    my ($title, $double_quotes, $help, $no_header, $base_id);

    GetOptions(
        'doctype=s'         => \$doctype,
        'base-id=s'         => \$base_id,
        'title=s'           => \$title,
        'spaces=i'          => \$spaces,
        'fix-double-quotes' => \$double_quotes,
        'no-header'         => \$no_header,
        'help'              => \$help,
    ) or pod2usage();

    pod2usage if $help;
    pod2usage if none { $doctype eq $_ } qw(article chapter refentry section);
    pod2usage unless $spaces =~ /^\d+$/xms;

    # TODO refactor this if so that there are no the same patterns in each of the branch
    if (@ARGV == 0) {
        $title = q{} unless defined $title;
        my $parser = Pod::2::DocBook->new(
            title             => $title,
            doctype           => $doctype,
            base_id           => $base_id,
            fix_double_quotes => $double_quotes,
            header            => !$no_header,
            spaces            => $spaces
        );
        $parser->parse_from_filehandle(\*STDIN);
    }

    else {
        my ($infile, $outfile) = @ARGV;
        
        # default doctype element id is the name of the input file
        if (!$base_id) {
            $base_id = $infile;
            $base_id =~ tr{/ }{._};
            $base_id =~ s/[^.A-Za-z_:-]//g;
            
        }
        
        # check if the id is valid (SEE http://www.w3.org/TR/2000/REC-xml-20001006#NT-Name)
        # TODO refactor to the function, we will need if also later and some tests will be handfull
        #      we should also "die" if the base_id is set through the command line parameter
        if ($base_id !~ m/^[A-Za-z_:] [-A-Za-z0-9_.:]*/xms) {
            warn 'wrong base_id "', $base_id, '", throwing away';
            $base_id = undef;
        }

        ($title) = $infile =~ m{([^/]+)\z}xms unless defined $title;
        my $parser = Pod::2::DocBook->new(
            title             => $title,
            doctype           => $doctype,
            base_id           => $base_id,
            fix_double_quotes => $double_quotes,
            header            => !$no_header,
            spaces            => $spaces
        );

        if (defined $outfile) {
            $parser->parse_from_file($infile, $outfile);
        }

        else {
            $parser->parse_from_file($infile);
        }
    }

    return 0;
}

__END__

=head1 NAME

pod2docbook - Convert POD data to DocBook SGML

=head1 SYNOPSIS

pod2docbook [B<--help>]
[B<--doctype>=B<article>|B<chapter>|B<section>|B<refentry>]
[B<--title>=I<title>] S<[B<--spaces>=I<# spaces per indent level>]>
[B<--fix-double-quotes>] [B<--no-header>]
[B<--base-id>=I<idstring>]
S<[I<infile> [I<outfile>]]>

=head1 DESCRIPTION

B<pod2docbook> converts files from pod format (see L<perlpod>) to
DocBook 4.2 SGML (see L<http://www.docbook.org/>).  The program itself
is merely a driver for the Pod::2::DocBook class; if you're interested in
details of pod-to-SGML translation see L<Pod::2::DocBook>.

=head1 OPTIONS AND ARGUMENTS

=over

=item [B<--help>]

Print usage and exit.

=item [B<--doctype>=B<article>|B<chapter>|B<section>|B<refentry>]

Specifies the document type for the output file; the default is
B<section>.

=item [B<--title>=I<title>]

Specifies the document title.  The default is I<infile>, if it is
supplied, or empty string otherwise.

=item [B<--spaces>=I<# spaces per indent level>]

Specifies the number of spaces per indent level in the SGML output;
the default is 2.

=item [B<--fix-double-quotes>]

Replace pairs of double quotes in regular paragraphs with <quote> and
</quote> (see L<Pod::2::DocBook> for details).

=item [B<--no-header>]

Omit the DOCTYPE line from the output.

=item I<infile>

The name of the file from which to read pod source; if not supplied,
STDIN is used for input.

=item I<outfile>

The name of the file to which to write SGML; if not supplied, STDOUT
is used for output.

=item [B<--base-id>=I<idstring>]

The root element of the B<--doctype> will have the I<idstring> set for attribute I<id>.
The default is an input file name "cleaned up" to conform with XML restriction for
characteds used in id strings. (SEE L<http://www.w3.org/TR/2000/REC-xml-20001006#NT-Name>)

=back

=head1 SEE ALSO

L<pod2docbook>, L<perlpod>, L<Pod::DocBook>,
SVN repo - L<https://cle.sk/repos/pub/cpan/Pod-2-DocBook/>,
L<http://www.ohloh.net/projects/pod-2-docbook>,
F<doc/> + F<examples/pod2docbook-docbook/> for Pod::2::DocBook
DocBook documentation

DocBook related links: L<http://www.docbook.org/>,
L<http://www.sagehill.net/docbookxsl/>,
L<http://developers.cogentrts.com/cogent/prepdoc/pd-axfrequentlyuseddocbooktags.html>

=head1 AUTHOR

Alligator Descartes <descarte@symbolstone.org> wrote a module called
Pod::2::DocBook, which was later maintained by Jan Iven
<jan.iven@cern.ch>.  That module was based on the original L<pod2html>
by Tom Christiansen <tchrist@mox.perl.com>.

Nandu Shah <nandu@zvolve.com> wrote Pod::DocBook, which is
unrelated to the previous module (even though they both perform the
same function). (L<http://search.cpan.org/~nandu/Pod-DocBook-1.2/>)

Jozef Kutej <jkutej@cpan.org> renamed the module to Pod::2::DocBook
because Nandus version was buried in the CPAN archive as an
"UNAUTHORIZED RELEASE".

=head1 COPYRIGHT

Copyright 2004, Nandu Shah <nandu@zvolve.com>

Copyright 2008, Jozef Kutej <jkutej@cpan.org>

This library is free software; you may redistribute it and/or modify
it under the same terms as Perl itself

=cut
