#! /bin/sh -e

PROGRAM=lr_log2report

# dereference sysconfdir's prefix dependency
prefix="/usr/local"
etcdir="${prefix}/etc/lire"

. $etcdir/profile_lean
. $etcdir/defaults

# an id which could be used to identify the source of the logfile, e.g.
# by setting it in the subject of the email containing a logfile
test -z "$LR_EXTID" && LR_EXTID='localhost'
export LR_EXTID

test -z "$LR_ID" && LR_ID=`lr_tag`
export LR_ID
tag="all all $LR_ID $PROGRAM"

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

USAGE="Usage $PROGRAM [-c <report_cfg>] [-i] [-o <outputformat>] service"

# 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 | "?")
            echo >&2 "$tag notice $USAGE"
            exit 0
            ;;
        v)
            echo >&2 "$tag notice $PROGRAM" '$Id: lr_log2report.in,v 1.78 2002/08/18 19:44:40 flacoste Exp $,' "shipped with Lire 1.1"
            exit 0
            ;;
        *)
            echo >&2 "$tag 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 $# -eq 0
then
    echo >&2 "$tag err $USAGE"
    exit 1
fi

if test $# -gt 1
then
    echo >&2 "$tag warning obsolete (since Lire 1.1) superservice argument detected"
    echo >&2 "$tag this argument is ignored"
    echo >&2 "$tag please change your scripts and call this program as $USAGE"
    shift
fi

# e.g. apache
LR_SERVICE=`lr_check_service "$1"`
if test -z "$LR_SERVICE"
then
    echo >&2 "$tag err lr_check_service failed"
    exit 1
fi
shift

tag="all $LR_SERVICE $LR_ID $PROGRAM"

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

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

LOGFILE=$TMPDIR/$PROGRAM.$LR_SERVICE.$LR_ID.log

if lr_inflate > $LOGFILE
then
    :
else
    echo >&2 "$tag err lr_inflate failed"
    exit 1
fi

echo >&2 "$tag info gonna run lr_log2xml $cfg_flag $LR_SERVICE"
XMLREPORTFILE=$TMPDIR/$PROGRAM.$LR_SERVICE.$LR_ID.report.xml

if lr_log2xml $cfg_flag $LR_SERVICE < $LOGFILE > $XMLREPORTFILE
then
    :
else
    echo >&2 "$tag err lr_log2xml $LR_SERVICE failed"
    echo >&2 "$tag notice keeping $LOGFILE for debug"
    echo >&2 "$tag notice you might like to inspect and clean up the Lire database in $LR_DBFILE"
    rm -f $XMLREPORTFILE
    exit 1
fi

eval `lr_check_errlines`
if test -n "$lr_check_errlines_OK"
then
    :
else
    echo >&2 "$tag warning log file may be bogus: $lr_check_errlines_ERRORLINES errors on $lr_check_errlines_LOGLINES lines"
    echo >&2 "$tag notice will generate a report for the $lr_check_errlines_DLFLINES records extracted"
fi

echo >&2 "$tag info gonna run lr_xml2report $image_flag $output_flag on $XMLREPORTFILE"
if lr_xml2report $image_flag $output_flag $XMLREPORTFILE
then
    echo >&2 "$tag info succeeded in formatting report"
else
    echo >&2 "$tag err lr_xml2report failed"
    echo >&2 "$tag notice keeping $XMLREPORTFILE for debugging."
    exit 1
fi
echo >&2 "$tag info lr_xml2report finished"

echo >&2 "$tag info running lr_archive_log -l $LOGFILE -x $XMLREPORTFILE"
if lr_archive_log -l $LOGFILE -x $XMLREPORTFILE
then
    echo >&2 "$tag info lr_archive_log succeeded"
else
    echo >&2 "$tag info lr_archive_log failed"
    exit 1
fi

echo >&2 "$tag 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<service>

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.

=item B<-i>

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

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

one of B<docbookx>, B<excel95>, B<html>, B<html_page>, B<logml>, B<pdf>,
B<rtf>, B<txt>, B<xhtml>, B<xhtml_page> and B<xml>.  Per default, simple txt
output is generated. The B<logml> output format is only available for the www
and email superservice.  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 the formatted
report on STDOUT. The format of the log file is specified using the I<service>
argument.  (Run B<lr_check_service> B<-l> to get a list of all currently
supported Lire services.)

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

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 SEE ALSO

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

=head1 VERSION

$Id: lr_log2report.in,v 1.78 2002/08/18 19:44:40 flacoste Exp $

=head1 COPYRIGHT

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


