#! /bin/sh -e

PROGRAM=lr_xml2pdf

# dereference sysconfdir's prefix dependency
prefix="/usr"
etcdir="/etc/lire"
 
. $etcdir/profile_lean

tag="all all ${LR_ID:-UNSET} $PROGRAM"

echo >&2 "$tag info started with $*"

if test $# -lt 1
then
  echo >&2 "$tag err expecting argument. syntax: $PROGRAM [-i] report.xml"
  exit 1
fi

images=
while getopts :i a
do
    case $a in
    i)
        images="-i"
        ;;
    *) 
        echo >&2 "$tag err usage $PROGRAM [-i] report.xml"
        exit 1
        ;; 
    esac
done
shift `expr $OPTIND - 1 || true`

file="$1"

if test ! -r "$file"
then
  echo >&2 "$tag err file '$file' is not readable."
  exit 1
fi

lr_check_prereq $images pdf || exit 1

# get global vars
. $etcdir/defaults

XML_DCL="${prefix}/share/sgml/declaration/xml.dcl"

if test ! -d "$TMPDIR"
then
    echo >&2 "$tag notice dir $TMPDIR does not exist, creating it"
    if mkdir "$TMPDIR"
    then
        :
    else
        echo >&2 "$tag err cannot create $TMPDIR, exiting!"
        exit 1
    fi
fi

tmpreportdir="$TMPDIR/$PROGRAM.$$"
echo >&2 "$tag debug creating temporary report dir $tmpreportdir"
if mkdir $tmpreportdir
then 
    :
else
    echo >&2 "$tag err failed to create $tmpreportdir"
    exit 1
fi

xmlfile="$TMPDIR/$PROGRAM.all.$$.xml"
if test -n "$images"
then
    lr_xml2chart -o $tmpreportdir < $file > $xmlfile
else 
    cp $file $xmlfile
fi

tmpbase="$tmpreportdir/report"
dbxfile="$tmpbase.dbx"
echo >&2 "$tag info running lr_xml2dbx to convert $xmlfile to DocBook XML $dbxfile..."
lr_xml2dbx $xmlfile > $dbxfile

oldpwd="`pwd`"
cd $tmpreportdir

tex2pdf=''
tmppdf="$tmpbase.pdf"
case $LR_PDF_STYLE in
    fo)
	tex2pdf=$LR_PDFXMLTEX
	tmptex=$tmpbase.fo
	echo >&2 "$tag info converting $dbxfile to $tmpbase.fo..."
	lr_xslt $LR_DBK_FO_XSL $dbxfile > $tmptex
	;;
    *)
	tex2pdf=$LR_PDFJADETEX
	tmptex=$tmpbase.tex
	# Create needed catalog file
	cat > catalog <<EOF
SYSTEM "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" "$LR_DBK_XML_DTD"
EOF

	# now convert that dbx file to intermediary TeX file
	echo >&2 "$tag info converting $dbxfile to $tmpbase.tex..."

	# tweak jade's stderr
	exec 3>&1
	$LR_JADE -t tex -d $LR_DBK_PRINT_STYLESHEET $XML_DCL $dbxfile 2>&1 1>&3 3>&- | 
	while read s; do echo >&2 "$tag info $LR_JADE says $s"; done 1>&2 3>&-

	;;
esac
echo >&2 "$tag info converting $tmptex to $tmppdf..."

# This could take up to three iterations
i=0
while test $i -lt 3; do
    if $tex2pdf -interaction=batchmode `basename $tmptex` > /dev/null 2>&1
    then
	# Check to see if we need to rerun
	if grep 'undefined references\| [Rr]erun' report.log > /dev/null
	then
	    echo >&2 "$tag info rerunnning $tex2pdf"
	else
	    break
	fi
    else
	echo >&2 "$tag err pdfjadetex failed"
	# The command failed, output the errors
	grep '^! ' report.log |
	    while read s; do echo >&2 "$tag err $tex2pdf said '$s'"; done
	echo >&2 "$tag notice keeping pdfjadetex files in $tmpreportdir for debugging"
	exit 1
    fi
    # Increase the counter
    i=`expr $i + 1`
done

# clean up jade and pdfjadetex tmpfiles (we never wanna keep them, even
#  when LR_KEEP is set we wanna get rid of them)
rm -f report.out report.log report.aux report.out texput.log catalog

# give output
cat $tmppdf

# this relies on executable cwd at script startup time
cd $oldpwd

# clean temp
if test -n "$LR_KEEP"
then
    echo >&2 "$tag notice keeping temporary files in $tmpreportdir on your request. remove manually"
else
    rm -fr $tmpreportdir $xmlfile
fi

echo >&2 "$tag info stopped"

exit 0


POD=<<'EOPOD'

=pod

=head1 NAME

lr_xml2pdf - generate a PDF report from a raw xml report

=head1 SYNOPSIS

B<lr_xml2pdf> B<[options]> I<file>

=head1 OPTIONS

=over 4

=item B<-i>

generate charts of subreport which have the I<charttype> attribute
defined, by running lr_xml2chart(1).

=back

=head1 DESCRIPTION

B<lr_xml2pdf> reads a raw xml report from file I<file>, and prints a
PDF report on STDOUT. It uses the LR_ID environment variable in debug
messages it prints to STDOUT. It expects to find lr_xml2chart(1) in it's
PATH.

Portable Document Format is a widely used format for viewing documents. They
can be viewed with e.g. Acroread, downloadable from the Adobe website
(http://www.adobe.com), and packaged for most unices and other OS-es.

=head1 SEE ALSO

lr_dlf2xml(1), lr_xml2ascii(1), lr_xml2pdf(1)

=head1 VERSION

$Id: lr_xml2pdf.in,v 1.50 2003/11/07 22:58:45 wsourdeau Exp $

=head1 COPYRIGHT

Copyright (C) 2000, 2001 Stichting LogReport Foundation LogReport@LogReport.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 COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html or write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.

=head1 AUTHOR

Egon Willighagen

=cut

EOPOD


