#!/bin/bash

source "`dirname "$0"`/set_cp.sh"
if [ -x /usr/bin/cygpath ]; then
	CLASSPATH=`cygpath -wp "$CLASSPATH"`
	LMNTAL_HOME=`cygpath -wp "$LMNTAL_HOME"`
fi

slimoptimizer=false
slimcode=false
lmnfile=initial_lmntal_file_name
lmntal_syntax_il=${lmnfile}_lmntal_syntax_il.lmn
for arg in $*
do
    if [ $arg = --optimize-slimoptimizer ]; then
	slimoptimizer=true
    fi
    if [ $arg = --slimcode ]; then
	slimcode=true;
    fi
    if [ ${arg%%.lmn} != $arg ];then
	lmnfile=${arg%%.lmn}
	lmntal_syntax_il=${lmnfile}_lmntal_syntax_il.lmn
    fi
done



optlibpath=$LMNTAL_HOME/lib/slimoptimizer
if [ $slimoptimizer = true -a $slimcode = true ]; then
    
    java -DLMNTAL_HOME="$LMNTAL_HOME" runtime.FrontEnd --slimcodelmnsyntax --interpret -O3 "$@" > ${lmntal_syntax_il}
   
    java -DLMNTAL_HOME="$LMNTAL_HOME" runtime.FrontEnd --interpret --slimcode -O3 \
    ${lmntal_syntax_il} ${optlibpath}/optimizeMain.lmn ${optlibpath}/reuse.lmn ${optlibpath}/merge.lmn ${optlibpath}/loop.lmn ${optlibpath}/outputIL.lmn  |  slim  -

    rm ${lmntal_syntax_il}

else
    java -DLMNTAL_HOME="$LMNTAL_HOME" runtime.FrontEnd --interpret "$@"  
fi