#! /bin/sh -e

PROGRAM=lr_sql2report

# 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 $*"

#
# FIXME: Is it possible to move common functionalities (output format, report 
# file, etc) to modules that could be shared with lr_log2report?
#
USAGE="Usage $PROGRAM [-c <report_cfg>] [-i] [-o <outputformat>] superservice datetime-start datetime-end "

# 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_sql2report.in,v 1.4 2002/08/16 22:02:55 vanbaal 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 $# -ne 3
then
    echo >&2 "$tag err $USAGE"
    exit 1
fi

# e.g. www
LR_SUPERSERVICE="$1"; shift
DATE_BEGIN="$1"; shift
DATE_END="$1"; shift

test -z "$LR_SERVICE" && LR_SERVICE="all"

tag="$LR_SUPERSERVICE $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


echo >&2 "$tag info gonna run lr_sql2dlf $LR_SUPERSERVICE $DLFFILE $DATE_BEGIN $DATE_END"

DLFFILE=$TMPDIR/$PROGRAM.$LR_SERVICE.$LR_ID.dlf
if touch $DLFFILE
then
    :
else
    echo >&2 "$tag crit cannot write $DLFFILE, exiting"
    exit 1
fi

lr_sql2dlf $LR_SUPERSERVICE $DLFFILE "$DATE_BEGIN" "$DATE_END"
echo >&2 "$tag info finished lr_sql2dlf"

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"


XMLREPORTFILE=$TMPDIR/$PROGRAM.$LR_SERVICE.$LR_ID.report.xml
if lr_dlf2xml $LR_SUPERSERVICE $REPORT_CFG $DLFFILE > $XMLREPORTFILE
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 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 -x $XMLREPORTFILE"
if lr_archive_log -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_sql2report - generate a report from Lire DLF data stored in an SQL database

=head1 SYNOPSIS

B<lr_sql2report> B<[>I<options>B<]> I<superservice> I<datetime-start> I<datetime-end>

=head1 OPTIONS

=over 4

=item B<-h>

Prints usage information and exits.

=item B<-i>

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

=item B<-c> I<report_cfg>

Use the file I<report_cfg> (e.g. /etc/lire/print.cfg.local) as the report
configuration file instead of the default one.

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

Per default, simple text output is generated.  Specify preferred output format.
Refer to lr_log2report(1) for a list of supported output formats.

=item B<-v>

Prints version and exits.

=back

=head1 DESCRIPTION

B<lr_sql2report> reports on the period I<datetime-start> I<datetime-end>, using
DLF data stored in an SQL database.  It calls lr_sql2dlf(1), lr_dlf2xml(1) and
lr_xml2report(1) to perform the real work.  The arguments I<datetime-start> and
I<datetime-end> are passed as-is to lr_sql2dlf(1); therefore, these should be
specified using YYYY-MM-DD HH:MM format.

=head1 SEE ALSO

lr_sql2dlf(1), lr_dlf2xml(1), lr_xml2report(1)

=head1 AUTHOR

Arnaud Gaillard with Francis Lacoste

=head1 VERSION

$Id: lr_sql2report.in,v 1.4 2002/08/16 22:02:55 vanbaal Exp $

=head1 COPYRIGHT

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

Arnaud Gaillard <wireless@orange.ch>

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.

=cut

EOPOD

