#! /bin/sh -e

# $Id: lr_archive_log.in,v 1.9 2002/08/18 19:10:04 flacoste Exp $

#
# Copyright (C) 2002 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_archive_log

tag="${LR_SUPERSERVICE:-UNSET} ${LR_SERVICE:-UNSET} ${LR_ID:-UNSET} $PROGRAM"
echo >&2 "$tag info started with $*"

if test -z "$LR_SERVICE"
then
    echo >&2 "$tag err LR_SERVICE is unset"
    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

if test -z "$LR_ID"
then
    echo >&2 "$tag err LR_ID is unset"
    exit 1
fi

if test -z "$LR_EXTID"
then
    echo >&2 "$tag info LR_EXTID unset, setting to 'localhost'"
    LR_EXTID='localhost'
fi

if test -z "$LR_GZIP"
then
    echo >&2 "$tag err gzip(1) isn't available"
    exit 1
elif test ! -x "$LR_GZIP"
then
    echo >&2 "$tag err $LR_GZIP isn't executable"
    exit 1
fi

USAGE="Usage $PROGRAM [-l logfile] [-l logfile] ... [-x xmlreportfile] [-x xmlreportfile] ..."

LOGS=''
XMLS=''

while getopts :x:l:vh\? o
do
    case $o in
        l) LOGS="$LOGS $OPTARG"
            ;;
        x) XMLS="$XMLS $OPTARG"
            ;;
        h | "?")
           echo >&2 "$tag notice $USAGE"
           exit 0
           ;;
        v)
           echo >&2 "$tag notice $PROGRAM" '$Id: lr_archive_log.in,v 1.9 2002/08/18 19:10:04 flacoste Exp $,' "shipped with Lire 1.1"
           exit 0
           ;;
        *)
           echo >&2 "$tag err $USAGE"
           exit 1
           ;;
     esac
done


if test -n "$LR_ARCHIVE"
then
    XMLARCHIVEDIR=$LR_ARCHIVEDIR/report/xml/$LR_SUPERSERVICE/$LR_SERVICE/$LR_EXTID
    XMLARCHIVEFILE=$XMLARCHIVEDIR/$LR_ID.xml.gz

    LOGARCHIVEDIR=$LR_ARCHIVEDIR/log/raw/$LR_SUPERSERVICE/$LR_SERVICE/$LR_EXTID
    LOGARCHIVEFILE=$LOGARCHIVEDIR/$LR_ID.gz

    test -n "$XMLS" && {
        test -d $XMLARCHIVEDIR || mkdir -p $XMLARCHIVEDIR
    }
    for x in $XMLS
    do
        echo >&2 "$tag notice storing compressed $x in $XMLARCHIVEFILE"
        $LR_GZIP --best -c $x > $XMLARCHIVEFILE
    done

    test -n "$LOGS" && {
        test -d $LOGARCHIVEDIR || mkdir -p $LOGARCHIVEDIR
    }
    for l in $LOGS
    do
        echo >&2 "$tag notice storing compressed $l in $LOGARCHIVEFILE"
        $LR_GZIP --best -c $l > $LOGARCHIVEFILE
    done
fi

if test -n "$LR_KEEP"
then
    test -n "$XMLS" && echo >&2 "$tag notice keeping $XMLS on your request. remove manually."
    test -n "$LOGS" && echo >&2 "$tag notice keeping $LOGS on your request. remove manually."
else
    test -n "$XMLS" && rm $XMLS
    test -n "$LOGS" && rm $LOGS
fi

echo >&2 "$tag info stopped"

exit 0

POD=<<'EOFPOD'

=pod

=head1 NAME

lr_archive_log - internal script that handles archiving log and report files

=head1 SYNOPSIS

B<lr_archive_log> B<[-l> I<logfile>B<]> B<[-l> I<logfile>B<]> ... B<[-x> I<xmlreportfile>B<]> B<[-x> I<xmlreportfile>B<]> ...

=head1 DESCRIPTION

B<lr_archive_log> is an internal script used by lr_log2report(1) and
lr_log2mail(1). It should be called at the end of the job to archive (or not)
the generated XML report and log file.

This script will keep, remove or archive the log and XML report
files depending on the value of I<LR_KEEP> and I<LR_ARCHIVE>.

When I<LR_ARCHIVE> is set, a compressed copy of the log and XML report
file will be stored in Lire's archive. The XML report will be
saved under F<LR_ARCHIVE_DIR/report/xml/LR_SUPERSERVICE/LR_SERVICE/LR_EXTID>
and the report will be saved under
F<LR_ARCHIVE_DIR/log/raw/LR_SUPERSERVICE/LR_SERVICE/LR_EXTID>. The filenames
will be I<LR_TIME>.gz and I<LR_TIME>.xml.gz respectively.

The log and XML report files will be removed unless I<LR_KEEP> is set.

=head1 ENVIRONMENT

The following environment variables must set and exported prior to
calling this script: I<LR_SERVICE> and I<LR_ID>.
Also, the F<defaults> file should have been sourced.

=head1 VERSION

This manpage: $Id: lr_archive_log.in,v 1.9 2002/08/18 19:10:04 flacoste Exp $

=head1 COPYRIGHT

Copyright (C) 2002 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.

=head1 AUTHOR

Francis J. Lacoste <flacoste@logreport.org>

=cut

EOFPOD

