#! /bin/sh -e

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"

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--stringparam$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"

exit 0

POD=<<'EOPOD'

=pod

=head1 NAME

lr_xslt - common interface to various XSLT processors

=head1 SYNOPSIS

B<lr_xslt> I<xsl_file> I<xml_file> B<[assigments*]>

=head1 DESCRIPTION

B<lr_xslt> offers a common interface to various XSLT processors. It
will process the XML file I<xml_file> using the XSL stylesheet
B<xsl_file>.

The XSLT processor run is selected by the value of the I<LR_XSLT_PROCESSOR>
variable. Supported values are 'none' or 'xsltproc'.

The result of the stylesheet is output on STDOUT.  When the output
is chunked (e.g. multiple HTML pages), files in the current working
directory are created.  No stuff on STDOUT is created in this case.

Top-level variables can be set in the stylesheets by adding
assigments. They are of the form I<name>=I<value>. For example :
number_to_show=10. When passing string value, you have to include
quotes around the value like in 'name="Francis"'. (The single quotes
are to prevent the shell from removing the quotes around the value).

=head1 BUGS

Currently, only xsltproc(1) is supported. We should support more XSLT
processors.

=head1 SEE ALSO

xsltproc(1)

=head1 VERSION

$Id: lr_xslt.in,v 1.18 2003/10/20 16:53:46 flacoste Exp $

=head1 COPYRIGHT

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


