#!/bin/sh

##############################################################################
# Scala test runner 2.8.0
##############################################################################
# (c) 2002-2010 LAMP/EPFL
#
# This is free software; see the distribution for copying conditions.
# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
##############################################################################

cygwin=false;
darwin=false;
case "`uname`" in
    CYGWIN*) cygwin=true ;;
    Darwin*) darwin=true ;;
esac

# Finding the root folder for this Scala distribution
SOURCE=$0;
SCRIPT=`basename "$SOURCE"`;
while [ -h "$SOURCE" ]; do
    SCRIPT=`basename "$SOURCE"`;
    LOOKUP=`ls -ld "$SOURCE"`;
    TARGET=`expr "$LOOKUP" : '.*-> \(.*\)$'`;
    if expr "${TARGET:-.}/" : '/.*/$' > /dev/null; then
        SOURCE=${TARGET:-.};
    else
        SOURCE=`dirname "$SOURCE"`/${TARGET:-.};
    fi;
done;

# see #2092
SCALA_HOME=`dirname "$SOURCE"`
SCALA_HOME=`cd "$SCALA_HOME"; pwd -P`
SCALA_HOME=`cd "$SCALA_HOME"/..; pwd`

if $cygwin; then
    SCALA_HOME=`cygpath --windows --short-name "$SCALA_HOME"`
    SCALA_HOME=`cygpath --unix "$SCALA_HOME"`
fi

# Constructing the extension classpath
EXT_CLASSPATH=""
if [ -z "$EXT_CLASSPATH" ] ; then
    if [ -f "$SCALA_HOME/lib/scala-partest.jar" ] ; then
        for ext in "$SCALA_HOME"/lib/* ; do
            if [ -z "$EXT_CLASSPATH" ] ; then
                EXT_CLASSPATH="$ext"
            else
                EXT_CLASSPATH="$EXT_CLASSPATH:$ext"
            fi
        done
    elif [ -f "$SCALA_HOME/build/pack/lib/scala-partest.jar" ] ; then
        for lib in `echo "partest library compiler"`; do
            ext="$SCALA_HOME/build/pack/lib/scala-$lib.jar"
            if [ -z "$EXT_CLASSPATH" ] ; then
                EXT_CLASSPATH="$ext"
            else
                EXT_CLASSPATH="$EXT_CLASSPATH:$ext"
            fi
        done
    fi
fi

if $cygwin; then
    if [ "$OS" = "Windows_NT" ] && cygpath -m .>/dev/null 2>/dev/null ; then
        format=mixed
    else
        format=windows
    fi
    SCALA_HOME=`cygpath --$format "$SCALA_HOME"`
    EXT_CLASSPATH=`cygpath --path --$format "$EXT_CLASSPATH"`
fi

# Reminder: substitution ${JAVA_OPTS:=-Xmx256M -Xms16M} DO NOT work on Solaris
[ -n "$JAVA_OPTS" ] || JAVA_OPTS="-Xmx512M -Xms16M"
[ -n "$SCALAC_OPTS" ] || SCALAC_OPTS="-deprecation"

partestDebugStr=""
if [ ! -z "${PARTEST_DEBUG}" ] ; then
  partestDebugStr="-Dpartest.debug=${PARTEST_DEBUG}"
fi

${JAVACMD:=java} $JAVA_OPTS -cp "$EXT_CLASSPATH" ${partestDebugStr} -Dscala.home="${SCALA_HOME}" -Dpartest.javacmd="${JAVACMD}" -Dpartest.java_opts="${JAVA_OPTS}" -Dpartest.scalac_opts="${SCALAC_OPTS}" -Dpartest.javac_cmd="${JAVA_HOME}/bin/javac" scala.tools.partest.nest.NestRunner  "$@"
