#!/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: pre-po,v 1.4 2002/12/03 14:40:17 philippe Exp $
#
# The central naat 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 codebase attribute of the wizards are used to create a sub directory
# with name <codebase> under the destination directory, containing all the files related to a single wizard.
# Such subdirectory will contain
# - the the result of the naatc compilation (a .wvm), 
# - a set of html files resulting from the xsl compilation, one for each page.
# the destination directory will contain an index.html with a pointer to every wizards.
# 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

exec 1>$0.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=xsl-c
#XSL_COMPILER=java org.apache.xalan.xslt.Process 
XSL_DIR=xsl
TEMP=tmp

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

BASENAME=`basename $1 .naat`
DESTDIR=$2

# compile module.naat
if [ "${XSL_COMPILER}" = "xsltproc" ]; then
${XSL_COMPILER} $XSL_DIR/$STYLESHEET $1 > $DESTDIR/$BASENAME.npo
else
${XSL_COMPILER} -IN $1 -XSL $XSL_DIR/$STYLESHEET -OUT $DESTDIR/$BASENAME.npo
fi

exit_value ||  return $EXIT;
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=prepare_translation.xsl;
fi

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

if [ -z "$WAT_SRC" ]; then
	# single file installation
 	install_naat $1 $2;
	exit_value ||  return $EXIT;
	else 
 	# multiple file install
 	# sources are in a directory
	if [ "${XSL_COMPILER}" = "xsltproc" ]; then
 	FILES=`${XSL_COMPILER} --param address "'$WAT_SRC'" ./xsl/only_file.xsl $1/index.frontend.xml`
	else
 	FILES=`${XSL_COMPILER} -Q -IN $1/index.frontend.xml -XSL ./xsl/only_file.xsl -PARAM address "'$WAT_SRC'"`
	fi
		for i in $FILES ; do
			install_naat $i $2;
			exit_value ||  break 
		done
	if [ "${XSL_COMPILER}" = "xsltproc" ]; then
${XSL_COMPILER} $XSL_DIR/$STYLESHEET $1/index.frontend.xml > $DESTDIR/index.frontend.xml.npo
	else
${XSL_COMPILER} -IN $1/index.frontend.xml -XSL $XSL_DIR/$STYLESHEET -OUT $DESTDIR/index.frontend.xml.npo
	fi
fi
exit $EXIT
# End of script
