#! /bin/sh -e

PROGRAM=lr_xml2report

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

test -z "$LR_ID" && LR_ID=`lr_tag`
export LR_ID

lire_log "info started with $*"

USAGE="Usage: $PROGRAM [-t] [-i] [-c <report_cfg>] [-o <outputformat>] [-U <superservice>] report.xml [report.xml ...]"
# get options (x)
OUTPUTFORMAT=""
dry_run=""
include_image=""
while getopts :o:c:U:S:ivth\? o; do
    case $o in
        o)  OUTPUTFORMAT="$OPTARG"
            ;;
        c)  REPORT_CFG="$OPTARG"
            ;;
        U)
            # needed by lr_xml_merge
            LR_SUPERSERVICE="$OPTARG"
            ;;
        S)
            # Pre 1.3 Backward compatibility
            LR_SERVICE="$OPTARG"
            ;;
	i)
	    include_image=1
	    ;;
	t)
	    dry_run=1
	    ;;
        h | "?")
            lire_log "notice $USAGE"
            exit 0
            ;;
	v)
	    lire_log "notice $PROGRAM" '$Id: lr_xml2report.in,v 1.17 2004/03/30 17:50:53 wsourdeau Exp $,' "shipped with Lire 1.5"
	    exit 0
	    ;;
        *)
            lire_log "err $USAGE"
            exit 1
            ;;
    esac
done

# yes, the '|| true' is needed. solaris' /bin/sh feels it should bawl out when
# encountering an 'expr 1 - 1', while running -e
shift `expr $OPTIND - 1 || true`

if test $# -lt 1
then
    lire_log "err $USAGE"
    exit 1
fi

if test $# -gt 1
then
    XML_REPORT_FILES="$@"
    if test -z "$LR_SUPERSERVICE"
    then
        lire_log "err multiple xmlreports specified, but LR_SUPERSERVICE unset"
        exit 1
    fi
else
    XMLREPORTFILE="$1"
fi


if test -z "$OUTPUTFORMAT"
then
    OUTPUTFORMAT="$lr_output_format"
fi

# lr_images only turn on images for supported format
if test -n "$lr_images"
then
    case $OUTPUTFORMAT in
    pdf|html|html_page|docbookx|xhtml|xhtml_page)
	image_flag="-i"
	;;
    *)
	;;
    esac
else
    if test -n "$include_image"
    then
	image_flag="-i"
    else
	image_flag=""
    fi
fi

# do a test for support before possibly unnecessary log file analysis
case $OUTPUTFORMAT in
    xml|txt|pdf|html|html_page|docbookx|xhtml|xhtml_page|excel95)
        ;;
    *)
        lire_log "err format $OUTPUTFORMAT is not supported"
        exit 1
        ;;
esac
lr_check_prereq $image_flag $OUTPUTFORMAT || exit 1

if test -n "$dry_run"
then
    exit 0
fi

if test -n "$XMLREPORTFILE"
then
    :
else 
    if test -z "$REPORT_CFG"
    then
        REPORT_CFG=`find_default_report_cfg $LR_SUPERSERVICE`
    fi

    XMLREPORTFILE=`lr_tempfile $PROGRAM.$LR_SUPERSERVICE.$LR_ID.XXXXXX.xml`
    lr_mark_for_cleanup $XMLREPORTFILE
    lire_log "info gonna run lr_xml_merge $LR_SUPERSERVICE $REPORT_CFG $XML_REPORT_FILES > $XMLREPORTFILE"
    if lr_xml_merge $LR_SUPERSERVICE $REPORT_CFG $XML_REPORT_FILES >> $XMLREPORTFILE
    then
        :
    else
        lire_log "err lr_xml_merge failed"
        exit 1
    fi
    lire_log "info lr_xml_merge finished"
fi

lire_log "info converting $XMLREPORTFILE to $OUTPUTFORMAT"

case $OUTPUTFORMAT in
    xml) 
        lire_log "info generating raw XML output, not doing lr_xml2ascii"
        cat $XMLREPORTFILE
        ;;
    txt)
        OK=
        lire_log "info gonna run lr_xml2ascii on $XMLREPORTFILE"
        lr_xml2ascii < $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            lire_log "info succeeded in generating ascii"
        else
            lire_log "err lr_xml2ascii failed"
            exit 1
        fi
        lire_log "info lr_xml2ascii finished"
        ;;
    html|html_page)
	one_page=
	if test $OUTPUTFORMAT = "html_page"
	then
	    one_page="-1"
	fi
        OK=
	lr_xml2html $image_flag $one_page $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            lire_log "info succeeded in generating HTML"
        else
            lire_log "err lr_xml2html failed"
            exit 1
        fi
        lire_log "info lr_xml2html finished"
        ;;
    pdf)
        OK=
        lr_xml2pdf $image_flag $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            lire_log "info succeeded in generating PDF"
        else
            lire_log "err lr_xml2pdf failed"
            exit 1
        fi
        lire_log "info lr_xml2pdf finished"
        ;;
    docbookx)
        OK=
        lr_xml2dbx $image_flag $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            lire_log "info succeeded in generating DocBook XML"
        else
            lire_log "err lr_xml2dbx failed"
            exit 1
        fi
        lire_log "info lr_xml2dbx finished"
        ;;
    xhtml|xhtml_page)
	one_page=
	if test $OUTPUTFORMAT = "xhtml_page"
	then
	    one_page="-1"
	fi
        OK=
        lr_xml2xhtml $image_flag $one_page $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            lire_log "info succeeded in generating XHTML"
        else
            lire_log "err lr_xml2xhtml failed"
            exit 1
        fi
        lire_log "info lr_xml2xhtml finished"
        ;;
    excel95)
        OK=
	lr_xml2xls < $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            lire_log "info succeeded in generating Excel95 spreadsheet"
        else
            lire_log "err lr_xml2xls failed"
            exit 1
        fi
        lire_log "info lr_xml2xls finished"
        ;;
    *)
        lire_log "err format $OUTPUTFORMAT is not supported"
        exit 1
        ;;
esac

if test -n "$XML_REPORT_FILES"
then
    lr_archive_log
fi

lire_log "info stopped"

exit 0

POD=<<'EOPOD'

=pod

=head1 NAME

lr_xml2report - Format a XML report to the appropriate output format

=head1 SYNOPSIS

B<lr_xml2report> B<[>I<options>B<]> I<report.xml> B<[>I<report.xml> ...B<]>

B<lr_xml2report> B<-h>

B<lr_xml2report> B<-v>

=head1 OPTIONS

=over 4

=item B<-h>

Prints usage information and exits.

=item B<-i>

Generate charts for all reports that support it. Normally, charts will be 
generated only if the variable LR_INCLUDEIMAGES is set to 1.

=item B<-c> I<report_cfg>

Use the file I<report_cfg> (e.g. /etc/lire/print.cfg.local) as the report
configuration file instead of the default one.  This option is only used
in case multiple XML report files where specified.

=item B<-o> I<output format>

one of B<docbookx>, B<excel95>, B<html>, B<html_page>, B<pdf>, 
 B<txt>, B<xhtml>, B<xhtml_page> and B<xml>.  Per default, simple txt
output is generated. B<html> and B<xhtml> generate multiple pages,
B<html_page> and B<xhtml_page> generate the complete report on one page.
(Refer to the Lire User Manual for a description of those formats.)

=item B<-U> I<superservice>

in case multiple XML report files are specified, the Lire
I<superservice> needs to be known. If this script is called by a
wrapper, the wrapper script should make sure the LR_SUPERSERVICE
environment variable is set. When calling this script by hand, one
could use the B<-U> option. The script lr_check_superservice(1) can be
used to look up Lire supported services and superservices.

=item B<-t>

Only tests for requirements, don't actually format the report.

=item B<-v>

Prints version and exits.

=back

=head1 DESCRIPTION

B<lr_xml2report> converts the XML reports generated by Lire to various
output format.

When run from the commandline, it's usefull to run it under lr_run(1), to
be able to tune the amount of debug output generated.

The script could be run as e.g.

 lr_run lr_xml2report -o pdf -U print -S cups_pagelog report.3.xml \
  report.2.xml report.1.xml > report.pdf

(in multiple XML input files mode) or as

 lr_run lr_xml2report -o txt report.xml > report.txt

(in single XML input file mode).

=head1 SEE ALSO

lr_xml2mail(1), lr_xml_merge(1); as well as manpages for the Lire internal
typesetter scripts: lr_xml2ascii(1), lr_xml2html(1), 
lr_xml2pdf(1), lr_xml2dbx(1), lr_xml2xhtml(1), lr_xml2xls(1)

=head1 VERSION

$Id: lr_xml2report.in,v 1.17 2004/03/30 17:50:53 wsourdeau Exp $

=head1 COPYRIGHT

Copyright (C) 2000, 2001, 2002 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 <joostvb@logreport.org>, Francis J. Lacoste
<flacoste@logreport.org>

=cut

EOPOD


