#! /bin/sh -e

PROGRAM=lr_xml2html

# 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] [-1] report.xml"
  exit 1
fi

images=
type=html
while getopts :i1 a
do
    case $a in
    i)
        images="-i"
        ;;
    1)
	type=html_page
	;;
    *)
        echo >&2 "$tag err usage $PROGRAM [-i] [-1] 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 $type || exit 1

# get global vars
prefix="/usr"
datadir="${prefix}/share/lire"
exec_prefix="${prefix}"
libexecdir="/usr/lib/lire"
etcdir="/etc/lire"
. $etcdir/defaults

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

tmpdir="$TMPDIR/$PROGRAM.$$"
tmpreportdir="$tmpdir/report"
echo >&2 "$tag debug creating temporary report dir $tmpreportdir"
if mkdir -p $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

dbxfile="$TMPDIR/$PROGRAM.all.$$.dbx"
echo >&2 "$tag info running lr_xml2dbx to convert $xmlfile to DocBook XML $dbxfile..."
lr_xml2dbx $xmlfile > $dbxfile

echo >&2 "$tag info converting $dbxfile to HTML into $tmpreportdir..."

if test $type = "html"
then
    xslfile=$LR_DBK_HTML_XSL
    ( cd $tmpreportdir && lr_xslt $xslfile $dbxfile )
else
    xslfile=$LR_DBK_HTML_ONEPAGE_XSL
    lr_xslt $xslfile $dbxfile > $tmpreportdir/index.html
fi

# see comment in lr_xml2dbx for rationale of subshell
( cd $tmpdir && $LR_TAR cf - report )

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

echo >&2 "$tag info stopped"

exit 0

POD=<<'EOPOD'

=pod

=head1 NAME

lr_xml2html - generate a HTML report from a raw xml report

=head1 SYNOPSIS

B<lr_xml2html> 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). 

=item B<-1>

Generate one HTML page for the whole report instead of putting each
sections in its own page.

=back

=head1 DESCRIPTION

B<lr_xml2html> reads a raw xml report from file I<file>, and creates a
HTML report from it. It will output a tar(1) file on STDOUT containing
a F<report> directory and the HTML files (and optionnaly some charts)
in it. One could pipe the output through '| tar xf -' to extract the
files in the current directory.

When the B<-i> option is used, B<lr_xml2html> will generates PNG
charts.

It uses the LR_ID environment variable in debug messages it prints to stdout.

It runs lr_xslt(1) to generate the HTML.

Since lr_xml2html(1) is a Lire internal script, is is likely only ran manually
when debugging.  In such a case, wrap it in lr_run(1): lr_xml2chart(1) is
assumed to be in the PATH.

=head1 THANKS

Joost Kooij <joost@topaz.mdcc.cx>, for explaining how to deal with
globbing in the shell.

=head1 SEE ALSO

lr_dlf2xml(1), lr_xml2ascii(1), lr_xml2xhtml(1)

=head1 VERSION

$Id: lr_xml2html.in,v 1.41 2003/07/02 14:25:18 vanbaal Exp $

=head1 COPYRIGHT

Copyright (C) 2000, 2001, 2002, 2003 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 AUTHORS

Joost van Baal, Francis J. Lacoste, Egon Willighagen

=cut

