#!/bin/bash

# Example: startNQ.sh			all players default host
# Example: startNQ.sh machine		all players on host 'machine'
# Example: startNQ.sh -r			all players reconnect to default host
# Example: startNQ.sh 1 2 3 4		players 1-4 on default host
# Example: startNQ.sh remote 1 2 		players 1-2 on host 'remote'
# Example: startNQ.sh remote -r 9 10 		players 9-10 reconnect to host 'remote'

PLAYER="./player"
GOALIE="./player"
COACH="./coach"
CLIENT_CONF="player.conf"
COACH_CONF="coach.conf"
SERVER_CONF="server.conf"
CONNECT_TYPE="UNum"
HOST="192.168.1.89"
PORT=6000
CPORT=6002
LIST="1 2 3 4 5 6 7 8 9 10 11 0"

if [ $# -gt 0 ]
then
	END=$#
	if echo "$1" | grep -q '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' ; then
		HOST=$1
		END=`expr $END - 1`
		shift 1
	fi
	if echo "$1" | grep -q '[-][r]' ; then
		CONNECT_TYPE="reconnect"
		END=`expr $END - 1`
		shift 1
	fi
	if [ $END -gt 0 ]
	then
		LIST="$@"
	fi
fi


echo "LoadingNQ : $LIST"
for LOOP in $LIST
do
	if [ $LOOP -eq 1 ]     #goalie
	then
		$GOALIE -file $SERVER_CONF -file $CLIENT_CONF -host $HOST -port $PORT -$CONNECT_TYPE $LOOP -goalie X &
		echo ">>>>>>>>>>>>>>>>>>>>>>>>>>Goalie[$CONNECT_TYPE-$LOOP]"
	elif [ $LOOP -ne 0 ]
	then                  #player 
		$PLAYER -file $SERVER_CONF -file $CLIENT_CONF -host $HOST -port $PORT -$CONNECT_TYPE $LOOP X &
		echo ">>>>>>>>>>>>>>>>>>>>>>>>>>Player[$CONNECT_TYPE-$LOOP]"	
	else
	 	$COACH  -host $HOST -file $COACH_CONF &
		echo ">>>>>>>>>>>>>>>>>>>>>>>>>>Coach"		
	fi
	sleep 1
done


