#!/usr/bin/env bash
# DO NOT EDIT - This file was automatically generated.
UNAME=`uname -smp | sed -e 's/ /,/g'`
FILE_SEP='/'; if [[ "$UNAME" = CYGWIN* ]]; then FILE_SEP='\\'; fi
PATH_SEP=':'; if [[ "$UNAME" = CYGWIN* ]]; then PATH_SEP=';'; fi

prog="$(readlink "$0" 2>&1)"
[ $? -eq 127 -o "$prog" = "" ] && prog="$0"
TOP="$(cd "$(dirname "$prog")/.." && pwd)"
if [[ "$UNAME" = CYGWIN* ]]; then PATH="$PATH:$TOP/lib"; TOP="$(cygpath -am "$TOP")"; fi

[ -n "$JAVA_HOME" ] || JAVA_HOME='/usr/local/openjdk6'
[ -n "$JRE_HOME" ] || JRE_HOME='/usr/local/openjdk6/jre'
if [[ "$UNAME" = CYGWIN* ]]; then JAVA_HOME="$(cygpath -am "$JAVA_HOME")"; JRE_HOME="$(cygpath -am "$JRE_HOME")"; fi
if [ -n "$JAVA_HOME" -a -e "$JAVA_HOME/bin/java" ]; then
    JAVA="$JAVA_HOME/bin/java"
    JAR="$JAVA_HOME/bin/jar"
elif [ -n "$JRE_HOME" -a -e "$JRE_HOME/bin/java" ]; then
    JAVA="$JRE_HOME/bin/java"
    JAR="$JRE_HOME/bin/jar"
else
    echo "JAVA_HOME ($JAVA_HOME) is not pointing to a JRE or a JDK"
    exit 1
fi
if [[ "$UNAME" = CYGWIN* ]]; then JAVA="$(cygpath -au "$JAVA")"; JAR="$(cygpath -au "$JAR")"; fi

# NOTE: the above assumes that Cygwin==win32 (probably valid)

#CONFIG_FILE='standard.cfg'
LPG_JAR='lpg.jar'
ECJ_JAR='ecj.jar'
WALA_JAR='com.ibm.wala.cast.java_1.0.0.201101071300.jar'
WALA2_JAR='com.ibm.wala.cast_1.0.0.201101071300.jar'
WALA3_JAR='com.ibm.wala.core_1.1.3.201101071300.jar'
WALA4_JAR='com.ibm.wala.shrike_1.3.1.201101071300.jar'
MATH_JAR='commons-math-2.2.jar'
CROSS_COMPILE_BGP='false'

testFailed() {
  rc=$?

  echo ""
  echo ""
  echo "FAILURE!!!"
  exit $rc
}

echo "Running precommit tests for X10"

ROOT="$TOP/.."

echo "Rebuilding X10"

cd "$ROOT/x10.dist"
target=dist
[ "$1" = "cpp" -o "$1" = "-cpp" -o "$1" = "-c++" ] && target=dist-cpp && shift
[ "$1" = "java" -o "$1" = "-java" ] && target=dist-java && shift
nice ant "$@" squeakyclean || testFailed
nice ant -Doptimize=true "$@" $target || testFailed

echo "Making temp dir"
if [ -z "$TMP_DIR" ]; then
  export TMP_DIR=/tmp/precommit-$USER
fi
if [[ "$UNAME" = CYGWIN* ]]; then TMP_DIR="$(cygpath -am "$TMP_DIR")"; fi

rm -rf "$TMP_DIR"
mkdir -p "$TMP_DIR"

echo "Testing"

if [ "$target" != "dist-java" ]; then

echo "Compiling PreCommit.x10 with x10c++"

cd "$ROOT/x10.tests/tests/Misc"

nice "$ROOT/x10.dist/bin/x10c++" -O -DEBUG \
 -sourcepath "$ROOT/x10.dist/samples/tutorial" \
 -sourcepath "$ROOT/x10.tests/examples/x10lib" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Async" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/At" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Atomic" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Call" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Cast" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Clock" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Closures" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/DepType" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Distribution" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/For" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Generics" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Import" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Instanceof" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Place" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Region" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Structs" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Typedefs" \
-d "$TMP_DIR" -o "$TMP_DIR/PreCommit" PreCommit.x10 || testFailed

echo "Running"

X10_NPLACES=2 X10_HOSTLIST=localhost "$ROOT/x10.dist/bin/runx10" "$TMP_DIR/PreCommit" || testFailed

fi

if [ "$target" != "dist-cpp" ]; then

echo "Compiling PreCommit.x10 with x10c"

# create temp dir for classes
rm -rf "${TMP_DIR}.tmp"
mkdir -p "${TMP_DIR}.tmp"

cd "$ROOT/x10.tests/tests/Misc"

nice "$ROOT/x10.dist/bin/x10c" -O \
 -sourcepath "$ROOT/x10.dist/samples/tutorial" \
 -sourcepath "$ROOT/x10.tests/examples/x10lib" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Async" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/At" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Atomic" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Call" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Cast" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Clock" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Closures" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/DepType" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Distribution" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/For" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Generics" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Import" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Instanceof" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Place" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Region" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Structs" \
 -sourcepath "$ROOT/x10.tests/examples/Constructs/Typedefs" \
-d "${TMP_DIR}.tmp" -o "$TMP_DIR/PreCommit.jar" PreCommit.x10 || testFailed

echo "Running"

"$ROOT/x10.dist/bin/x10" -classpath "$TMP_DIR/PreCommit.jar" PreCommit || testFailed

fi

echo ""
echo ""
echo "SUCCESS"
