#! /bin/sh -e

# $Id: lr_xslt.in,v 1.12 2001/11/19 16:08:28 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_xslt

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

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

cfgfile=
while getopts :c: a
do
    case $a in
    c)
        cfgfile=$OPTARG
        ;;
    *) 
        echo >&2 "$tag err usage $USAGE"
        exit 1
        ;; 
    esac
done
shift `expr $OPTIND - 1 || true`

if test $# -lt 2
then
  echo >&2 "$tag err expecting argument. syntax: $PROGRAM file.xsl report.xml [param=value]..."
  exit 1
fi

xsl_file="$1"
xml_file="$2"
shift && shift

if test ! -r "$xml_file"
then
  echo >&2 "$tag err file '$xml_file' is not readable."
  exit 1
fi

if test ! -r "$xsl_file"
then
  echo >&2 "$tag err file '$xsl_file' is not readable."
  exit 1
fi

# get global vars
prefix="/usr"
datadir="${prefix}/share/lire"
exec_prefix="${prefix}"
libexecdir="/usr/lib/lire"
etcdir="/etc/lire"

if test -f "$cfgfile"
then
    # Try to setup XML_CATALOG_FILES based on the location of
    # the cfgfile
    LR_CATALOG_FILE="`dirname $cfgfile`/catalog.xml"
    if test -f "$LR_CATALOG_FILE"
    then
	if test -z "$XML_CATALOG_FILES"
	then
	    XML_CATALOG_FILES="$LR_CATALOG_FILE"
#	else
#	    XML_CATALOG_FILES="$LR_CATALOG_FILE:$XML_CATALOG_FILES"
	fi
	export XML_CATALOG_FILES
    fi
    # running lr_xslt during make in source tree
    . "$cfgfile"
else
    . $etcdir/defaults
fi

if test "$XSLT_PROCESSOR" != "xsltproc"
then
    if xsltproc --version > /dev/null 2> /dev/null
    then
	echo >&2 "$tag warning XSLT_PROCESSOR is set to $XSLT_PROCESSOR but only xsltproc is now supported, since it is available i'll swith to it now"
	XSLT_PROCESSOR="xsltproc"
	if test -z "$XSLTPROC"
	then
	    XSLTPROC="xsltproc"
	fi
    else
	echo >&2 "$tag err $XSLT_PROCESSOR isn't a supported xslt processor anymore. Please install xsltproc and update the XSLT_PROCESSOR and XSLTPROC variables accordingly"
	exit 1
    fi
fi

tab="	" # There is a tab in there
params=""
args=""

# We use IFS magic and set -- to handle case where there are 
# embedded spaces in the parameter's value.
case "$XSLT_PROCESSOR" in
    xsltproc)
	if  test ! -x "$XSLTPROC"
	then
	    echo >&2 "$tag err program $XSLTPROC is not executable!"
	    exit 1
	fi

	while test $# -gt 0
	do
	    name="`echo $1  | cut -f 1 -d =`"
	    value="`echo $1 | cut -f 2- -d =`"
	    if test -z "$name" -o -z "$value"
	    then
		echo >&2 "$tag err can't parse $1 parameter"
		exit 1
	    fi
	    
	    params="$params$tab--param$tab$name$tab$value"
	    shift
	done

	args="--nonet$tab$params$tab$xsl_file$tab$xml_file"
	OLDIFS="$IFS"
	IFS="$tab"
	set -- $args
	IFS="$OLDIFS"

	echo >&2 "$tag info running xsltproc with $@"
        # tweak XSLTPROC's stderr
        exec 3>&1
	$XSLTPROC "$@" 2>&1 1>&3 3>&- | while read s; do echo >&2 "$tag info $XSLTPROC says $s"; done 1>&2 3>&-
	;;
    *)
	echo >&2 "$tag err no XSLT processor available"
	exit 1
	;;
esac

echo >&2 "$tag info stopped"

