#!/bin/sh

BINDIR=$(cd $(dirname "$0"); pwd)
. ${BINDIR}/set-env.sh
if [ -z "$COH_HOME" ]; then
    echo Cannot run $0, environment not correctly set
    echo Please examine bin/set-env.sh and set OBE_HOME and COH_HOME
    kill -2 $$
fi


# specify the JVM heap size
MEMORY=128m


if [ -f $JAVA_HOME/bin/java ]; then
  JAVAEXEC=$JAVA_HOME/bin/java
else
  JAVAEXEC=java
fi

HOSTNAME=`hostname`
JAVA_OPTS="-Xms$MEMORY -Xmx$MEMORY -Dtangosol.coherence.machine=testmachine "

JAVA_OPT=""
#JAVA_OPTS="${JAVA_OPTS} -Dtangosol.coherence.log=log4j "
#JAVA_OPTS="${JAVA_OPTS} -Dpid=$$"
JAVA_OPTS="${JAVA_OPTS} -Dtangosol.coherence.ttl=0"
JAVA_OPTS="${JAVA_OPTS} -Dtangosol.coherence.wka=localhost"
JAVA_OPTS="${JAVA_OPTS} -Dtangosol.coherence.distributed.localstorage=false"
JAVA_OPTS="${JAVA_OPTS} -Dtangosol.pof.enabled=true"
JAVA_OPTS="${JAVA_OPTS} -Dtangosol.coherence.clusterport=12358"
JAVA_OPTS="${JAVA_OPTS} -Dtangosol.coherence.override=coherence-override-dev.xml"
JAVA_OPTS="${JAVA_OPTS} -Dtangosol.coherence.cacheconfig=cache-configuration.xml"
JAVA_OPTS="${JAVA_OPTS} -Dtangosol.coherence.log.level=9"



CCP="./classes:./config:."
CCP="${CCP}:$COH_HOME"
CCP="${CCP}:$COH_HOME/client/ct.jar"
CCP="${CCP}:$COH_HOME/client/domain.jar"
CCP="${CCP}:$COH_HOME/lib/log4j-1.2.16.jar"
CCP="${CCP}:$COH_HOME/lib/coherence.jar"


COUNT=3
p=0

declare -a PIDS

while [ $p -lt $COUNT ]
do
    echo "Starting sim $p to log sim.logs/sim.$p.log"
    $JAVAEXEC -server -showversion $JAVA_OPTS -Dconfig.which=${p} -Dconfig.file=${OBE_HOME}/sim/sim.config.${p}.properties -cp ${CCP} example.simulation.Driver > sim.logs/sim.${p}.log 2>&1 &    PID=$!
    PIDS[ $p + 1 ]=$PID
    p=`expr $p + 1`
	sleep 1
done

ps


p=1
PIDLIST=""
while [ $p -le $COUNT ]
do
    PIDLIST="${PIDS[$p]} $PIDLIST" 
    p=`expr $p + 1`
done

echo Running pids = "$PIDLIST"
echo $PIDLIST > /tmp/coherence.PIDLIST.SIM


