#!/bin/bash

if [ -z "${CNC_HOME}" ]; then
	echo ""
	echo "ERROR CNC_HOME environment variable must be defined and point to an HJ Distribution"
	echo ""
    exit 1
fi

export HJ_HOME=${CNC_HOME}
HJ=${HJ_HOME}/bin/hj

help=""
verbose=""
args=""
HJ_OPT="-rt=wsh"
NW="1"

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;;
        -classpath|-cp) shift; USER_CLASSPATH="$1";;
        -verbose) verbose="yes";;
        -CNC_NUM_WORKERS) shift; NW="$1";;        
        *) args="$args '$1'";;
    esac
    shift
done

if [ -n "$help" -o -z "$args" ]; then
    cat << EOF
Usage: cnc_run [options] <main-class> [arg0 arg1 ...]
where [options] includes:
    -h -help --help          print this message        
    Use "hj -- -help" to get help on Hj runtime configuration options
EOF
    exit 1
fi

if [ ! -z $USER_CLASSPATH ]; then
	HJ_OPT="${HJ_OPT} -cp $USER_CLASSPATH"
fi


#HJ_OPT="${HJ_OPT} -INIT_THREADS_PER_PLACE=$NW -NUMBER_OF_LOCAL_PLACES=1"
HJ_OPT="${HJ_OPT} -places 1:$NW"

CMD="\"$HJ\" $HJ_OPT CnCHJ.runtime.CnCRuntime $args"	

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

eval "$CMD"
exit $?

