#!/usr/bin/perl -w
use strict;
use FindBin;
use lib "$FindBin::RealBin/../lib";
use Getopt::Long qw(:config no_ignore_case);
use Pod::Usage;
use LaTeXML;
use LaTeXML::Util::Pathname;

#**********************************************************************
# Parse command line

my $identity = "latexml (LaTeXML version $LaTeXML::VERSION)";
my($verbosity,$strict,$comments,$noparse)=(0,0,1,0);
my ($format,$output,$help,$showversion)=('xml','');
my (@paths,@preload,@debugs);
GetOptions("output=s"  => \$output,
	   "preload=s" => \@preload,
	   "path=s"    => \@paths,
	   "quiet"     => sub { $verbosity--; },
	   "verbose"   => sub { $verbosity++; },
	   "strict"    => \$strict,
	   "xml"       => sub { $format = 'xml'; },
	   "tex"       => sub { $format = 'tex'; },
	   "box"       => sub { $format = 'box'; },
	   "noparse"   => \$noparse,
	   "comments!" => \$comments,
	   "VERSION"   => \$showversion,
	   "debug=s"   => sub { eval "\$LaTeXML::$_[1]::DEBUG=1; "; },
	   "help"      => \$help,
	  ) or pod2usage(-message => $identity, -exitval=>1, -verbose=>0, -output=>\*STDERR);
pod2usage(-message=>$identity, -exitval=>1, -verbose=>2, -output=>\*STDOUT) if $help;
if($showversion){ print STDERR "$identity\n"; exit(1); }
pod2usage(-message=>"$identity\nMissing input TeX file", 
	  -exitval=>1, -verbose=>0,-output=>\*STDERR) unless @ARGV;
my $source = $ARGV[0];

#**********************************************************************
# Do the processing.
print STDERR "$identity\n" unless $verbosity < 0;

my $latexml= LaTeXML->new(preload=>[@preload], searchpaths=>[@paths],
			 verbosity=>$verbosity, strict=>$strict,includeComments=>$comments,
			 nomathparse=>$noparse);

if($output){
  my($dir,$name,$type)=pathname_split($output);
  if($dir){
    pathname_mkdir($dir) or die "Couldn't create destination directory $dir: $!"; }
  open(OUT,">:utf8",$output) or die "Couldn't open output file $output: $!"; }
else {
  binmode(STDOUT,":utf8");
  *OUT = *STDOUT; }

binmode(STDERR,":utf8");
if($format eq 'tex'){
  my $digested = $latexml->digestFile($source);
  print OUT $digested->untex; }
elsif($format eq 'box'){
  my $digested = $latexml->digestFile($source);
  print OUT $digested->toString; }
else {
  my $dom = $latexml->convertFile($source); 
  print OUT $dom->toString(1);
}

if($output){ close(OUT); }

#**********************************************************************
__END__

=head1 NAME

C<latexml> transforms a TeX/LaTeX file into XML

=head1 SYNOPSIS

latexml [options] texfile

  Options:
   --output=outputfile        specifies output file; default prints to stdout.
   --preload=module           requests loading of an optional module; can be repeated
   --path=dir                 adds dir to the paths searched for files, modules, etc; 
   --quiet                    suppress messages (can repeat)
   --verbose                  more informative output (can repeat)
   --strict                   makes latexml less forgiving of errors
   --xml                      requests xml output (default).
   --tex                      requests TeX output after expansion.
   --box                      requests box output after expansion and digestion.
   --nocomments               omit comments from the output
   --VERSION                  show version number.
   --debug=package            enables debugging output for the named package
   --help                     shows help message.

=head1 OPTIONS AND ARGUMENTS

latexml transforms a TeX/LaTeX file into XML.

=over 4

=item B<--output=>I<outputfile>

Specifies the output file; by default the XML is written to stdout.

=item B<--preload=module>

Requests the loading of an optional module or package.  This may be useful if the TeX code
does not specificly require the module (eg. through input or usepackage).

=item B<--path>=I<dir>

Add I<dir> to the search paths used when searching for files, modules, style files, etc;
somewhat like TEXINPUTS.  This option can be repeated.

=item B<--quiet>

Reduces the verbosity of output during processing, used twice is pretty silent.

=item B<--verbose>

Increases the verbosity of output during processing, used twice is pretty chatty.
Can be useful for getting more details when errors occur.

=item B<--strict>

Specifies a strict processing mode. By default, undefined control sequences and
invalid document constructs (that violate the DTD) give warning messages, but attempt
to continue processing.  Using --strict makes them generate fatal errors.

=item B<--xml>

Requests XML output; this is the default.

=item B<--tex>

Requests TeX output for debugging purposes;  processing is only carried out through expansion and digestion.
This may not be quite valid TeX, since Unicode may be introduced.

=item B<--box>

Requests Box output for debugging purposes;  processing is carried out through expansion and digestions,
and the result is printed.

=item B<--nocomments>

Normally latexml preserves comments from the source file, and adds a comment every 25 lines as
an aid in tracking the source.  The option --nocomments discards such comments.

=item B<--VERSION>

Shows the version number of the LaTeXML package..

=item B<--debug>=I<package>

Enables debugging output for the named package. The package is given without the leading LaTeXML::.

=item B<--help>

Shows this help message.

=back

=head1 SEE ALSO

L<latexmlpost>, L<LaTeXML>

=cut
#**********************************************************************

