#!/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: post-po,v 1.9 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

date=`date +%d.%m.%Y`
DEST=$2
lg=`basename $2`
exec 1>$0.$lg.$date.log  2>&1
export PATH=$PATH:./compiler/c
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`
#DIRNAME=$(basename `dirname $1`);
#echo "- making directory $DESTDIR/$DIRNAME";
#mkdir -p $DESTDIR
#DESTDIR=$2

if [ "${XSL_COMPILER}" = "xsltproc" ]; then
	${XSL_COMPILER} --param wpofile "'$DEST/$BASENAME.npo'" $XSL_DIR/$STYLESHEET $1 > $DESTDIR/$BASENAME.naat
else
	# compile module.naat
	# 
	${XSL_COMPILER}  -IN $1 -XSL $XSL_DIR/$STYLESHEET -PARAM wpofile "'$DEST/$BASENAME.npo'" -OUT $DESTDIR/$BASENAME.naat
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=translator.xsl;
fi

[ -d $TEMP ] || mkdir -p $TEMP
LG=`basename $DEST`
DESTDIR="`basename $1`-$LG"
mkdir -p $DESTDIR

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
			echo Listing files...
 			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
# translate index
if [ "${XSL_COMPILER}" = "xsltproc" ]; then
${XSL_COMPILER} --param wpofile "'$DEST/index.frontend.xml.npo'" $XSL_DIR/index-$STYLESHEET $1/index.frontend.xml > $DESTDIR/index.frontend.xml
else
${XSL_COMPILER} -IN $1/index.frontend.xml -XSL $XSL_DIR/index-$STYLESHEET -PARAM wpofile "'$DEST/index.frontend.xml.npo'" -OUT $DESTDIR/index.frontend.xml
fi

fi
exit $EXIT
# End of script
