#! /bin/sh -e

# $Id: lr_rawmail2mail.in,v 1.20 2002/06/21 23:00:34 vanbaal 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
tag="all all ${LR_ID:-UNSET} $PROGRAM"

# when running directly from .qmail:
# exit 100 : hard error
# exit 111 : soft error
# see qmail-command

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

. $etcdir/profile_lean

echo >&2 "$tag info started with $*"

USAGE="Usage $PROGRAM [-i] [-o <outputformat>] anondb sendto+"

# get options (x)
output_flag=""
image_flag=""
subject=""
while getopts :o:ivh\? o; do
    case $o in
        o)  output_flag="-o $OPTARG"
            ;;
	i)  image_flag="-i"
	    ;;
        h | "?")
            echo >&2 "$tag notice $USAGE"
            exit 0
            ;;
        v)
            echo >&2 "$tag notice $PROGRAM" '$Id: lr_rawmail2mail.in,v 1.20 2002/06/21 23:00:34 vanbaal Exp $,' "shipped with Lire 1.1"
            exit 0
            ;;
        *)
            echo >&2 "$tag err $USAGE"
            exit 1
            ;;
    esac
done

# yes, the '|| true' is needed. solaris' /bin/sh feels it should bawl out when
# encountering an 'expr 1 - 1', while running -e
shift `expr $OPTIND - 1 || true`

if test $# -lt 2
then
    echo >&2 "$tag err $USAGE"
    exit 1
fi

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

if /usr/bin/perl -MMIME::Tools -e 'exit 0' 2>/dev/null
then
    :
else
    cat >&2 <<EOF
$tag err can't parse email: the needed MIME::Entity perl module isn't available
$tag err Please install the MIME::Tools perl modules from a local CPAN mirror.
$tag err Consult the Lire User's Manual for more informations.
EOF
    #' Fontification hack
    exit 1
fi

# Test output format requirements
if lr_xml2report -t $image_flag $output_flag /dev/null
then
    :
else
    echo >&2 "$tag err missing requirements for the selected output format"
    exit 1
fi

DUMPSTEM="$1"; shift
# TO= "$@"

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

# this sets lr_getbody_FILE, lr_getbody_SUBJECT
# and lr_getbody_SUBMITTER
eval `lr_getbody -c application/x-lire-report $MAILFILE || true`
if test -z "lr_getbody_OK"
then
    echo >&2 "$tag err lr_getbody failed. Keeping $MAILFILE for debugging"
    exit 1
else
    echo >&2 "$tag info lr_getbody gave FILE '$lr_getbody_FILE'"
    echo >&2 "$tag info lr_getbody gave SUBJECT '$lr_getbody_SUBJECT'"
    echo >&2 "$tag info lr_getbody gave SUBMITTER '$lr_getbody_SUBMITTER'"
fi

if test -n "$lr_getbody_FILE"
then
    # We found an application/x-lire-report attachment
    echo >&2 "$tag info file $MAILFILE is a message containing a XML report"

    REPORTFILE=$TMPDIR/$PROGRAM.$$.xml

    # Deanonymize the report
    if lr_deanonymize $DUMPSTEM < $lr_getbody_FILE > $REPORTFILE
    then
	:
    else
	echo >&2 "$tag err lr_deanonymize failed. Keeping $lr_getbody_FILE for debugging"
	exit 1
    fi

    echo >&2 "$tag info Formatting and sending report to $@"
    subject="[LogReport] deanonymized report (was: $lr_getbody_SUBJECT)"
    if lr_xml2mail -s "$subject" $image_flag $output_flag -x $REPORTFILE "$@"
    then
	:
    else
	echo >&2 "$tag err lr_xml2mail failed. Keeping $lr_getbody_FILE for debugging"
	exit 1
    fi

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

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

