#! /bin/sh
file=`pwd`/test-port
prefix="/usr/local"
exec_prefix="${prefix}"
mpiexec="${exec_prefix}/bin/mpiexec"
starter="$mpiexec -n 1"

rm -f $file

# Default number of processes to start (the test is for large numbers
# of processes; use "manyconnect 10" to start with fewer for debugging)
nconn=100
# To avoid problems with listner queues, we pause every blockstart
# processes
blockstart=10
if [ "$1" = "-singleton" ] ; then
    starter=eval
    shift
fi
if [ -n "$1" ] ; then
    nconn=$1
fi
# Start one and sleep to avoid problems with two processes opening the
# test-port file for writing
$starter ./testconnect $file $nconn 1 &
sleep 1
nstarted=2
blockcount=2
while [ $nstarted -lt $nconn ] ; do
    #echo "Starting $nstarted"
    $starter ./testconnect $file $nconn $nstarted &
    echo "Started process $! (num $nstarted)"
    nstarted=`expr $nstarted + 1`
    blockcount=`expr $blockcount + 1`
    if [ -n "$2" ] ; then
         sleep $2
    elif [ $blockcount -ge $blockstart ] ; then
        $blockcount=0
        sleep 1
    fi
done
#echo "Starting $nstarted"
$starter ./testconnect $file $nconn $nstarted
wait
