#!/bin/sh
# compile a TasteCurry program and generate the definitional trees
# for the Java backend

# the values of the following environment variables are required:
# JAVA, PIZZACLASSES

# define environment variable PAKCSHOME if not already defined:
if [ -z "$PAKCSHOME" ] ; then
  PAKCSHOME=`(cd \`dirname $0\` > /dev/null ; pwd)`/..
fi

# main directory with the TasteCurry->Java translator
TCJAVADIR=$PAKCSHOME/tc2java

QUIET=
if [ "xx$1" = "xx-q" ] ; then
  QUIET="-q"
  shift
elif [ "xx$1" = "xx-quiet" ] ; then
  QUIET="-q"
  shift
fi

if [ xx$PIZZACLASSES = xx ] ; then
  echo "ERROR: PIZZACLASSES missing, can't run curry2java compiler!" >&2
  exit 1
fi

if [ ! -f $TCJAVADIR/classes/curry4/compiler/Main.class ] ; then
  echo "ERROR: curry2java compiler is not installed!" >&2
  exit 1
fi

if [ $# != 1 ] ; then
  echo "Usage: $0 [-q|-quiet] <Curry program (without suffix)>" >&2
  exit 1
fi

# translate Curry program into TasteCurry program:
$PAKCSHOME/bin/parsecurry -def $QUIET "$@"
if test ! -f $1.def ; then
  # an error has occurred during parsing:
  exit 1
fi

# compile tree file into Java and execute it:
CLASSDIR=$1.classes
if [ ! -d $CLASSDIR ] ; then
  mkdir $CLASSDIR
fi
#CURRYCLASSES=$CLASSDIR:$TCJAVADIR/tc2java.zip:$PIZZACLASSES
CURRYCLASSES=$CLASSDIR:$TCJAVADIR/classes:$PIZZACLASSES
if [ -z ${CLASSPATH:=""} ] ; then
  CLASSPATH=$CURRYCLASSES
else
  CLASSPATH=$CURRYCLASSES:${CLASSPATH}
fi
export CLASSPATH
exec $JAVA -DTASTECURRY_PATH=$PAKCSHOME -ms32m -mx32m curry4.runtime.Run $QUIET "$@" $CLASSDIR
#exec $JAVA -ms32m -mx32m curry4.runtime.Run $1 $CLASSDIR
