#! /bin/sh -e

PROGRAM=lr_log2report

# 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 [-c <report_cfg>] [-i] [-o <outputformat>] dlf_converter"

# get options (x)
output_flag=""
image_flag=""
cfg_flag=""
while getopts :o:c:ivh\? o; do
    case $o in
	c) cfg_flag="-c $OPTARG"
	    ;;
        o) output_flag="-o $OPTARG"
            ;;
	i) image_flag="-i"
	    ;;
        h | "?")
            lire_log "notice $USAGE"
            exit 0
            ;;
        v)
            lire_log "notice $PROGRAM" '$Id: lr_log2report.in,v 1.90 2003/04/30 14:31:16 flacoste 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 $# -ne 1
then
    lire_log "err $USAGE"
    exit 1
fi

# e.g. apache
LR_SERVICE=`lr_check_service "$1"`
LR_SUPERSERVICE=`lr_check_superservice $1`

# Test output format requirements
if lr_xml2report -t $image_flag $output_flag /dev/null
then
    :
else
    lire_log "err missing requirements for the selected output format"
    exit 1
fi

LOGFILE=`lr_tempfile $PROGRAM.$LR_SERVICE.$LR_ID.XXXXXX.log`
lr_mark_for_cleanup $LOGFILE
if lr_inflate >> $LOGFILE
then
    :
else
    lire_log "err lr_inflate failed"
    exit 1
fi

lire_log "info gonna run lr_log2xml $cfg_flag $LR_SERVICE"
XMLREPORTFILE=`lr_tempfile $PROGRAM.$LR_SERVICE.$LR_ID.report.XXXXXX.xml`
STATSFILE=`lr_tempfile $PROGRAM.$LR_SERVICE.$LR_ID.import_log_stats.XXXXXX.sh`
lr_mark_for_cleanup $XMLREPORTFILE $STATSFILE

if lr_log2xml $cfg_flag $LR_SERVICE $STATSFILE < $LOGFILE >> $XMLREPORTFILE
then
    :
else
    lire_log "err lr_log2xml $LR_SERVICE failed"
    exit 1
fi

lr_check_import_log_stats $STATSFILE
if test -n "$lr_import_OK"
then
    :
else
    lire_log "warning log file may be bogus: $lr_import_error_count errors on $lr_import_line_count lines"
    lire_log "notice will generate a report for the $lr_import_dlf_count records extracted"
fi

lire_log "info gonna run lr_xml2report $image_flag $output_flag on $XMLREPORTFILE"
if lr_xml2report $image_flag $output_flag $XMLREPORTFILE
then
    lire_log "info succeeded in formatting report"
else
    lire_log "err lr_xml2report failed"
    exit 1
fi
lire_log "info lr_xml2report finished"

# This only triggers warnings now
lr_archive_log

lire_log "info stopped"

exit 0

POD=<<'EOPOD'

=pod

=head1 NAME

lr_log2report - generates a report from a log file

=head1 SYNOPSIS

B<lr_log2report> B<[>I<options>B<]> I<dlf_converter>

B<lr_log2report> B<-h|-v>

=head1 OPTIONS

=over 4

=item B<-h>

Prints usage information and exits.

=item B<-c> I<report_cfg>

Use the file I<report_cfg> as the report configuration file instead of
the default one.  See the lr_log2xml(1) manpage for details.

=item B<-i>

Generate charts for all reports that support it. Normally, charts will
be generated only if the variable I<lr_images> is set.

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

one of B<docbookx>, B<excel95>, B<html>, B<html_page>, B<pdf>, B<rtf>,
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<-v>

Prints Lire's version and exits.

=back

=head1 DESCRIPTION

B<lr_log2report> reads a log file from STDIN and will output a
formatted Lire report on STDOUT. The format of the log file is
specified using the I<dlf_converter> argument. Run B<lr_check_service>
B<-l> to get a list of all currently available DLF converters.

In case the log is passed in a compressed format, it's uncompressed on the fly,
using lr_inflate(1).

WARNING: This command outputs binary data on the terminal for all
output formats except B<txt> and B<xml>. You should redirect
stdout to a file when using those output formats.

In case I<output format> is one of B<html>, B<html_page>, B<xhtml>,
B<xhtml_page> , output is generated in tar(1) format.

The environment variable I<LR_ID> is used in tags in debug messages,
which get printed to stdout. In case I<LR_ID> is not set,
lr_log2report(1) itself generates one.

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

=head1 EXAMPLES

To process a Postfix log and display the report as text, use

 $ lr_run lr_log2report postfix < /var/log/mail.log

To process a Postfix log and create the report in HTML, packed up in a tar
archive,  use:

 $ lr_run lr_log2report -o html postfix < /var/log/mail.log > postfix.tar

=head1 SEE ALSO

lr_log2mail(1), lr_log2xml(1), lr_xml2report(1), lr_anonymize(1),
lr_check_service(1), lire(7), documentation in the Lire User Manual

=head1 VERSION

$Id: lr_log2report.in,v 1.90 2003/04/30 14:31:16 flacoste Exp $

=head1 COPYRIGHT

Copyright (C) 2000-2003 Stichting LogReport Foundation LogReport@LogReport.org

This program is part of Lire.

Lire 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

Joost van Baal <joostvb@logreport.org>

=cut

EOPOD


