#!/bin/sh
#
# Copyright (C) 2002 Daniel Veillard.  All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is fur-
# nished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
# NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# DANIEL VEILLARD BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
# NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the name of Daniel Veillard shall not
# be used in advertising or otherwise to promote the sale, use or other deal-
# ings in this Software without prior written authorization from him.
#
# Copyright (C) 2002 Eric Baudais.  All Rights Reserved.
#
# Modifications made to Daniel Veillard's catalog build script at 
# http://xmlsoft.org/buildDocBookCatalog.

if [ ! -d /etc/xml -a -w /etc ]
then
    echo Directory /etc/xml missing creating it
    mkdir /etc/xml
    chmod 755 /etc/xml
fi
if [ -w /etc/xml ]
then
    ROOTCATALOG=/etc/xml/catalog
    CATALOG=/etc/xml/docbook
else
    echo Unable to update root catalog /etc/xml/catalog
    echo Using $HOME/xmlcatalog as the root catalog
    echo export XMLCATALOG=$HOME/xmlcatalog
    ROOTCATALOG=$HOME/xmlcatalog
    CATALOG=$HOME/dbkxmlcatalog
fi

#
# create the catalogs root and docbook specific
#
if [ ! -r $ROOTCATALOG ] ; then
   echo creating XML Catalog root $ROOTCATALOG
   /usr/bin/xmlcatalog --noout --create $ROOTCATALOG
fi
if [ ! -r $ROOTCATALOG ] ; then
   echo Failed creating XML Catalog root $ROOTCATALOG
   exit 1
fi
if [ ! -r $CATALOG ] ; then
   echo creating DocBook XML Catalog $CATALOG
   /usr/bin/xmlcatalog --noout --create $CATALOG
fi
if [ ! -r $CATALOG ] ; then
   echo Failed creating DocBook XML Catalog $CATALOG
   exit 1
fi

#
# find the prefix for XSLT stylesheets
#
found=`find $1 -name chunk.xsl`
if [ "$found" = "" ] ; then
    found =`find /usr/share/xml -name chunk.xsl`
fi
if [ "$found" = "" ] ; then
    found=`find /usr/share/sgml -name chunk.xsl`
fi
if [ "$found" = "" ] ; then
    found=`find /usr/local -name chunk.xsl`
fi
if [ "$found" = "" ] ; then
    found=`find $HOME -name chunk.xsl`
fi
if [ "$found" = "" ] ; then
    echo could not locate chunk-common.xsl of DocBook XSLT stylesheets
    exit 1
fi
echo Found stylesheets $found
xsldir=""
for tst in $found; do
    dir=`dirname $tst`
    dir=`dirname $dir`
    echo First matching stylesheets $dir
    if [ -r $dir/html/docbook.xsl -a -r $dir/common/l10n.xml ]; then
        xsldir=$dir
        echo Matching stylesheets $xsldir
        break
    fi
done

if [ "$xsldir" = "" ] ; then
    echo could not locate DocBook XSLT stylesheets
    exit 1
fi
echo Found DocBook XSLT stylesheets in $xsldir
for version in current $2
do
    xmlcatalog --noout --add "rewriteSystem" \
	"http://docbook.sourceforge.net/release/xsl/$version" \
	"file://$xsldir" $CATALOG
    xmlcatalog --noout --add "rewriteURI" \
	"http://docbook.sourceforge.net/release/xsl/$version" \
	"file://$xsldir" $CATALOG
done

xmlcatalog --noout --add "delegateSystem" \
    "http://docbook.sourceforge.net/release/xsl/" \
    "file://$CATALOG" $ROOTCATALOG
xmlcatalog --noout --add "delegateURI" \
    "http://docbook.sourceforge.net/release/xsl/" \
    "file://$CATALOG" $ROOTCATALOG

#
#
