#!/bin/sh
#
# Copyright (C) 2002 MandrakeSoft SA
# 
# This program may be distributed and/or
# modified under the terms of the GNU General Public
# License version 2 as published by the Free Software Foundation
# 
# 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.
# 
# Licensees holding valid MandrakeSecurity MNF Commercial License may use this
# file in  accordance with the MandrakeSecurity MNF Commercial License Agreement
# provided with the Software.
# 
# See http://www.mandrakesoft.com/mnf/license for information about MandrakeSecurity MNF Commercial License Agrrement.
# 
# $Id: index_builder,v 1.8 2002/12/03 14:40:17 philippe Exp $
# Generate an index out of a given set of .naat
# - The first argument is the directory containing the .naat file to process
#exec 1>index.log  2>&1
date=`date +%d.%m.%Y`
DEST=$1
long=`basename $1`
lg=`echo $long |sed -e 's!.*-!!; s!/!!'`

exec 1> index.${lg:-en}.$date.log  2>&1

TEMP="tmp"
#XSL_COMPILER=xsl-c
XSL_COMPILER=xsltproc
XSL_COMPILER_OPTIONS=--timing
[ -d $TEMP ] || mkdir -p $TEMP

if [ -z $1 ]; then
echo "usage: index_builder <naat directory> <result file> <OPTIONS>";
exit 1;
fi

DESTDIR=""
OPT=
if [ "${XSL_COMPILER}" = "xsltproc" ]; then
[ -z $2 ] || DESTDIR="$2";
[ -z $3 ] || OPT="--param $3 $4";
else
[ -z $2 ] || DESTDIR="-OUT $2";
[ -z $3 ] || OPT="-PARAM $3 $4";
fi

# 1. create an index.xml containing only the "Wizard" tags of all the wizards in the directory. The file has the form 
#    <Index>
#      <Wizard name="name1" group="group1"/>
#      <Wizard name="name2" group="group2"/>
#      ... ...
#    </Index>
#
#    The extract.xsl stylesheet is used to select only the Wizard tags
 
#cat <<EOF > $TEMP/index.xml
#<?xml version="1.0"?>
#<Index>
#EOF

#for i in `find $1 -name "*.naat" |sort` 
#do
#	${XSL_COMPILER} -IN $i -XSL xsl/extract.xsl $OPT
#done >> $TEMP/index.xml

#echo "</Index>" >> $TEMP/index.xml

# 2. Apply  the groups.xsl stylesheet to transform index.xml in a file of this form:
#    <Index>
#       <Group>group1
#          <Group name="localisation1">name 1</Group>
#       </Group>
#       <Group>group2
#          <Group name="localisation3">name 3</Group>
#    </Index>
     
#${XSL_COMPILER} -IN $TEMP/index.xml -XSL xsl/group.xsl  $OPT > $TEMP/temporary

# 3. Apply hierarchy.xsl to obtain the final file where
#    tags appartaining to the same group are under the same tag.
#${XSL_COMPILER} -IN $TEMP/temporary  -XSL xsl/hierarchy.xsl $DESTDIR $OPT

# . New stuff for table summary
# uncomment all previous XSL_compiler stuff
#${XSL_COMPILER} -IN $1/index.frontend.xml  -XSL xsl/index-group.xsl $DESTDIR $OPT
if [ "${XSL_COMPILER}" = "xsltproc" ]; then
#echo ${XSL_COMPILER}  $OPT xsl/index.xsl $1/index.frontend.xml '>' $DESTDIR;
${XSL_COMPILER} ${XSL_COMPILER_OPTIONS} $OPT xsl/index.xsl $1/index.frontend.xml > $DESTDIR;
else
#echo ${XSL_COMPILER} -IN $1/index.frontend.xml  -XSL xsl/index.xsl $DESTDIR $OPT;
${XSL_COMPILER} -IN $1/index.frontend.xml  -XSL xsl/index.xsl $DESTDIR $OPT;
fi

# End script
