#! /bin/sh -e

PROGRAM=lr_log2xml

prefix="/usr"
etcdir="/etc/lire"
. $etcdir/defaults

lire_log "info started with $*"

USAGE="Usage: $PROGRAM [-c <report_cfg>] <service> <stats_file>"

# get options (x)
REPORT_CFG=""
while getopts :c: o; do
    case $o in
	c) if test -r $OPTARG
	   then
		REPORT_CFG="$OPTARG"
	   else
		lire_log "err can't read report configuration '$OPTARG'"
		exit 1
	   fi
	   ;;
        *)
           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 2
then
    lire_log "err $USAGE"
    exit 1
fi

LR_SERVICE="$1"
STATSFILE="$2"

LR_SERVICE=`lr_check_service "$LR_SERVICE"`
if test -z "$LR_SERVICE"
then
    lire_log "err lr_check_service failed."
    exit 1
fi

LR_SUPERSERVICE=`lr_check_superservice "$LR_SERVICE"`
if test -z "$LR_SUPERSERVICE"
then
    lire_log "err lr_check_superservice failed"
    exit 1
fi

tmpdir=`lr_tempdir $PROGRAM.XXXXXX`
lr_mark_for_cleanup $tmpdir
TMPSTORE=$tmpdir/lr_log2xml_dlf_store

lr_store create $TMPSTORE
lr_store import_log $TMPSTORE $LR_SERVICE >> $STATSFILE

if test -z "$REPORT_CFG"
then
    REPORT_CFG=`find_default_report_cfg $LR_SUPERSERVICE`
fi

if lr_dlf2xml $LR_SUPERSERVICE $REPORT_CFG $TMPSTORE
then
    :
else
    lire_log "err lr_dlf2xml $LR_SERVICE $STATSFILE failed, stuff on stdout probably bogus"
    exit 1
fi

if echo " $lr_sql_archive " | grep " $LR_SUPERSERVICE " 2>&1 > /dev/null
then
    lire_log "warning SQL archiving isn't supported anymore"
fi

lire_log "info stopped"

exit 0

POD=<<'EOPOD'

=pod

=head1 NAME

lr_log2xml - generate a report in raw XML format from a log file

=head1 SYNOPSIS

B<lr_log2xml> B<[>I<options>B<]> I<service> I<stats_file>

=head1 OPTIONS

=over 4

=item B<-c> I<report_cfg>

Use the file I<report_cfg> as the report configuration file instead of
the default one.

=back

=head1 DESCRIPTION

B<lr_log2xml> reads a logfile on stdin, feeds it to a DLF Converter using the
lr_store(1) C<import_log> interface, and processes the filled store with
lr_dlf2xml(1).

The I<superservice> to which I<service> belongs is contructed, using
lr_check_superservice(1).

If a file $HOME/.lire/etc/I<superservice>.cfg exists, this is used as the
report configuration file for lr_dlf2xml(1), otherwise
/etc/lire/<superservice>.cfg is used.  Use the B<-c> flag to
override this.

A report in raw XML format is generated, and written to stdout.

This scriptlet is normally not run directly from the commandline, but run by
lr_log2report(1) and lr_log2mail(1).

=head1 SEE ALSO

lr_log2report(1), lr_log2mail(1), lr_dlf2xml(1), lr_store(1), documentation in
the Lire User Manual

=head1 VERSION

$Id: lr_log2xml.in,v 1.34 2004/03/21 17:31:27 vanbaal Exp $

=head1 COPYRIGHT

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


