#!/usr/local/bin/perl -w

use strict;
use Bibulus::LaTeX;

my $filename;
my $DEBUG = 0;
my $terse = 0;

my $bib = new Bibulus::LaTeX;
$bib->inlinecrossref(2); # standard for BibTeX
$bib->setmainlang('en'); # default language for BibTeX

# Parse command line:
foreach (@ARGV) {
  if (/^--?min-crossrefs=(\d+)$/) {
    $bib->inlinecrossref($1);
  } elsif (/^--?language=(\w+)$/) {
    $bib->setmainlang($1);
  } elsif (/^--?terse$/) {
    $bib->verbose(0);
    $terse = 1;
  } elsif (/^--?version$/) {
    print <<'EOT;';
Bibulustex $Id: bibulustex,v 1.10 2003/09/06 20:21:45 twid Exp $
Copyright (C) 2003 Thomas Widmann.
There is NO warranty.  Redistribution of this software is
covered by the terms of the GNU General Public License.
For more information about these matters, see the file
named doc/GPL_V2 and the manual pages embedded in the source files.
Primary author of Bibulus: Thomas Widmann.

EOT;
    exit 0;
  } elsif (/^--?help$/) {
    usage();
  } elsif (/^--?debug$/) {
    print STDERR 'This is bibulustex $Id: bibulustex,v 1.10 2003/09/06 20:21:45 twid Exp $', "\n";
    Bibulus::LaTeX->setdebug;
  } elsif (/^-/) {
    die "bibulustex: unrecognized option `$_'\nTry `bibulustex --help' for more information.\n";
  } else {
    $filename = $_;
  }
}

# It would be possible to generate UTF-8 output by doing
#      binmode(STDOUT, ":utf8");

$filename or die "bibulustex: Need exactly one file argument.\nTry `bibuluxtex --help' for more information.\n";

print "This is Bibulus\n" unless $terse;

$bib->procaux($filename);
open (BBL, ">$filename.bbl") or die "Could not write $filename.bbl.\n";
print BBL $bib->getbib;
close BBL;

# Print usage information and exit:
sub usage {
  print <<"EOT;";
Usage: bibulustex [OPTION]... AUXFILE[.aux]
  Write bibliography for entries in AUXFILE to AUXFILE.bbl.

-min-crossrefs=NUMBER  include item after NUMBER cross-refs; default 2
-terse                 do not print progress reports
-help                  display this help and exit
-version               output version information and exit
-debug                 print debugging information while running
-language=LN           output bibliography in language LN (two-letter ISO code)

Email bug reports to twid\@cpan.org
EOT;
  exit 0;
}

exit 0;

__END__

=head1 NAME

bibulustex - a drop-in replacement for bibtex

=head1 SYNOPSIS

  bibulustex [OPTION]... AUXFILE[.aux]

=head1 DESCRIPTION

Write bibliography for entries in AUXFILE to AUXFILE.bbl.

 -min-crossrefs=NUMBER  include item after NUMBER cross-refs; default 2
 -terse                 do not print progress reports
 -help                  display this help and exit
 -version               output version information and exit
 -debug                 print debugging information while running
 -language=LN           output bibliography in language LN (two-letter ISO code)

=head1 BUGS

The output is not yet identical to what BibTeX would produce,
but that is really the fault of F<Bibulus.pm> and F<LaTeX.pm>,
not of C<bibulustex>.

=head1 SEE ALSO

The homepage is L<http://www.nongnu.org/bibulus/>.

=head1 AUTHOR

Thomas M. Widmann, L<twid@cpan.org>

=head1 COPYRIGHT AND LICENSE

Copyright 2003 by Thomas M. Widmann

This module 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; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

=cut
