#!/usr/bin/perl
use strict;
use warnings;

use vars qw( $VERSION );
$VERSION = "0.13";

use Getopt::Long 2.17; # support for Configure()
use Pod::WikiDoc;
use Pod::Usage;

Getopt::Long::Configure( "bundling" );
my %opts;
my $opts_ok = GetOptions( \%opts,
    "comment|c",
    "help|h",
    "length|l:i",
    "version|V"
);

if ( ! $opts_ok ) {
    pod2usage("Syntax Error.");
}

if ( $opts{help} ) {
    pod2usage( -verbose => 1 );
}

if ( $opts{version} ) {
    print "Version: wikidoc $VERSION\n"; 
    exit;
}
    
my ($input_file, $output_file) = @ARGV;

if ( ! $input_file and -t *STDIN ) {
    pod2usage("wikidoc: Syntax error.")
}

die "wikidoc: Can't use the same file for input and output.\n"
    if $input_file && $output_file && $input_file eq $output_file;

my $args = {
    comment_blocks => $opts{comment} ? 1 : 0,
    comment_prefix_length => $opts{length} ? $opts{length} : 3,
};

my $parser = Pod::WikiDoc->new( $args );

if ( $output_file ) {
    print "Extracting Pod from $input_file\n";
}

if ( defined $input_file and $input_file eq '-' ) {
    print "wikidoc: Reading from terminal:\n";
    my $buffer;
    $buffer .= $_ while <STDIN>;
    print $parser->convert( $buffer );
}
else {
    $parser->filter( { input => $input_file, output => $output_file } );
}

__END__

=head1 NAME

wikidoc - convert mixed Pod and wikidoc text to Pod

=head1 SYNOPSIS

 wikidoc [options] [inputfile] [outputfile]
 
 options: 
    --comments or -c
        include wikidoc comment-blocks
    
    --help or -h       
        give usage information
    
    --length <INTEGER> or -l <INTEGER>
        set the length of the comment block prefix, e.g. 3 means "###"
    
    --version or -V
        print the version number of this program
 
 inputfile: 
    A filename from which Pod and wikidoc will be extracted.  To read
    from STDIN, use the special name '-'.  If no filename is given, data 
    will be read from STDIN if wikidoc is receiving data from a pipe, 
    otherwise a syntax error will occur.
 
 outputfile: 
    A filename for the generated Pod.  If no filename is given, Pod
    will be written to STDOUT.

=head1 DESCRIPTION

C<wikidoc> processes Pod and wikidoc, converting the wikidoc to Pod, and then
writing the combined document back to a file or standard output. 

See documentation for L<Pod::WikiDoc> for details.

=head1 DIAGNOSTICS

C<wikidoc: Can't use the same file for input and output.>

Because an output file is clobbered, using the same input and output
file would destroy the input and produce no output.

=head1 DEPENDENCIES

=over

=item *

L<Getopt::Long>

=item *

L<Pod::Usage>

=item *

L<Pod::WikiDoc>

=back

=head1 BUGS

Please report bugs using the CPAN Request Tracker at 
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Pod-WikiDoc>

=head1 AUTHOR

David A Golden (DAGOLDEN)

dagolden@cpan.org

L<http://dagolden.com/>

=head1 COPYRIGHT

Copyright (c) 2005 by David A Golden

This program is free software; you can redistribute
it andE<sol>or modify it under the same terms as Perl itself.

The full text of the license can be found in the
LICENSE file included with this module.


