#! /bin/sh -e

# $Id: lr_xslt.in,v 1.15 2002/08/04 19:27:51 flacoste 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_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/local"
datadir="${prefix}/share/lire"
exec_prefix="${prefix}"
libexecdir="${exec_prefix}/libexec/lire"
etcdir="${prefix}/etc/lire"

lr_catalog=
if test -f "$cfgfile"
then
    # running lr_xslt during make in source tree
    . "$cfgfile"
    cfgdir=`dirname $cfgfile`
    lr_catalog=`cd $cfgdir && pwd`"/../lib/xml/dtd/catalog.xml"
else
    . $etcdir/defaults
    lr_catalog="$datadir/xml/dtd/catalog.xml"
fi

if test "$LR_XSLT_PROCESSOR" != "xsltproc"
then
    if xsltproc --version > /dev/null 2> /dev/null
    then
	echo >&2 "$tag warning LR_XSLT_PROCESSOR is set to $LR_XSLT_PROCESSOR but only xsltproc is now supported, since it is available i'll swith to it now"
	LR_XSLT_PROCESSOR="xsltproc"
	if test -z "$LR_XSLTPROC"
	then
	    LR_XSLTPROC="xsltproc"
	fi
    else
	echo >&2 "$tag err $LR_XSLT_PROCESSOR isn't a supported xslt processor anymore. Please install xsltproc and update the LR_XSLT_PROCESSOR and LR_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 "$LR_XSLT_PROCESSOR" in
    xsltproc)
	if  test ! -x "$LR_XSLTPROC"
	then
	    echo >&2 "$tag err program $LR_XSLTPROC is not executable!"
	    exit 1
	fi

	# Create catalog.xml dynamically
	catalog=$TMPDIR/$PROGRAM-${LR_ID:-UNSET}-$$-catalog.xml
	echo >&2 "$tag info creating XML catalog $catalog"
	cat > $catalog <<EOF
<?xml version="1.0"?>
<!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">

<!-- The catalog for our DTDs -->
  <delegatePublic publicIdStartString="-//LogReport.ORG/" catalog="$lr_catalog"/>
  <delegateSystem systemIdStartString="http://www.logreport.org/" catalog="$lr_catalog"/>

EOF
	if test -n "$LR_DBK_XML_DTD"
	then
	cat >> $catalog <<EOF
  <public publicId="-//OASIS//DTD DocBook XML V4.1.2//EN" uri="$LR_DBK_XML_DTD"/>
  <system systemId="http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" uri="$LR_DBK_XML_DTD"/>
EOF
	fi
	cat >> $catalog <<EOF
</catalog>

EOF
	XML_CATALOG_FILES="$catalog"
	export XML_CATALOG_FILES

	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 LR_XSLTPROC's stderr
        exec 3>&1
	$LR_XSLTPROC "$@" 2>&1 1>&3 3>&- | while read s; do echo >&2 "$tag info $LR_XSLTPROC says $s"; done 1>&2 3>&-

	rm -f "$catalog"
	;;
    *)
	echo >&2 "$tag err no XSLT processor available"
	exit 1
	;;
esac

echo >&2 "$tag info stopped"

