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

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

use Getopt::Lucid ':all';
use Pod::WikiDoc;
use Pod::Usage;

my @spec = (
    Switch("comment|c"),
    Param("length|l")->default(3),
    Keypair("define|d"),
    Switch("help|h"),
    Switch("version|V"),
);
        
my $opts;

eval { $opts = Getopt::Lucid->getopt( \@spec ) };

if ($@) {
    if ( ref $@ && $@->isa("Getopt::Lucid::Exception::ARGV") ) {
        pod2usage("Syntax Error.");
    }
    else {
        die $@;
    }
}

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

if ( $opts->get_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->get_comment ? 1 : 0,
    comment_prefix_length => $opts->get_length,
    keywords => { $opts->get_define },
};

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
    
    --define <KEYWORD=VALUE> or -d <KEYWORD=VALUE>
        define keyword for expansion
        
    --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::Lucid>

=item *

L<Pod::Usage>

=item *

L<Pod::WikiDoc>

=back

=head1 BUGS

Please report bugs or feature requests using the CPAN Request Tracker.
Bugs can be sent by email to C<<< bug-Pod-WikiDoc@rt.cpan.org >>> or
submitted using the web interface at
L<http://rt.cpan.org/Public/Dist/Display.html?Name=Pod-WikiDoc>

When submitting a bug or request, please include a test-file or a patch to an
existing test-file that illustrates the bug or desired feature.

=head1 AUTHOR

David A. Golden (DAGOLDEN)

dagolden@cpan.org

http:E<sol>E<sol>dagolden.comE<sol>

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2005,2006 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.

=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS ANDE<sol>OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY ANDE<sol>OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.

