#! /bin/sh -e

PROGRAM=lr_log2xml
id=${LR_ID:-UNSET}

tag="all all $id $PROGRAM"

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

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

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

LR_SERVICE="$1"; shift

if test $# -gt 0
then
    echo >&2 "$tag warning ignoring extra arguments $* (since Lire 1.1, no superservice argument is required any longer)"
fi

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

LR_SUPERSERVICE=`lr_check_superservice "$LR_SERVICE"`
if test -z "$LR_SUPERSERVICE"
then
    echo >&2 "$tag err lr_check_superservice failed"
    exit 1
fi

tag="$LR_SUPERSERVICE $LR_SERVICE $id $PROGRAM"

prefix="/usr/local"
etcdir="${prefix}/etc/lire"

if test -z "$TMPDIR"
then
    echo >&2 "$tag err TMPDIR not set. did you source $etcdir/defaults?"
    exit 1
fi

DLFFILE=$TMPDIR/$PROGRAM.$LR_SERVICE.$id.$$.dlf

if test ! -d "$TMPDIR"
then
    echo >&2 "$tag notice dir $TMPDIR does not exist, creating it"
    if mkdir $TMPDIR
    then
        :
    else
        echo >&2 "$tag crit cannot create $TMPDIR, exiting"
        exit 1
    fi
fi

if touch $DLFFILE
then
    :
else
    echo >&2 "$tag crit cannot write $DLFFILE, exiting"
    exit 1
fi


LR_TODLF=${LR_SERVICE}2dlf


echo >&2 "$tag info gonna run $LR_TODLF > $DLFFILE"
if $LR_TODLF > $DLFFILE
then
    :
else
    echo >&2 "$tag crit $LR_TODLF failed, exiting"
    echo >&2 "$tag notice keeping $DLFFILE for debugging."
    exit 1
fi

echo >&2 "$tag info finished $LR_TODLF"

if test -n "$REPORT_CFG"
then
    :
elif test -r $HOME/.lire/etc/$LR_SUPERSERVICE.cfg
then
    REPORT_CFG=$HOME/.lire/etc/$LR_SUPERSERVICE.cfg
else
    REPORT_CFG=$etcdir/$LR_SUPERSERVICE.cfg
fi

echo >&2 "$tag info gonna run lr_dlf2xml $LR_SUPERSERVICE $REPORT_CFG $DLFFILE"

if lr_dlf2xml $LR_SUPERSERVICE $REPORT_CFG $DLFFILE
then
    :
else
    echo >&2 "$tag crit lr_dlf2xml $DLFFILE $LR_SUPERSERVICE failed, stuff on stdout probably bogus"
    echo >&2 "$tag notice keeping $DLFFILE for debugging"
    exit 1
fi

echo >&2 "$tag info lr_dlf2xml finished"
# now time_span, dlflines, loglines and errorlines is in database

if echo " $LR_SQL_ARCHIVE " | grep " $LR_SUPERSERVICE " 2>&1 > /dev/null
then
    echo >&2 "$tag info SQL archiving is enabled for $LR_SUPERSERVICE"
    if lr_dlf2sql $LR_SUPERSERVICE $DLFFILE
    then
	echo >&2 "$tag info DLF was archived sucessfully by lr_dlf2sql"
    else
	echo >&2 "$tag err lr_dlf2sql failed"
    fi
fi

# go deal with tmpfiles and archive
if test -n "$LR_ARCHIVE"
then
    # LR_EXTID is used to identify e.g. the host the log came from
    ARCHIVEDIR=$LR_ARCHIVEDIR/log/dlf/$LR_SUPERSERVICE/complete/$LR_EXTID
    ARCHIVEFILE=$ARCHIVEDIR/$LR_ID
    # name constructed, store it

    echo >&2 "$tag notice storing $DLFFILE in $ARCHIVEFILE"
    test -d $ARCHIVEDIR || mkdir -p $ARCHIVEDIR
    mv $DLFFILE $ARCHIVEFILE
elif test -n "$LR_KEEP"
then
    echo >&2 "$tag notice keeping $DLFFILE on your request. remove manually."
else
    rm $DLFFILE
fi

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

=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 2dlf convertor,
and feeds the convertors output to lr_dlf2xml(1). The environment
variable I<LR_TODLF> should point to a 2dlf convertor. A report in raw XML
format is generated, and written to stdout. Furthermore, the key
'dlflines' is stored in the Lire database.

It runs lr_dlf2sql(1), if asked so by the $LR_SQL_ARCHIVE environment variable.

B<lr_log2xml> needs a tweaked path, and a TMPDIR environment variable. These
are generally set in sysconfdir/profile_lean and sysconfdir/defaults.

The environment variable I<LR_ID> is used in tags in debug messages, which get
printed to stdout.

This scriptlet is run by lr_log2report(1) and lr_log2mail(1).

=head1 SEE ALSO

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

=head1 VERSION

$Id: lr_log2xml.in,v 1.27 2002/08/18 12:58:51 vanbaal 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


