#!/bin/bash

checkstatus() {
	j="0"
	while [[ $j -lt 150 ]]
	do
	echo "Not ready yet. ($j/150)" >&2
	status=`bin/status`
	if [[ "$status" = "Running ..." ]]
	then
		returnStatus="0"
		break
	fi
	j=$[$j+1]
	if [[ $j -eq 150 ]]
	then
		returnStatus="1"
	fi
	done
	echo "$returnStatus"
}

execute() {
	echo "Executing version..." >&2
	bin/client version >&2
	echo "$?"
}

i="0"

while [[ $1 -lt 100 ]]
do
echo "run number: $i/100"
echo "starting..."
bin/start
if [[ "$(checkstatus)" = "1" ]]
then
echo "Failed to connect"
pkill -f karaf -e
break;
fi
echo "Wait a little..."
sleep 5
if [[ "$(execute)" = "1" ]]
then
echo "Unable to execute version -> deadlock..." >&2
break;
fi
echo "stopping..."
bin/stop
echo "I need to rest. Give me 5 seconds..."
sleep 5
i=$[$i+1]
done
