#!/bin/env bash

function echoUsage {
	echo "Usage: nlgc -d <destDir> [-j <jar packages>] [ -a <suppPackDir> ] [-k] <file>+"
	echo ""
	echo "  <file>"
	echo "   the file composing the grammar "
	echo ""
	echo " -d <destDir> " 
	echo "   Directory of where the interpreter will be created. The directory will be overwritten if it already exists "	
	echo "   "	
	echo " -a <suppPackDir> "	
	echo "   specify the directory containing the java sources that need to be compiled with the "
	echo "	 generated source code in order to create the interpreter "	
	echo ""	
	echo " -j <jar packages>"
	echo "   specify the needed java packages not contained in the classpath."
	echo ""
	echo " -k "
	echo "	 Keep the soure code generated, the cade will be intended if the astyle tool is present in the system"
	echo "   Multiple jar files are separated with ':'"
	echo ""
	echo ""
}

KEEP="0"
DESTDIR=""
PACKDIR=""

if [ $# -lt 3 ]; then
  echoUsage
  exit 1
fi

while [ ! -z `echo $1 | grep [-]` ]
do  
  if [ "$1" == "-d" ];then
    if [ -d $2 ];then
      DESTDIR="$2"      
      rm -rf "$DESTDIR" 2> /dev/null
      mkdir $DESTDIR
      if [ "$?" != "0" ];then
	echo "ERROR: is not possible create the destination dir"	
        exit 1
      fi
      shift
      shift
    else
      DESTDIR="$2"      
      mkdir $DESTDIR
      if [ "$?" != "0" ];then
	echo "ERROR: is not possible create the destination dir"	
        exit 1
      fi
      shift
      shift
    fi  
  else
    DESTDIR="./"  
  fi

  if [ "$1" == "-a" ];then
    if [ -d $2 ];then
      PACKDIR="$2"      
      shift
      shift
    else
      echo "The Directory "
      echo $2
      echo " doesn't exists or is not a directory"
      echoUsage
      exit 1
    fi    
  fi  
  if [ "$1" == "-k" ];then
     KEEP="1"
     shift
  fi   
  if [ "$1" == "-j" ];then
     JARS=$2
     shift
     shift
  fi

done

if [ -z "$DESTDIR" ];then
	echo "ERROR: The DESTDIR has not been specified"
	echoUsage
	exit 1
fi	




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

CLASSDIR="$NLGCDIR/../class/"
RESDIR="$NLGCDIR/../res/"
LIBDIR="$NLGCDIR/../lib"

#TODO errore se ajc non  stato trovato
## execute interpreter on input file
echo "Starting source generation ..."
#echo "-cp $CLASSDIR:$LIBDIR/xtc.jar:$LIBDIR/nlCommon.jar compiler.NeverlangC $RESDIR $DESTDIR $*"
java -cp $CLASSDIR:$LIBDIR/xtc.jar:$LIBDIR/nlCommon.jar compiler.NeverlangC $RESDIR $DESTDIR $*  
echo "Generation end!"

## get AspectJ installation directory
AJCDIR=`which ajc`
if [ "$AJCDIR" == "" ];then
	echo "ERROR: aspectJ compiler not present "
	exit 1
fi	
AJDIR=`dirname $(dirname $AJCDIR)`


if [ "$KEEP" == "1" ];then
	if [ ! -z "`which astyle 2> /dev/null`" ];then
		echo "Formatting the code with astyle "
		find $DESTDIR -name *.java  -exec astyle --mode=java --suffix=none --indent=tab {} >/dev/null \;  
		find $DESTDIR -name *.aj -exec astyle --mode=java --suffix=none --indent=tab {} >/dev/null \; 
	else
		echo "Warning astyle not found prenset.Couldn't indent the generated code " 
	fi	
fi

if [ ! -z "$PACKDIR" ];then
	echo "Adding Support pack from $PACKDIR to $DESTDIR "
	rsync -Cau $PACKDIR $DESTDIR
fi


echo "Compiling the sources... "
## if this is the first time this interpreter is executed
## .class files are not yet been created
if [ `find $DESTDIR -name *.class | wc -l` -eq 0 ] ; then
	## weave aspects with source code
#	echo "ajc  -Xmx512MB -Xms256MB -1.6 -cp $LIBDIR/nlCommon.jar:$LIBDIR/xtc.jar:$DESTDIR:$AJDIR/lib/aspectjrt.jar:$JARS `find $DESTDIR -name *.java` `find $DESTDIR -name *.aj`"
	ajc  -Xmx512MB -Xms256MB -1.6 -cp $LIBDIR/nlCommon.jar:$LIBDIR/xtc.jar:$DESTDIR:$AJDIR/lib/aspectjrt.jar:$JARS `find $DESTDIR -name *.java` `find $DESTDIR -name *.aj`
fi
echo "Compilation ended"


if [ "$KEEP" == "0" ];then
	echo "Removing the source files"
	find $DESTDIR -name *\.java -exec rm -f {} \;
	find $DESTDIR -name *\.aj -exec rm -f {} \;
	find $DESTDIR -name *\.rats -exec rm -f {} \;
fi


