#! /bin/sh -e

# $Id: lr_cron.in,v 1.10 2002/04/13 22:55:16 vanbaal Exp $

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

# dereference sysconfdir's prefix dependency
prefix="/usr/local"
etcdir="${prefix}/etc/lire"

. $etcdir/profile_lean
. $etcdir/defaults

test $# -eq 1 || {
    echo >&2 "$PROGRAM: give arg 'daily' or 'weekly'" &&
    exit 1
}

period="$1";

test $period = weekly -o $period = daily || {
    echo >&2 "$PROGRAM: give arg 'daily' or 'weekly'" &&
    exit 1
}

eval jobs=\${lr_jobfiles_$period}

#
# we silently exit in case lr_jobfiles_$period apparently was empty or unset
#
test -n "$jobs" || exit 0

for f in $jobs
do

    test -r "$f" || {
        echo >&2 "$PROGRAM: file '$f' from joblist lr_jobfiles_$period not readable" &&
        exit 1
    }

    . $f

    test -r "$log" || {
        echo >&2 "$PROGRAM: logfile '$log' from '$f' not readable" &&
        exit 1
    }

    test -n "$filter" || {
        echo >&2 "$PROGRAM: jobfile '$f' didn't set filter" &&
        exit 1
    }

    lr_run $filter < $log | \
      lr_run lr_log2mail -s "$subject" $superservice $service "$to" $flags
done

exit 0

