#!/usr/bin/perl -w

# bib2html script to generate an HTML document for BibTeX database
# Copyright (C) 1998-06  Stephane Galland <galland@arakhne.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

use strict ;
use Getopt::Long ;
use Pod::Usage ;
use File::Basename ;
use File::Spec ;
use File::Path ;

use lib '/usr/share/bib2html/lib';

use Bib2HTML::Release ;
use Bib2HTML::General::Verbose ;
use Bib2HTML::General::Error ;
use Bib2HTML::General::Misc ;
use Bib2HTML::Parser::Parser ;

#------------------------------------------------------
#
# Global vars
#
#------------------------------------------------------

my $PERLSCRIPTDIR = '/usr/share/bib2html';
# Version number of bib2html
my $VERSION = Bib2HTML::Release::getVersionNumber() ;
# Date of this release of bib2html
my $VERSION_DATE = Bib2HTML::Release::getVersionDate() ;
# URL from which the users can submit a bug
my $SUBMIT_BUG_URL = Bib2HTML::Release::getBugReportURL() ;
# Email of the author of bib2html
my $AUTHOR = Bib2HTML::Release::getAuthorName() ;
# Email of the author of bib2html
my $AUTHOR_EMAIL = Bib2HTML::Release::getAuthorEmail() ;
# Page of bib2html
my $URL = Bib2HTML::Release::getMainURL() ;
# Contributors to Bib2HTML
my %CONTRIBUTORS = Bib2HTML::Release::getContributors() ;

# Default Generator
my $DEFAULT_GENERATOR = 'HTML' ;
# Default Language
my $DEFAULT_LANGUAGE = 'English' ;
# Default Theme
my $DEFAULT_THEME = 'Simple' ;

# Command line options
my %options = () ;

#------------------------------------------------------
#
# Main program
#
#------------------------------------------------------

# Read the command line
$options{warnings} = 1 ;
$options{genphpdoc} = 1 ;
$options{generator} = "$DEFAULT_GENERATOR" ;
$options{lang} = "$DEFAULT_LANGUAGE" ;
$options{theme} = "$DEFAULT_THEME" ;
$options{genparams} = {} ;
$options{'show-bibtex'} = 1 ;
Getopt::Long::Configure("bundling") ;
if ( ! GetOptions( "b|bibtex!" => \$options{'show-bibtex'},
		   "checknames" => \$options{'check-names'},
		   "doctitle=s" => \$options{'title'},
		   "f|force" => \$options{'force'},
		   "generator|g=s" => \$options{'generator'},
		   'generatorparam|d:s%' => sub {
		     my $name = lc($_[1]) ;
		     @{$options{'genparams'}{"$name"}} = ()
		       unless ( exists $options{'genparams'}{"$name"} ) ;
		     push @{$options{'genparams'}{"$name"}}, $_[2] ;
		   },
		   "generatorparams!" => \$options{'genparamlist'},
		   "genlist" => \$options{'genlist'},
		   "h|?" => \$options{'help'},
		   "help|man|manual" => \$options{'manual'},
		   "lang=s" => \$options{'lang'},
		   "langlist" => \$options{'langlist'},
		   "o|output=s" => \$options{'output'},
		   "p|preamble=s" => \$options{'tex-preamble'},
		   "q" => \$options{'quiet'},
		   "sortw!" => \$options{'sort-warnings'},
		   "texcmd" => \$options{'tex-commands'},
		   "theme=s" => \$options{'theme'},
		   "themelist" => \$options{'themelist'},
		   "v+" => \$options{'verbose'},
		   "version" => \$options{'version'},
		   "warning!" => \$options{'warnings'},
		   "windowtitle=s" => \$options{'wintitle'},
		 ) ) {
  pod2usage(2) ;
}

# Generator class
if ( $options{'generator'} !~ /::/ ) {
  $options{'generator'} = "Bib2HTML::Generator::".$options{'generator'}."Gen" ;
}
eval "require ".$options{'generator'}.";" ;
if ( $@ ) {
  Bib2HTML::General::Error::syserr( "Unable to find the generator class: ".$options{'generator'}."\n$@\n" ) ;
}

# Show the version number
if ( $options{version} ) {
  print "bib2html $VERSION, $VERSION_DATE\n" ;
  print "Copyright (c) 1998-05, $AUTHOR <$AUTHOR_EMAIL>, under GPL\n" ;
  print "Contributors:\n" ;
  while ( my ($email,$name) = each(%CONTRIBUTORS) ) {
    print "  $name <$email>\n" ;
  }
  exit 1 ;
}

# Show the list of generators
if ( $options{genlist} ) {
  use Bib2HTML::Generator::AbstractGenerator ;
  Bib2HTML::Generator::AbstractGenerator::display_supported_generators($PERLSCRIPTDIR,
								       "$DEFAULT_GENERATOR") ;
  exit 1 ;
}

# Show the list of languages
if ( $options{langlist} ) {
  use Bib2HTML::Generator::AbstractGenerator ;
  Bib2HTML::Generator::AbstractGenerator::display_supported_languages($PERLSCRIPTDIR,
								      "$DEFAULT_LANGUAGE") ;
  exit 1 ;
}

# Show the list of themes
if ( $options{themelist} ) {
  use Bib2HTML::Generator::AbstractGenerator ;
  Bib2HTML::Generator::AbstractGenerator::display_supported_themes($PERLSCRIPTDIR,
								   "$DEFAULT_THEME") ;
  exit 1 ;
}

# Show the list of themes
if ( $options{'tex-commands'} ) {
  use Bib2HTML::Translator::TeX ;
  Bib2HTML::Translator::TeX::display_supported_commands($PERLSCRIPTDIR) ;
  exit 1 ;
}

# Show the list of generator params
if ( $options{'genparamlist'} ) {
  ($options{'generator'})->display_supported_generator_params() ;
  exit 1 ;
}

# Show the help screens
if ( $options{manual} ) {
    system( "perldoc $0" ) ;
    exit(1) ;
}
if ( $options{help} || ( $#ARGV < 0 ) ) {
    pod2usage(1);
}

#
# Sets the default values of options
#
# Titles:

# Verbosing:
if ( $options{quiet} ) {
  $options{verbose} = -1 ;
}
Bib2HTML::General::Verbose::setlevel( $options{verbose} ) ;

# Error messages:
if ( $options{'warnings'} ) {
  Bib2HTML::General::Error::unsetwarningaserror() ;
}
else {
  Bib2HTML::General::Error::setwarningaserror() ;
}
if ( $options{'sort-warnings'} ) {
  Bib2HTML::General::Error::setsortwarnings() ;
}
else {
  Bib2HTML::General::Error::unsetsortwarnings() ;
}

#
# Create the output directory
#
eval '$options{\'output\'} = '.$options{'generator'}.'::create_output_directory($options{\'output\'},$options{\'force\'});' ;
if ( $@ ) {
  Bib2HTML::General::Error::syserr( "$@\n" ) ;
}

# Read the BibTeX files
my $parser = new Bib2HTML::Parser::Parser($options{'show-bibtex'}) ;
if ( $options{'tex-preamble'} ) {
  $parser->read_preambles( $options{'tex-preamble'} ) ;
}
$parser->parse( \@ARGV ) ;

# Check if the names of the authors are similars
if ( $options{'check-names'} ) {
  eval "require Bib2HTML::Checker::Names;" ;
  if ( $@ ) {
    Bib2HTML::General::Error::syserr( "Unable to find the generator class: Bib2HTML::Checker::Names\n$@\n" ) ;
  }
  my $check = new Bib2HTML::Checker::Names() ;
  $check->check($parser->content()) ;
}

# Generates the HMTL pages
#
# Create the generator
#
my $generator = ($options{'generator'})->new( $parser->content(),
					      $options{'output'},
					      { 'VERSION' => $VERSION,
						'BUG_URL' => $SUBMIT_BUG_URL,
						'URL' => $URL,
						'AUTHOR_EMAIL' => $AUTHOR_EMAIL,
						'AUTHOR' => $AUTHOR,
						'PERLSCRIPTDIR' => $PERLSCRIPTDIR,
					      },
					      { 'SHORT' => $options{'wintitle'},
						'LONG' => $options{'title'},
					      },
					      $options{'lang'},
					      $options{'theme'},
					      $options{'show-bibtex'},
					      $options{'genparams'} ) ;
$generator->generate() ;

# Display the quantity of warnings
Bib2HTML::General::Error::printwarningcount() ;

exit 0 ;

__END__

=head1 NAME

bib2html - A perl script that generates an HTML documentation for a BibTeX database


=head1 SYNOPSYS

bib2html [options] F<file> [F<file> ...]

=head1 DESCRIPTION

bib2html is a script which permits to generate a set of HTML pages for
the entries of BibTeX files.

=head1 OPTIONS

=over 4

=item B<-[no]b>

=item B<--[no]bibtex>

This option permits to generate, or not, a verbatim of the
BibTeX entry code.

=item B<--[no]checknames>

This option permits to check if the some author's names are
similar and generates a set of warnings about each of them.

=item B<-d> I<name>[=I<value>]

See B<--generatorparam>.

=item B<--doctitle> I<text>

Sets the title that appears in the main page.

=item B<-f>

=item B<--force>

Forces to overwrite into the output directory.

=item B<-g> I<class>

=item B<--generator> I<class>

Sets the generator to use. I<class> must be a valid Perl class.

=item B<--generatorparam> I<name>[=I<value>]

Sets a generator param. It must be a I<key>=I<value> pair or
simply a I<name>.
Example: "target=thisdirectory" defines the parameter target
with corresponding value "thisdirectory". The specified
parameters which are not supported by the generator are
ignored.

=item B<--generatorparams>

Shows the list of supported parameters, and their
semantics for the current generator.

=item B<--genlist>

Shows the list of supported generators.

=item B<-?>

=item B<-h>

Show the list of available options.

=item B<--help>

See B<--man>.

=item B<--lang> I<name>

Sets the language used by the generator.

=item B<--langlist>

Shows the list of supported language.

=item B<--man>

=item B<--manual>

Show the manual page.

=item B<-o> F<file>

=item B<--output> F<file>

Sets the directory or the F<file> in which the documentation will be put.

=item B<-p> F<file>

=item B<--preamble> F<file>

Sets the name of the F<file> to read to include some TeX preambles.
You could use this option to dynamicaly defined some unsupported
LaTeX commands.

=item B<-q>

Don't be verbose: only error messages are displayed.

=item B<--[no]sortw>

Shows (or not) a sorted list of warnings.

=item B<--texcmd>

Shows the list of supported LaTeX commands.

=item B<--theme> I<name>

Sets the theme used by the generator.

=item B<--themelist>

Shows the list of supported themes.

=item B<-v>

Be more verbose.

=item B<--version>

Show the version of this script.

=item B<--[no]warning>

If false, the warning are converted to errors.

=item B<--windowtitle> I<text>

Sets the title that appears as the window's title.

=back

=head1 LICENSE

S<GNU Public License (GPL)>

=head1 COPYRIGHT

S<Copyright (c) 1998-06 Stphane Galland E<lt>galland@arakhne.orgE<gt>>

=head1 CONTRIBUTORS

=over

=item S<Aurel GABRIS E<lt>L<gabrisa@optics.szfki.kfki.hu>E<gt>>

=item S<Tobias LOEW E<lt>L<loew@mathematik.tu-darmstadt.de>E<gt>>

=item S<Joao LOURENCO E<lt>L<joao.lourenco@di.fct.unl.pt>E<gt>>

=item S<Dimitris MICHAIL E<lt>L<michail@mpi-sb.mpg.de>E<gt>>

=item S<Luca PAOLINI E<lt>L<paolini@di.unito.it>E<gt>>

=item S<Norbert PREINING E<lt>L<preining@logic.at>E<gt>>

=item S<Cristian RIGAMONTI E<lt>L<cri@linux.it>E<gt>>

=item S<Sebastian RODRIGUEZ E<lt>L<sebastian.rodriguez@utbm.fr>E<gt>>

=item S<Martin P.J. ZINSER E<lt>L<zinser@zinser.no-ip.info>E<gt>>

=back

=head1 SEE ALSO

L<latex>, L<bibtex>
