#!/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: naatbuilder,v 1.10 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
long=`basename $2`
lg=`echo $long |sed -e 's!.*-!!; s!/!!'`

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

#exec 1>naat.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_naat() {
#$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/$BASENAME;
DESTDIR=$2/$DIRNAME/$BASENAME
TEMP="./tmp"

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

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

# compile navigation table / module
echo
echo "- naatc -p $DIRNAME/$BASENAME/ $1 > $DESTDIR/$BASENAME.wvm"
naatc -p "$DIRNAME/$BASENAME/" $1 > $DESTDIR/$BASENAME.wvm;

# compile module.naat
#cd xsl;
echo
echo "- compile.sh $1  xsl/$STYLESHEET $DESTDIR"
./compile.sh $1 xsl/$STYLESHEET $DESTDIR ${lg:-en} ;

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;
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_naat $1 $2;
	exit_value ||  return $EXIT;

	
# 	cd xsl;
# 	${XSL_COMPILER} -IN  $1 -XSL ./index_maker.xsl -OUT $DEST/index.html -PARAM address "'engine.php'";

	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

	#  FILES=`find $WAT_SRC -name "*.naat" |sort`

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

 
	# rm -rf $TEMP;
	# make index , nav.inc.php
	# sh index_builder $1  "$DEST/nav.inc.php" address "'../engine.php'"
	# done by makefile
	
fi
exit $EXIT
# End of script
