#!/bin/sh

DF3_ROLES="-DF DF +DF"
DF4_ROLES="-SB -CB +CB +SB -SB-Sweeper +SB-Sweeper"
MF_ROLES="-MF MF +MF"
FW_ROLES="-SFW -FW FW +FW +SFW"

ROLES="Sweeper ${DF3_ROLES} ${DF4_ROLES} ${MF_ROLES} ${FW_ROLES} DYNAMIC"
role="DYNAMIC"

host="localhost"
port="${DEFAULT_SSERVER_PORT:-6000}"
team="Zeng01"
reconnect_number=""
goalie="false"
with_debug_server="false"
debug_log="false"

usage()
{
	(echo "Usage: $0 [options]"
	 echo "Possible options are:"
	 echo "      --help"
	 echo "  -h, --host HOST"
	 echo "  -p, --port PORT"
	 echo "  -t, --team-name TEAM"
	 echo "  -r, --reconnect NUMBER"
	 echo "  -g, --goalie"
	 echo "  -R, --role"
	 echo "  -a, --acceptable-roles"
	 echo "  -d, --with-debug-server"
	 echo "  -D, --debug") 1>&2
}

while [ $# -gt 0 ]
do
	case $1 in

	--help)
		usage
		exit 0
		;;

	-h|--host)
		host=$2
		shift 1
		;;

	-p|--port)
		port=$2
		shift 1
		;;

	-t|--team-name)
		team=$2
		shift 1
		;;

	-r|--reconnect)
		reconnect_number=$2
		shift 1
		;;

	-g|--goalie)
		goalie="true"
		;;

	-R|--role)
		role=$2
		shift 1
		;;

	-a|--acceptable-roles)
		echo $ROLES
		exit 0
		;;

	-d|--with-debug-server)
		with_debug_server="true"
		;;

	-D|--debug)
		debug_log="true"
		;;

	*)
		usage
		exit 1
		;;
	esac

	shift 1
done



if [ "${goalie}" = "true" ]; then
	goalie_option="--goalie"
else
	goalie_option=""
fi

if [ ! -z "${reconnect_number}" ]; then
	reconnect_option="--reconnect ${reconnect_number}"
fi

if [ ${with_debug_server} = "true" ]; then
	with_debug_server_option="--with-debug-server"
fi

if [ ${debug_log} = "true" ]; then
	debug_log_option="--debug"
fi

options="-h ${host} -p ${port} -t ${team} \
	 ${reconnect_option} ${with_debug_server_option} ${debug_log_option}"

case $role in
	DYNAMIC)
		./puppets ${options}
		;;

	Sweeper|-DF|DF|+DF|-SB|-CB|+CB|+SB|-SB-Sweeper|+SB-Sweeper)
		./Zeng-DF ${options} --role ${role}
		;;

	-MF|MF|+MF|-SFW|-FW|FW|+FW|+SFW)
		./puppets ${options} --role ${role}
		;;

	*)
		echo "Can't accept role: \"${role}\""
		exit 1
		;;
esac
