#! /bin/sh -e

# $Id: lr_log2mail.in,v 1.34 2001/11/10 22:19:50 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_log2mail

# dereference sysconfdir's prefix dependency
prefix="/usr"
etcdir="/etc/lire"
. $etcdir/defaults
. $etcdir/profile_lean

test -z "$LR_ID" && LR_ID=`lr_tag`
export LR_ID
tag="all all $LR_ID $PROGRAM"
echo >&2 "$tag info started with $@"

if test $# -lt 3
then
    echo >&2 "$tag err usage $PROGRAM [-a] [-s subject] superservice service addressee[,addressee...] [flags...]"
    exit 1
fi

ANON=
if test "-a" = "$1"
then
    ANON="-o xml"; shift
fi

subject=
if test "-s" = "$1"
then
    shift
    subject="$1"; shift

    if test -z "$LR_EXTID"
    then
        LR_EXTID=`echo $subject | sed 's/ /_/g'`
        newextid=yes
    fi
fi

test -z "$LR_EXTID" && LR_EXTID=UNSET && newextid=yes
export LR_EXTID

if test -n "$ARCHIVE"
then
    if test -n "$newextid"
    then
        if lr_db_store $LR_ID extid "$LR_EXTID"
        then
            :
        else
            echo >&2 "$tag crit cannot lr_db_store $LR_ID extid $LR_EXTID, exiting"
            exit 1
        fi
    fi
fi

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

# e.g. joe,ann@foo.bar
TO="$1"; shift

FLAGS="$@"

tag="$SUPERSERVICE $SERVICE $LR_ID $PROGRAM"

. $etcdir/profile_lean

if test ! -f "$SIGNATUREFILE"
then
    echo >&2 "$tag err no signaturefile $SIGNATUREFILE, exiting"
    exit 1
fi

if test ! -f "$DISCLAIMERFILE"
then
    echo >&2 "$tag err no disclaimerfile $DISCLAIMERFILE, exiting"
    exit 1
fi

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

REPORTFILE=$TMPDIR/$PROGRAM.$SERVICE.$LR_ID.report
ERRFILE=$TMPDIR/$PROGRAM.$SERVICE.$LR_ID.errors

echo >&2 "$tag info gonna run lr_log2report $ANON $SUPERSERVICE $SERVICE $FLAGS"

if lr_log2report $ANON $ERRFILE $SUPERSERVICE $SERVICE $FLAGS > $REPORTFILE
then
    :
else
    echo >&2 "$tag err lr_log2report $ANON $ERRFILE $SUPERSERVICE $SERVICE $FLAGS failed, exiting"
    echo >&2 "$tag notice keeping $REPORTFILE for debugging, will send error message to $TO"
fi

# wether to sent additional notes
NOTES=
if test -f $ERRFILE
then
    # lr_log2report created errfile: something went wrong
    NOTES=yes
fi

if test -n "$ANON"
then
    FILES=$REPORTFILE
    NOTES=yes
else
    FILES="$REPORTFILE $SIGNATUREFILE $DISCLAIMERFILE"
fi

MAILFILE=$TMPDIR/$PROGRAM.$SERVICE.$LR_ID.mail
HEADERS=$TMPDIR/$PROGRAM.$SERVICE.$LR_ID.mailheaders

if touch $HEADERS
then
    :
else
    echo >&2 "$tag writing $HEADERS failed"
    exit 1
fi

if touch $MAILFILE
then
    :
else
    echo >&2 "$tag writing $MAILFILE failed"
    exit 1
fi

echo "To: $TO" > $HEADERS
test -n "$FROM" && echo "From: $FROM" >> $HEADERS
test -n "$REPLYTO" && echo "Reply-To: $REPLYTO" >> $HEADERS

{ cat <<EOT
Subject: [LogReport] $SUPERSERVICE / $SERVICE report (was: $subject)

EOT
} | cat $HEADERS - $FILES >> $MAILFILE

if test -s $REPORTFILE
then
    if /usr/sbin/sendmail -t < $MAILFILE
    then
	:
    else
	echo >&2 "$tag err sendmail failed"
	exit 1
    fi
fi

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

if test -n "$NOTES"
then
    # send an additional explanation mail

    FILES=

    if test -n "$ANON"
    then
        if test ! -f "$EXPLANATIONFILE"
        then
            echo >&2 "$tag err no explanationfile $EXPLANATIONFILE, exiting"
            exit 1
        fi
        FILES=$EXPLANATIONFILE
    fi

    test -r "$ERRFILE" && FILES="$FILES $ERRFILE"

    FILES="$FILES $SIGNATUREFILE $DISCLAIMERFILE"

    if { cat <<EOT
Subject: [LogReport] notes on $SUPERSERVICE / $SERVICE report (was: $subject)

EOT
} | cat $HEADERS - $FILES | /usr/sbin/sendmail -t
    then
        :
    else
        echo >&2 "$tag err sendmail pipe failed"
        exit 1
    fi
fi

echo >&2 "$tag info sent report about $SERVICE to $TO"

rm $HEADERS

if test -n "$ARCHIVE"
then
    #
    # go deal with tmpfiles and archive
    #
    tmpfile=$TMPDIR/$PROGRAM.$SERVICE.$LR_ID.time_span
    if lr_db_fetch $LR_ID time_span > $tmpfile
    then
        LR_TIME="`cat $tmpfile`"
        rm $tmpfile
    else
        echo >&2 "$tag crit lr_db_fetch $LR_ID time_span failed"
        exit 1
    fi

    # LR_ARCHIVEDIR is set in defaults, something like
    #  /usr/local/var/lib/lire/data

    # TODO FIXME deal with other formats
    ARCHIVEDIR=$LR_ARCHIVEDIR/report/ascii/$SUPERSERVICE/$SERVICE/complete/$LR_EXTID
    ARCHIVEFILE=$ARCHIVEDIR/$LR_TIME

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

if test -n "$KEEP"
then
    echo >&2 "$tag notice keeping possible $ERRFILE on your request. remove manually."
else
    # -f : might not exist
    rm -f $ERRFILE
fi

echo >&2 "$tag info stopped"

