#!/bin/env bash

if [ "$1" == "-j" ];then
     JARS=$2
     shift
     shift
fi


## $1 is the directory produced by NeverLangC; from $2 there are all the source file to be executed
## check presence of parameters
## parameters must be at least 2, the first parameter must be a directory
if [ $# -lt 2 ] ; then
	echo
	echo "Usage: nlg  [-j jars] <dir> <file>+ [-a args*]"
	echo
	echo "  <dir>"
	echo "      the directory created by NeverLangC containing"
	echo "      the interpreter to be executed."
	echo "  <file>"
	echo "      the file containing the source to be executed"
	echo "      by the interpreter."
	echo "  -a args"
	echo "	    The arguments that will be pass to the interpreter	"
	exit 1
fi

## get NeverLang programs directory
#NLDIR=`dirname $0`

## get AspectJ installation directory
AJCDIR=`which ajc 2>&- `
if [ "$AJCDIR" == "" ];then
	echo "ERROR: aspectJ compiler not present "
	exit 1
fi	

AJDIR=`dirname $(dirname $AJCDIR)`

NLGCDIR="`dirname $(which nlgc 2> /dev/null) 2>&- `"
if [ "$NLGCDIR" == "" ];then
	NLGCDIR=`dirname $0`
fi

LIBDIR="$NLGCDIR/../lib"

## get the execution directory
EXEDIR=$1
#now condider only the other args
shift

java -cp $AJDIR/lib/aspectjrt.jar:$LIBDIR/nlCommon.jar:$LIBDIR/xtc.jar:$EXEDIR:$JARS runtime.NeverLang $@  

