#! /bin/sh -e

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"
etcdir="/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.25 2006/07/23 13:16:33 vanbaal Exp $,' "shipped with Lire 2.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

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 text/xml $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 text/xml 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 -q -s "$subject" $image_flag $output_flag $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

exit 0

POD=<<'EOPOD'

=pod

=head1 NAME

B<lr_rawmail2mail> - deanonymizes and formats a report from an email message

=head1 SYNOPSIS

B<lr_rawmail2mail> B<[I<options>]> I<dumpstem> I<addresses>...

B<lr_rawmail2mail> B<-h>

B<lr_rawmail2mail> B<-v>

=head1 OPTIONS

=over 4

=item B<-h>

Prints usage information and exits.

=item B<-i>

Generate charts for all reports that support it. Normally, charts will
be generated only if the variable I<LR_INCLUDEIMAGES> is set to 1.

=item B<-o> I<output format>

One of B<pdf>, B<html>, B<html_page>, B<xhtml>, B<xhtml_page>, B<txt>,
B<docbookx>, B<rtf> and B<xml>. Per default, simple txt output is
generated.

=item B<-v>

Prints Lire's version and exits.

=back

=head1 DESCRIPTION

B<lr_rawmail2mail> can be used to automatically process reply from an
Online Responder which contains a XML report. It will extract the
report (which should have a content type of text/xml)
and deanonymized it. It will then use the lr_xml2mail(1) command to
formats and send the report to the recipients specified on the command
line.

The I<dumpstem> argument is used by lr_deanonymize(1) to find the database
that was used in the anonymization process.

Environment variable TMPDIR, as set in sysconfdir/defaults, is used to
store tmpfiles; if LR_KEEP is set to a nonempty string, temporary files
are kept.

=head1 VERSION

$Id: lr_rawmail2mail.in,v 1.25 2006/07/23 13:16:33 vanbaal Exp $

=head1 COPYRIGHT

Copyright (C) 2000, 2001, 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.

=head1 AUTHOR

Joost van Baal <joostvb@logreport.org>

=cut

EOPOD

