#!/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'

help=""
time=""
verbose=""
extra_cp=""
ext=""
rtdev=""
java_args=""
args=""
optimize=""

if [ "$CROSS_COMPILE_BGP" = "true" ]; then
    x10rt_impl="pgas_bgp"
    args="$args -STATEMENT_EXPR_GCC_WORKAROUND=true"
else
    x10rt_impl="sockets"
fi

parse=true
while [ -n "$1" ]; do
    if [ -z "$parse" ]; then
        args="$args '$1'"
        shift
        continue
    fi
    case "$1" in
        -h|-help|--help) help="1"; break;;
        -v|-verbose|--verbose) verbose="1";;
        -t|-time) time="time ";;
        -classpath|-cp) shift; extra_cp="$1";;
        -profile) profile="-Xrunhprof:cpu=samples";;
        -config) shift; config="$1.cfg";;
        -extclass) shift; ext=$1;;
        -x10rt) shift; x10rt_impl=$1;;
        -O|-optimize) optimize="true";;
        -rtdev) rtdev="true";;
        -J*) java_args="${java_args} '${1##-J}'";;
        --) parse=;;
        *) args="$args '$1'";;
    esac
    shift
done

if [ -n "$help" -o -z "$args" ]; then
    cat << EOF
Usage: x10c [options] <source-file>.x10 ...
where [options] includes:
    -sourcepath <path>       source path
    -classpath <path>        search path for class files
    -config <conf>           read configuration <conf> from etc${FILE_SEP}<conf>.cfg
    -version                 print version info
    -x10rt <impl>            select x10rt impl (lapi, pgp, sockets, mpi, standalone)
    -t -time                 display execution time
    -v -verbose --verbose    print verbose debugging information
    -h -help --help          print this message
    -disable <pass>          disable compiler pass <pass>
                             valid passes are: async-elimination
    -O -optimize             generate optimized code

    Use "x10c -- -help" to get more detailed help on compiler options
EOF
    exit 1
fi

[ -z "$DEXT" ] && DEXT="x10c.ExtensionInfo"
[ -z "$ext" ] && ext="$DEXT"

CONFIG_DIR="${TOP}${FILE_SEP}etc"
LIB_DIR="${TOP}${FILE_SEP}lib"
STDLIB_DIR="${TOP}${FILE_SEP}stdlib"

#[ -z "$config" ] && config="${CONFIG_FILE}"
[ -n "$config" ] && config="-Dx10.configuration=\"${CONFIG_DIR}${FILE_SEP}${config}\""

[ -n "$extra_cp" ] && args="$args -classpath \"${extra_cp}\""

WALA_JARS="${LIB_DIR}${FILE_SEP}${WALA_JAR}"
WALA_JARS="${WALA_JARS}${PATH_SEP}${LIB_DIR}${FILE_SEP}${WALA2_JAR}"
WALA_JARS="${WALA_JARS}${PATH_SEP}${LIB_DIR}${FILE_SEP}${WALA3_JAR}"
WALA_JARS="${WALA_JARS}${PATH_SEP}${LIB_DIR}${FILE_SEP}${WALA4_JAR}"
WALA_JARS="${WALA_JARS}${PATH_SEP}${LIB_DIR}${FILE_SEP}x10wala.jar"
WALA_JARS="${WALA_JARS}${PATH_SEP}${LIB_DIR}${FILE_SEP}org.eclipse.equinox.common_3.6.0.v20100503.jar"

classpath="${CP_OVERRIDE}"
classpath="${classpath}${LIB_DIR}${FILE_SEP}x10c.jar${PATH_SEP}${LIB_DIR}${FILE_SEP}${LPG_JAR}${PATH_SEP}${WALA_JARS}"

# Set flags to generate optimized code if we've been asked to do so.
[ "$optimize" = "true" ] && args="-OPTIMIZE=true $args"

# include libx10.properties as a x10lib unless rtdev
[ -z "$rtdev" ] && args="-x10lib ${STDLIB_DIR}/libx10.properties $args"

# pass x10rt_impl via environment variable
export X10RT_IMPL=$x10rt_impl

[ -z "$MAIN" ] && MAIN="polyglot.main.Main"

command="\"$JAVA\" -Xmx1024m -Dfile.encoding=UTF-8 -Dx10.dist=\"$TOP\" -Dx10c.ecj.jar=\"$ECJ_JAR\" -Dx10c.math.jar=\"$MATH_JAR\" $config $profile -classpath \"$classpath\" ${java_args} $MAIN -extclass $ext $args"

[ -n "$verbose" ] && echo "$command"

eval "$time$command"
exit $?

