#! /bin/sh -e

# $Id: lr_log2xml.in,v 1.19 2001/11/13 19:47:19 flacoste Exp $

#
# Copyright (C) 2000-2001 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.
#

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

tag="all all $id $PROGRAM"

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

if test $# -lt 2
then
    echo >&2 "$tag err please give at least 2 args, indicating superservice service (flags)"
    exit 1
fi

# e.g. www
SUPERSERVICE="$1"; shift

# e.g. apache
SERVICE="$1"; shift

FLAGS="$@"

tag="$SUPERSERVICE $SERVICE $id $PROGRAM"

prefix="/usr"
etcdir="/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.$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

if test -z "$TODLF"
then
    echo >&2 "$tag err TODLF not set. is '$SERVICE' a service? (You can find a list of valid SERVICEs in $etcdir/defaults.) If this script ($PROGRAM) was not run by lr_log2report, you should have manually sourced $etcdir/defaults, while SERVICE was set."
    exit 1
fi

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

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

if test -r $HOME/.lire/etc/$SUPERSERVICE.cfg
then
    REPORT_CFG=$HOME/.lire/etc/$SUPERSERVICE.cfg
else
    REPORT_CFG=$etcdir/$SUPERSERVICE.cfg
fi

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

if lr_dlf2xml $SUPERSERVICE $REPORT_CFG $DLFFILE
then
    :
else
    echo >&2 "$tag crit lr_dlf2xml $DLFFILE $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

# go deal with tmpfiles and archive
if test -n "$ARCHIVE"
then
    # construct name for archivefile
    TMPFILE=$TMPDIR/$PROGRAM.$SERVICE.$id.$$.time_span
    if lr_db_fetch $id time_span > $TMPFILE
    then
        echo >&2 "$tag info lr_db_fetch $id timespan succeeded"
        LR_TIME=`cat $TMPFILE`
        rm $TMPFILE
    else
        echo >&2 "$tag crit lr_db_fetch $id timespan failed, exiting"
        echo >&2 "$tag notice keeping $TMPFILE for debugging"
        exit 1
    fi
    # LR_EXTID is used to identify e.g. the host the log came from
    ARCHIVEDIR=$LR_ARCHIVEDIR/log/dlf/$SUPERSERVICE/complete/$LR_EXTID
    ARCHIVEFILE=$ARCHIVEDIR/$LR_TIME
    # name constructed, store it

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

echo >&2 "$tag info stopped"

