#! /bin/sh -e

# $Id: lr_xml2mail.in,v 1.14 2002/08/16 22:02:55 vanbaal Exp $

#
# 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.
#

PROGRAM=lr_xml2mail

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

. $etcdir/profile_lean
. $etcdir/defaults

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

tag="${LR_SUPERSERVICE:-all} ${LR_SERVICE:-all} ${LR_ID:-UNSET} $PROGRAM"

echo >&2 "$tag 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)
            # needed by lr_xml2report
            LR_SERVICE="$OPTARG"
            ;;
        x)
            XMLS="$XMLS $OPTARG"
            ;;
        h | "?")
            echo >&2 "$tag notice $USAGE"
            exit 0
            ;;
	v)
	    echo >&2 "$tag notice $PROGRAM" '$Id: lr_xml2mail.in,v 1.14 2002/08/16 22:02:55 vanbaal Exp $,' "shipped with Lire 1.1"
	    exit 0
	    ;;
        *)
            echo >&2 "$tag err $USAGE"
            exit 1
            ;;
    esac
done


# reset tag, now that we might know LR_SUPERSERVICE
tag="${LR_SUPERSERVICE:-all} ${LR_SERVICE:-all} ${LR_ID:-UNSET} $PROGRAM"
# export them: lr_xml2report need them
export LR_SUPERSERVICE
export LR_SERVICE

# 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
            echo >&2 "$tag err $USAGE"
    exit 1
fi

echo >&2 "$tag info will send to $*"


if test -z "$OUTPUTFORMAT"
then
    OUTPUTFORMAT="$LR_DEFAULT_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"
    	;;
    rtf)
	type="application/x-zip"
	ext="zip"
	;;
    txt)
	type="text/plain"
	ext="txt"
    	;;
    pdf)
	type="application/pdf"
	ext="pdf"
    	;;
    xml)
	type="application/x-lire-report"
	ext="xml"
	;;
    logml)
	type="text/xml"
	ext="xml"
    	;;
    excel95)
	type="application/vnd.ms-excel"
	ext="xls"
    	;;
esac

tmpdir="$TMPDIR/$PROGRAM.$$"
echo >&2 "$tag debug creating temporary report dir $tmpdir/report"
if mkdir -p $tmpdir/report
then
    :
else
    echo >&2 "$tag 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
echo >&2 "$tag info running lr_xml2report $image_flag $output_flag $XMLS"
if lr_xml2report $image_flag $output_flag $XMLS > $reportfile
then
    echo >&2 "$tag info lr_xml2report completed successfully"
else
    echo >&2 "$tag err lr_xml2report failed"
    echo >&2 "$tag notice keeping tmpfiles in $tmpdir"
    exit 1
fi

extrafile="$tmpdir/signature.txt"
if touch $extrafile
then
    :
else
    echo >&2 "$tag err can't create $extrafile"
    echo >&2 "$tag notice keeping tmpfiles in $tmpdir"
    exit 1
fi

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

EOF
	;;
    esac
fi

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

if test -f "$LR_DISCLAIMERFILE"
then
    cat "$LR_DISCLAIMERFILE" >> $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
echo >&2 "$tag info running lr_mail -s \"$subject\" $type $reportfile $*"
if lr_mail -s "$subject" $attachflag $maintype $mainfile "$@"
then
    echo >&2 "$tag info lr_mail suceeded"
else
    echo >&2 "$tag err lr_mail failed"
    echo >&2 "$tag notice keeping tmpfiles in $tmpdir"
    exit 1
fi

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

echo >&2 "$tag info stopped"


