#! /bin/sh -e

# $Id: lr_rawmail2mail.in,v 1.15 2001/10/18 19:13:17 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_rawmail2mail
identifier=${LR_ID:-UNSET}
tag="all all $identifier $PROGRAM"

if test $# -ne 2
then
    echo >&2 "$tag err please give 2 args, indicating dumpstem addressee"
    # when running directly from .qmail:
    # exit 100 : hard error
    # exit 111 : soft error
    # see qmail-command
    exit 1
fi  

# dereference sysconfdir's prefix dependency
prefix="/usr"
etcdir="/etc/lire"

. $etcdir/defaults
# get sane path
. $etcdir/profile_lean

if test -z "$TMPDIR"
then
    echo >&2 "$tag err TMPDIR not set. did you source sysconfdir/defaults?"
    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

DUMPSTEM="$1"
shift
TO="$1"

MAILFILE=$TMPDIR/$PROGRAM.$$.mail
cat > $MAILFILE

SHFILE=$TMPDIR/$PROGRAM.$$.sh
# this sets lr_getbody_BODYFILE, lr_getbody_SUBJECTFILE
# and lr_getbody_SUBMITTER
if lr_getbody -i $identifier -f $MAILFILE > $SHFILE
then
    . $SHFILE
    rm $SHFILE
else
    echo >&2 "$tag err lr_getbody -i $identifier -f $MAILFILE failed"
    echo >&2 "$tag notice keeping $MAILFILE and $SHFILE for debug"
    exit 1
fi 

REPORTFILE=$TMPDIR/$PROGRAM.$$.report

# SunOS's grep doesn't grok -q
if grep '^\[LogReport\]' < $lr_getbody_SUBJECTFILE > /dev/null
then
    echo >&2 "$tag info file $MAILFILE seems to be a message from LogReport"
    if grep ' notes on ' < $lr_getbody_SUBJECTFILE > /dev/null
    then
        # skip the explanation messages
        :
    else
        echo >&2 "$tag info file $lr_getbody_BODYFILE contains a raw xml report"
        # this does NOT exit when lr_xml2ascii exits unsuccessfully... XXX
        lr_deanonimize $DUMPSTEM < $lr_getbody_BODYFILE | lr_xml2ascii \
            > $REPORTFILE

        echo >&2 "$tag info gonna sent $REPORTFILE to $TO"

        # create new mailfile

        # get evil stuff out of lr_getbody_SUBJECTFILE
        sed 's/[^-_\.:a-zA-Z0-9 ]//g' $lr_getbody_SUBJECTFILE | head -1 \
            > $TMPDIR/$PROGRAM.$$.tmp
        mv $TMPDIR/$PROGRAM.$$.tmp $lr_getbody_SUBJECTFILE
        SUBJECT=`cat $lr_getbody_SUBJECTFILE`

        if { cat <<EOT
To: $TO
Subject: [LogReport] deanonimized report (was: $SUBJECT)

EOT
} | cat - $REPORTFILE > $MAILFILE
        then
            :
        else
            echo >&2 "$tag err writing $MAILFILE failed"
            exit 1
        fi

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

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

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

