#!/bin/bash

echo '*******************************************************'
echo '* SKLO Robocup Team starter                           *'
echo '*                                                     *'
echo '* Slovak University of Technology in Bratislava       *'
echo '* Faculty of Informatics and Information Technologies *'
echo '*                                                     *'
echo '* Copyright 2004. Team SKLO.                          *'
echo '* All rights reserved.                                *'
echo '*******************************************************'

rm -f Debug/*.txt
rm -f Log/*

team_name="Sklo"
server_host="localhost"

homedir=`dirname $0`
childcnt="0"
children=""

function killchildren {
	echo "Exit request. Stopping processes."
	for x in $children; do
		if kill -0 $x 2> /dev/null; then
			echo "Terminating process $x"
			kill $x 2> /dev/null
		fi
	done
	sleep 1
	for x in $children; do
		if kill -0 $x 2> /dev/null; then
			echo "Killing process $x"
			kill -9 $x 2> /dev/null
		fi
	done
	echo "All processes stopped. Exiting."
}

trap killchildren SIGINT SIGTERM SIGQUIT SIGHUP

echo
echo "Starting processes. Please Wait."
echo

$homedir/Output/GoalieExe/GoalieExe -t 11 -a -g -n $team_name -s $server_host > /dev/null 2>&1 &
children=${children}${children:+ }$!
let childcnt++
sleep 1

for i in 2 3 4 5 6 7 8 9 10 11; do
	$homedir/Output/PlayerExe/PlayerExe -t 11 -a -n $team_name -s $server_host > /dev/null 2>&1 &
	children=${children}${children:+ }$!
	let childcnt++
	sleep 1
done

$homedir/Output/CoachExe/CoachExe -t 11 -a -n $team_name -s $server_host -t 4 -l Logo > /dev/null 2>&1 &
children=${children}${children:+ }$!
let childcnt++
sleep 1

echo
echo "Hit Ctrl+C to terminate all processes."
echo

while [ "$childcnt" '>' "0" ]; do
	echo "Waiting for processes to die."
	echo "Currently $childcnt processes are executed."
	wait
	let childcnt--
done

echo "All processess are dead. Exiting."
exit 0

