#! /bin/sh -e

PROGRAM=lr_xml2mail

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

test -z "$LR_ID" && LR_ID=`lr_tag`
export LR_ID

lire_log "info started with $*"

USAGE="Usage: $PROGRAM [-s subject] [-i] [-o outputformat] -x report.xml [-x report.xml] ... sendto..."
# get options (x)
OUTPUTFORMAT=""
image_flag=""
subject="[LogReport]"
XMLS=''

while getopts :o:s:U:S:x:ivh\? o; do
    case $o in
        o)  OUTPUTFORMAT=$OPTARG
            ;;
	i)
	    image_flag="-i"
	    ;;
	s)
	    subject=$OPTARG;
            ;;
        U)
            # needed by lr_xml2report
            LR_SUPERSERVICE="$OPTARG"
	    ;;
        S)
            # Pre 1.3 backward compatibility
            LR_SERVICE="$OPTARG"
            ;;
        x)
            XMLS="$XMLS $OPTARG"
            ;;
        h | "?")
            lire_log "notice $USAGE"
            exit 0
            ;;
	v)
	    lire_log "notice $PROGRAM" '$Id: lr_xml2mail.in,v 1.19 2004/03/30 17:50:53 wsourdeau Exp $,' "shipped with Lire 1.5"
	    exit 0
	    ;;
        *)
            lire_log "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 1
then
            lire_log "err $USAGE"
    exit 1
fi

lire_log "info will send to $*"


if test -z "$OUTPUTFORMAT"
then
    OUTPUTFORMAT="$lr_output_format"
fi

# Make sure that we use the one page report, they work
# better in emails
ext=
type=
case $OUTPUTFORMAT  in
    html|html_page)
	type="application/x-lire-html-report"
	ext="tar"
	OUTPUTFORMAT="html_page"
	;;
    xhtml|xhtml_page)
	type="application/x-lire-xhtml-report"
	ext="tar"
	OUTPUTFORMAT="xhtml_page"
	;;
    docbookx)
	type="application/tar"
	ext="tar"
    	;;
    txt)
	type="text/plain"
	ext="txt"
    	;;
    pdf)
	type="application/pdf"
	ext="pdf"
    	;;
    xml)
	type="application/x-lire-report"
	ext="xml"
	;;
    excel95)
	type="application/vnd.ms-excel"
	ext="xls"
    	;;
esac

tmpdir=`lr_tempdir $PROGRAM.XXXXXX`
lr_mark_for_cleanup $tmpdir
lire_log "debug creating temporary report dir $tmpdir/report"
if mkdir $tmpdir/report
then
    :
else
    lire_log "err failed to create $tmpdir/report"
    exit 1
fi

# Just so that the filename makes sense
reportfile="$tmpdir/report/report.$ext"
output_flag="-o $OUTPUTFORMAT"
# LR_SERVICE and LR_SUPERSERVICE are passed to lr_xml2report via the
# environment
lire_log "info running lr_xml2report $image_flag $output_flag $XMLS"
if lr_xml2report $image_flag $output_flag $XMLS > $reportfile
then
    lire_log "info lr_xml2report completed successfully"
else
    lire_log "err lr_xml2report failed"
    lire_log "notice keeping tmpfiles in $tmpdir"
    exit 1
fi

extrafile="$tmpdir/signature.txt"
if touch $extrafile
then
    :
else
    lire_log "err can't create $extrafile"
    exit 1
fi

if test $OUTPUTFORMAT = "xml" && test -f "$lr_mail_explanation"
then
    cat "$lr_mail_explanation" >> $extrafile
else
    case $OUTPUTFORMAT in
    txt|html*|xhtml*)
	;;
    *)
	cat >> $extrafile <<EOF
Here is your $OUTPUTFORMAT report.

EOF
	;;
    esac
fi

if test -f "$lr_mail_signature"
then
    cat "$lr_mail_signature" >> $extrafile
fi

if test -f "$lr_mail_signature"
then
    cat "$lr_mail_disclaimer" >> $extrafile
fi

attachflag=
mainfile=
maintype=
case $OUTPUTFORMAT in
    txt)
	# Append extra stuff to the report
	cat $extrafile >> $reportfile
	mainfile="$reportfile"
	maintype="$type"
	;;
    html*|xhtml*)
	# Extra stuff comes after the report
	mainfile="$reportfile"
	maintype="$type"
	if test -s $extrafile
	then
	    attachflag="-c text/plain -a $extrafile"
	fi
	;;
    *)
	# Report is in an attachment
	if test -s $extrafile
	then
	    mainfile="$extrafile"
	    maintype="text/plain"
	    attachflag="-c $type -a $reportfile"
	else
	    mainfile="$reportfile"
	    maintype="$type"
	fi
	;;
esac

# lr_mail handles LR_FROM and LR_REPLYTO itself
lire_log "info running lr_mail -s \"$subject\" $type $reportfile $*"
if lr_mail -s "$subject" $attachflag $maintype $mainfile "$@"
then
    lire_log "info lr_mail suceeded"
else
    lire_log "err lr_mail failed"
    lire_log "notice keeping tmpfiles in $tmpdir"
    exit 1
fi

lire_log "info stopped"

exit 0

POD=<<'EOPOD'

=pod

=head1 NAME

lr_xml2mail - Formats a XML report and sends it by email

=head1 SYNOPSIS

B<lr_xml2mail> B<[>I<options>B<]> B<-x> I<report.xml> B<[-x> I<report.xml>B<]> ... I<sendto>...

B<lr_xml2mail> B<-h>

B<lr_xml2mail> 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<docbookx>, B<excel95>, B<html>, B<html_page>, B<pdf>,
B<txt>, B<xhtml>, B<xhtml_page> and B<xml>.  Per default, simple txt
output is generated. The B<html> and B<xhtml> output format behave exactly
the same as B<html_page> and B<xhtml_page>, respectively: they generate the
complete report on one page, since this is nicer in email messages.  The
B<html> and B<xhtml> options are just supplied for compatibity with
lr_log2report(1) and friends. (Refer to the Lire User Manual for a description
of those formats.)

=item B<-U> I<superservice>

in case multiple XML report files are specified, the Lire
I<superservice> needs to be known. If this script is called by a
wrapper, the wrapper script should make sure the LR_SUPERSERVICE
environment variables is set. When calling this script by hand, one
could use the B<-U> option.

=item B<-s> I<subject>

Sets the subject of the email message. Defaults to [LogReport].

=item B<-v>

Prints version and exits.

=back

=head1 DESCRIPTION

B<lr_xml2mail> formats the XML reports generated by Lire to various
output format and sends it by email to the recipients specified as
remaining arguments.

The command will make sure the report is MIME encapsulated
properly depending on the output format chosen. For example, a PDF
report will be sent as an attachment and (X)HTML output will appear as
an HTML mail message. The content-type of the MIME message will also
be set properly.

If the I<LR_FROM> environment variable is set, its content will be used
in the From header.

If the I<LR_REPLYTO> environment variable is set, its content will be
used in the Reply-To header.

=head1 NOTES ON THE (X)HTML OUTPUT

With this command, the B<html> and B<xhtml> output formats are
strictly equivalent to the B<html_page> and B<xhtml_page> since
multiple pages aren't handled too well by most HTML capable mailers. If
the B<-i> is used, the images will also be embedded in the email
message and should appear inline in most HTML mail readers.

=head1 NOTES ON THE XML OUTPUT

The B<xml> output format will not format the XML report but will send it by
email. The content-type will be set to B<application/x-lire-report> so that it
can be recognised by a lr_rawmail2mail(1) running at the other end. This is
useful in the case that the log file was anonymized and you want to deanonymize
the generated report before formatting it.

=head1 SEE ALSO

lr_rawmail2mail(1), lr_log2mail(1), lr_xml2report(1)

=head1 VERSION

$Id: lr_xml2mail.in,v 1.19 2004/03/30 17:50:53 wsourdeau Exp $

=head1 COPYRIGHT

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

=head1 AUTHOR

Francis J. Lacoste <flacoste@logreport.org>

=cut

EOPOD


