#! /bin/sh -e

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

#
# Copyright (C) 2000-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_xml2report

# 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} $PROGRAM"

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

USAGE="Usage: $PROGRAM [-t] [-i] [-c <report_cfg>] [-o <outputformat>] [-U <superservice>] [-S <service>] report.xml [report.xml ...]"
# get options (x)
OUTPUTFORMAT=""
dry_run=""
include_image=""
while getopts :o:c:U:S:ivth\? o; do
    case $o in
        o)  OUTPUTFORMAT="$OPTARG"
            ;;
        c)  REPORT_CFG="$OPTARG"
            ;;
        U)
            # needed by lr_xml_merge
            LR_SUPERSERVICE="$OPTARG"
            ;;
        S)
            # needed by lr_archive_log
            LR_SERVICE="$OPTARG"
            ;;
	i)
	    include_image=1
	    ;;
	t)
	    dry_run=1
	    ;;
        h | "?")
            echo >&2 "$tag notice $USAGE"
            exit 0
            ;;
	v)
	    echo >&2 "$tag notice $PROGRAM" '$Id: lr_xml2report.in,v 1.10 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} $PROGRAM"

# export it: lr_archive_log needs it
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

if test $# -gt 1
then
    XML_REPORT_FILES="$@"
    if test -z "$LR_SUPERSERVICE"
    then
        echo >&2 "$tag err multiple xmlreports specified, but LR_SUPERSERVICE unset"
        exit 1
    fi
    if test -z "$LR_SERVICE"
    then
        echo >&2 "$tag err multiple xmlreports specified, but LR_SERVICE unset"
        exit 1
    fi
else
    XMLREPORTFILE="$1"
fi


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

# LR_INCLUDEIMAGES only turn on images for supported format
if test -n "$LR_INCLUDEIMAGES"
then
    case $OUTPUTFORMAT in
    pdf|html|html_page|docbookx|xhtml|xhtml_page|rtf)
	image_flag="-i"
	;;
    *)
	;;
    esac
else
    if test -n "$include_image"
    then
	image_flag="-i"
    else
	image_flag=""
    fi
fi

# do a test for support before possibly unnecessary log file analysis
case $OUTPUTFORMAT in
    xml|txt|pdf|html|html_page|docbookx|xhtml|xhtml_page|rtf|excel95)
        ;;
    logml)
        case $LR_SUPERSERVICE in
        www|email)
	    ;;
        *)
	    echo >&2 "$tag err format logml can only be used for the www and email superservice"
	    exit 1
        esac
        ;;
    *)
        echo >&2 "$tag err format $OUTPUTFORMAT is not supported"
        exit 1
        ;;
esac
lr_check_prereq $image_flag $OUTPUTFORMAT || exit 1

if test -n "$dry_run"
then
    exit 0
fi


if test -n "$REPORT_CFG"
then
    :
elif test -r $HOME/.lire/etc/$LR_SUPERSERVICE.cfg
then
    REPORT_CFG=$HOME/.lire/etc/$LR_SUPERSERVICE.cfg
else
    REPORT_CFG=$etcdir/$LR_SUPERSERVICE.cfg
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 test -n "$XMLREPORTFILE"
then
    :
else 
    XMLREPORTFILE=$TMPDIR/$PROGRAM.$LR_SUPERSERVICE.$LR_ID.xml
    echo >&2 "$tag info gonna run lr_xml_merge $LR_SUPERSERVICE $REPORT_CFG $XML_REPORT_FILES > $XMLREPORTFILE"
    if lr_xml_merge $LR_SUPERSERVICE $REPORT_CFG $XML_REPORT_FILES > $XMLREPORTFILE
    then
        :
    else
        echo >&2 "$tag err lr_xml_merge failed"
        echo >&2 "$tag notice keeping $XMLREPORTFILE for debugging"
        exit 1
    fi
    echo >&2 "$tag info lr_xml_merge finished"
fi


echo >&2 "$tag info converting $XMLREPORTFILE to $OUTPUTFORMAT"

case $OUTPUTFORMAT in
    xml) 
        echo >&2 "$tag info generating raw XML output, not doing lr_xml2ascii"
        cat $XMLREPORTFILE
        ;;
    txt)
        OK=
        echo >&2 "$tag info gonna run lr_xml2ascii on $XMLREPORTFILE"
        lr_xml2ascii < $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            echo >&2 "$tag info succeeded in generating ascii"
        else
            echo >&2 "$tag err lr_xml2ascii failed"
            exit 1
        fi
        echo >&2 "$tag info lr_xml2ascii finished"
        ;;
    html|html_page)
	one_page=
	if test $OUTPUTFORMAT = "html_page"
	then
	    one_page="-1"
	fi
        OK=
	lr_xml2html $image_flag $one_page $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            echo >&2 "$tag info succeeded in generating HTML"
        else
            echo >&2 "$tag err lr_xml2html failed"
            exit 1
        fi
        echo >&2 "$tag info lr_xml2html finished"
        ;;
    rtf)
        OK=
	lr_xml2rtf $image_flag $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            echo >&2 "$tag info succeeded in generating RTF"
        else
            echo >&2 "$tag err lr_xml2rtf failed"
            exit 1
        fi
        echo >&2 "$tag info lr_xml2rtf finished"
        ;;
    pdf)
        OK=
        lr_xml2pdf $image_flag $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            echo >&2 "$tag info succeeded in generating PDF"
        else
            echo >&2 "$tag err lr_xml2pdf failed"
            exit 1
        fi
        echo >&2 "$tag info lr_xml2pdf finished"
        ;;
    docbookx)
        OK=
        lr_xml2dbx $image_flag $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            echo >&2 "$tag info succeeded in generating DocBook XML"
        else
            echo >&2 "$tag err lr_xml2dbx failed"
            exit 1
        fi
        echo >&2 "$tag info lr_xml2dbx finished"
        ;;
    logml)
        OK=
        lr_xml2logml $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            echo >&2 "$tag info succeeded in generating LogML"
        else
            echo >&2 "$tag err lr_xml2logml failed"
            exit 1
        fi
        echo >&2 "$tag info lr_xml2logml finished"
        ;;
    xhtml|xhtml_page)
	one_page=
	if test $OUTPUTFORMAT = "xhtml_page"
	then
	    one_page="-1"
	fi
        OK=
        lr_xml2xhtml $image_flag $one_page $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            echo >&2 "$tag info succeeded in generating XHTML"
        else
            echo >&2 "$tag err lr_xml2xhtml failed"
            exit 1
        fi
        echo >&2 "$tag info lr_xml2xhtml finished"
        ;;
    excel95)
        OK=
	lr_xml2xls < $XMLREPORTFILE && OK=1

        if test -n "$OK"
        then
            echo >&2 "$tag info succeeded in generating Excel95 spreadsheet"
        else
            echo >&2 "$tag err lr_xml2xls failed"
            exit 1
        fi
        echo >&2 "$tag info lr_xml2xls finished"
        ;;
    *)
        echo >&2 "$tag err format $OUTPUTFORMAT is not supported"
        exit 1
        ;;
esac

#
# lr_xml_merge has set a time_span in the Lire db.
#
if test -n "$XML_REPORT_FILES"
then
    # we are running in multiple input files mode: XMLREPORTFILE is
    # created by us
    echo >&2 "$tag info running lr_archive_log -x $XMLREPORTFILE"
    if lr_archive_log -x $XMLREPORTFILE
    then
        echo >&2 "$tag info lr_archive_log succeeded"
    else
        echo >&2 "$tag info lr_archive_log failed"
        exit 1
    fi
fi


echo >&2 "$tag info stopped"


