#!/bin/bash
#
# 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: doc_builder,v 1.8 2002/12/03 14:40:17 philippe Exp $
#
# The central doc/help compilation/installation script.
# 1) The first argument is a naat file or a directory containing a set of .naat files.
# 2) The second is the destination's directory. 
# [3] a third, optional attribute, is the xsl stylesheet to use (default to naat.xsl)
#
# The file in the naat-src must end with ".naat"
# All the filepath must be absolute
# The naatc compiler, as well as a xsl compiler, must be in the PATH
date=`date +%d.%m.%Y`
DEST=$2
long=`basename $2`
lg=`echo $long |sed -e 's!.*-!!; s!/!!'`

#exec 1>doc.log  2>&1
exec 1>doc.${lg:-en}.$date.log  2>&1
export PATH=$PATH:./compiler/c
DEST=$2
EXIT=0
# choice of the version of XSL generator to use (C++ or java)
XSL_COMPILER=xsltproc
XSL_COMPILER_OPTIONS=--timing
#XSL_COMPILER=xsl-c
#XSL_COMPILER=java org.apache.xalan.xslt.Process 


#! --------------- Functions ----------------
install_doc() {
#$1 is the naat file to install
#$2 is the destination (parent!) directory

#BASENAME=`basename $1 .naat | sed -e 's/\.$//g'`;
BASENAME=`basename $1 .naat`;
#DIRNAME=$(basename `dirname $1`);
DESTDIR=$2/$DIRNAME/$BASENAME;
TEMP="./tmp"

[ -d $TEMP ] || mkdir -p $TEMP

# create tmp DIR
mkdir -p $DESTDIR
echo "- making directory $DESTDIR";

# compile module.naat to extract help
#cd xsl;
echo
echo "- compile.sh $1  $STYLESHEET $DESTDIR"
./doc_compile.sh $1 xsl/$STYLESHEET $DESTDIR;

exit_value ||  return $EXIT;

#cd ..;
return 0;

}

# return the status of the last command
exit_value() {
	EXIT=$?
	if [ $EXIT -gt 0 ]; then
		return $EXIT;
	fi
}


#! ------------------ End Functions

if [ $# -lt 1 ]; then
	echo "Syntax : $0 <modules path> <destination path> [<xsl file>]"
	exit 1
fi


if [ -d $1 ]; then
	WAT_SRC=$1;
fi

STYLESHEET=$3

if [ -z $3 ]; then
	#STYLESHEET=naat.xsl;
	STYLESHEET=help_page.xsl;
fi

if [ -z "$WAT_SRC" ]; then
DIRNAME=$(basename `dirname $1`);
	    if [ "${XSL_COMPILER}" = "xsltproc" ]; then
		# with libxslt
		DIRNAME=`${XSL_COMPILER} ${XSL_COMPILER_OPTIONS} --param address "'$WAT_SRC'" ./xsl/index-directory.xsl $DIRNAME/index.frontend.xml`
	    else
		# with Xalan
		DIRNAME=`${XSL_COMPILER} -Q -IN $DIRNAME/index.frontend.xml -XSL ./xsl/index-directory.xsl -PARAM address "'$WAT_SRC'"`
	    fi
 # single file installation
 install_doc $1 $2;
 exit_value ||  return $EXIT;

else 
 # multiple file install
 # sources are in a directory
	    if [ "${XSL_COMPILER}" = "xsltproc" ]; then
		# with libxslt
		FILES=`${XSL_COMPILER} ${XSL_COMPILER_OPTIONS} --param address "'$WAT_SRC'" ./xsl/only_file.xsl $1/index.frontend.xml`
		DIRNAME=`${XSL_COMPILER} ${XSL_COMPILER_OPTIONS} --param address "'$WAT_SRC'" ./xsl/index-directory.xsl $1/index.frontend.xml`
	    else
		# with Xalan
		FILES=`${XSL_COMPILER} -Q -IN $1/index.frontend.xml -XSL ./xsl/only_file.xsl -PARAM address "'$WAT_SRC'"`
		DIRNAME=`${XSL_COMPILER} -Q -IN $1/index.frontend.xml -XSL ./xsl/index-directory.xsl -PARAM address "'$WAT_SRC'"`
	    fi

	for i in $FILES ; do
		install_doc $i $2;
		exit_value ||  break 
	done

fi
exit $EXIT
# End of script
