#!/usr/bin/perl -w

##########################################
#                                        #
##           for documentation          ##
###               execute:             ###
####          % perldoc pod2hpp       ####
###          to get the built-in       ###
##              docs rendered           ##
#                                        #
##########################################

use strict;
$|=1;

# I was told that it doesn't run under perl5.004 mainly because of
# s/XXX/YYY/ for @foo 5.005-isms. It's about a time you should upgrade
# to 5.005 :) 5.6 is out!!!
# require 5.005; 

# allow to execute this script from any path and not only from the current
# directory
#use FindBin qw($Bin);
#use lib $Bin;
#use Cwd ();
#my $orig_dir = Cwd::cwd;
#chdir $Bin;

use Carp;

BEGIN{
    my $config_file = pop @ARGV;
    croak ("!!! Target configuration file is missing\n!!! Must specify the configuration file as a last argument\n"), exit
	unless -e $config_file and -r _;
    use Pod::HtmlPsPdf::Config ();
    Pod::HtmlPsPdf::Config::set_config_file($config_file);
}

use Getopt::Std;

use Pod::HtmlPsPdf::RunTime ();
use Pod::HtmlPsPdf::Book ();
my $config = Pod::HtmlPsPdf::Config->new();

######################################
### Init Command Line Arguments
######################################

# set defaults if no options given
my $verbose        = 0;  # verbose?
my $podify_items   = 0;  # podify pseudo-pod (s/^* /=item */)
my $split_html     = 0;  # create the splitted html version
my $make_tgz       = 0;  # create the rel and bin+src archives?
my $generate_ps    = 0;  # generate PS file
my $generate_pdf   = 0;  # generate PDF file
my $rebuild_all    = 0;  # ignore the timestamp of ../src/.last_modified
my $print_anchors  = 0;  # print the available anchors
my $validate_links = 0;  # validate %links_to_check against %valid_anchors
my $makefile_mode  = 0;	 # executed from Makefile (forces rebuild, no
                         # PS/PDF file, no tgz archive created!)
my $slides_mode    = 0;

######################################
### Process Command Line Arguments
######################################

my %opts;
getopts('hvtpdfalmise', \%opts);

usage() if $opts{h};

if (keys %opts) {   # options given
  $verbose        = $opts{v} || 0;
  $podify_items   = $opts{i} || 0;
  $split_html     = $opts{s} || 0;
  $make_tgz       = $opts{t} || 0;
  $generate_ps    = $opts{p} || 0;
  $generate_pdf   = $opts{d} || 0;
  $rebuild_all    = $opts{f} || 0; # force
  $print_anchors  = $opts{a} || 0;
  $validate_links = $opts{l} || 0;
  $slides_mode    = $opts{e} || 0;
  $makefile_mode  = $opts{m} || 0;
}

if ($makefile_mode) {
  $verbose        = 1;
  $make_tgz       = 0;
  $generate_ps    = 0;
  $generate_pdf   = 0;
  $rebuild_all    = 1;
  $print_anchors  = 0;
  $validate_links = 0;
}

# in the slides mode we turn preprocessing automatically to be on
if ($slides_mode) {
  $podify_items = 1;
}

  # we need a PS version in order to create a pdf
$generate_ps = 1 if $generate_pdf;

  # verify the ability to create PS version
$generate_ps  = Pod::HtmlPsPdf::RunTime::can_create_ps if $generate_ps;

  # verify the ability to create PDF version
$generate_pdf = Pod::HtmlPsPdf::RunTime::can_create_pdf if $generate_pdf;

  # we cannot create PDF if we cannot generate PS
$generate_pdf = 0 unless $generate_ps;

  # if there is no toc_file we cannot produce correct internal links,
  # therefore we force this option.
my $toc_file = $config->get_param('toc_file');
$rebuild_all = 1,
  print "!!! No $toc_file was found, forcing complete rebuild\n"
    unless -e $toc_file or $rebuild_all;

my %options =
  (
   verbose        => $verbose,
   podify_items   => $podify_items,
   split_html     => $split_html,
   make_tgz       => $make_tgz,
   generate_ps    => $generate_ps,
   generate_pdf   => $generate_pdf,
   rebuild_all    => $rebuild_all,
   print_anchors  => $print_anchors,
   validate_links => $validate_links,
   makefile_mode  => $makefile_mode,
   slides_mode    => $slides_mode,
  );

# make the runtime options available to other packages
Pod::HtmlPsPdf::RunTime::set_runtime_options(\%options);

######################################
### Create the Book
######################################

  # create the book object
my $book = Pod::HtmlPsPdf::Book->new();

  # process the source files and convert them into html files write
  # two copies of the files -- one as normal html using the html
  # teplates, and the other again in html but using the PS templates,
  # since generally these don't need TOC and all the other stuff,
  # html2ps generates this for us automatically.
$book->create_html_version();

  # Validate pod's L<> links
$book->validate_links if $validate_links;

  # when there are broken links reported by validate_links() we need
  # to know the correct links as rendered by pod2html converter. This
  # will print all the available achors.
$book->print_anchors if $print_anchors;

  # generate the PS version of the book
$book->create_ps_version if $generate_ps;

  # generate the PDF version of the book
$book->create_pdf_version if $generate_pdf;

  # generate the Split HTML version of the book
$book->create_split_html_version if $split_html;

  # build the dist
$book->make_tar_gz() if $make_tgz;

  # do the cleanup chores
$book->cleanup();

  # go back to where you have from
#chdir $orig_dir;

######################################
### Subs
######################################

##########
sub usage{

  print <<USAGE;
    pod2hpp [options] config_file_location

Options:

  -h    this help
  -v    verbose
  -i    podify pseudo-pod items (s/^* /=item */)
  -s    create the splitted html version
  -t    create tar.gz
  -p    generate PS file
  -d    generate PDF file
  -f    force a complete rebuild
  -a    print available hypertext anchors
  -l    do hypertext links validation
  -e    slides mode (for presentations)
  -m    executed from Makefile (forces rebuild,
				no PS/PDF file,
				no tgz archive!)

USAGE

  exit;

} # end of sub usage



__END__

=head1 NAME

pod2hpp - a script that does documentation projects building in HTML, PS and PDF formats

=head1 SYNOPSIS

  pod2hpp [options] configuration_file_location

=head1 DESCRIPTION

See C<Pod::HtmlPsPdf>

=head1 AUTHOR

Stas Bekman <stas@stason.org>

=head1 COPYRIGHT

This program is distributed under the Artistic License, like the Perl
itself.

=cut


=cut
