# vim: syntax=sh
# $Id: lr_functions.in,v 1.6 2004/01/21 07:23:00 wsourdeau Exp $

#
# Copyright (C) 2003 Stichting LogReport Foundation LogReport@LogReport.org
#
#     This file 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.
#

########################################################################
# lire_log ()
#
# Write a message using Lire logging format.
lire_log () {
    echo "${LR_SUPERSERVICE:-all} ${LR_SERVICE:-all} ${LR_ID:-none} ${PROGRAM:-unknown} $@" >&2
}

########################################################################
# lr_tag ()
#
# Generate something that can be used as LR_ID
lr_tag () {
    echo lr_tag-`date +%Y%m%d%H%M%S`-${$}
}

########################################################################
# find_default_report_cfg ( <superservice> )
#
# Returns the default report configuration file for the <superservice>
# 
find_default_report_cfg () {
    if test $# -ne 1
    then
        lire_log "missing 'superservice' parameter to find_default_report_cfg"
        return 1
    fi
    if test -r $HOME/.lire/etc/$1.cfg
    then
        echo $HOME/.lire/etc/$1.cfg
    else
        echo /etc/lire/$1.cfg
    fi
}

########################################################################
# lire_tempfile( template )
#
# Create a temporary file and prints the filename on stdout
lr_tempfile () {
    if test $# -ne 1
    then
        lire_log "missing 'template' parameter to lr_tempfile"
        return 1
    fi
    perl -MLire::Utils=tempfile -e '
my ( $template, $suffix ) = $ARGV[0] =~ /(.*?)(\.[^\.]+)$/;
my ( $fh, $filename ) = tempfile( $template, SUFFIX => $suffix ); 
print $filename, "\n"
' $1
}

########################################################################
# lr_tempdir ( template )
#
# Create temporary directory print its name
lr_tempdir () {
    if test $# -ne 1
    then
        lire_log "missing 'template' parameter to lr_tempdir"
        return 1
    fi
    perl -MLire::Utils=tempdir -e 'print tempdir( $ARGV[0] ), "\n"' $1
}

########################################################################
# lr_archive_log
#
# Print notice mesage when lr_archive is set
lr_archive_log () {
    if test -n "$lr_archive"
    then
        lire_log "notice lr_archive is now only used by the Online Responder."
        lire_log "notice archiving of log files and XML reports is now outside"
        lire_log "notice the scope of the $PROGRAM command."
    fi
}

#########################################################################
# lr_check_log2xml_stats
#
# check that error ratio is above a ratio. Sets the env var
# lr_import_log_OK=1 if this was the case.
lr_check_import_log_stats () {
    if test ! -f $1
    then
        lire_log "err Usage lr_check_import_log_stats stats_file"
        return 1
    fi
    eval `cat $1`

    # Check the percentage of lines that failed
    # max value for error lines / log lines.
    # When there is more then 5% error rate, we suspect there is a bogus log file
    MAXERRORRATE="0.05"

    if test 0 -lt "$lr_import_line_count"
    then
        lr_import_OK=`/usr/bin/perl -e "print \"1\\n\" if $lr_import_error_count / $lr_import_line_count < 0.05"`
    else
        # Empty log file, this is not an error 
        lr_import_OK=1
    fi
    if test 1 != "$lr_import_OK"
    then
        lire_log "warning more than 5% error rate"
    fi
}

# Local Variables:
# mode: sh
# End:


