#!/usr/local/bin/bash

verlihub=$1
shift
pid=

trap "StopHub" SIGQUIT SIGTERM SIGKILL SIGINT

function StartHub
{
	$verlihub $* &
	pid=$!
	wait $pid
	return $?
}

function StopHub
{
	if [ "_$pid" != "_" ]; then
		echo Killing hub..
		kill -3 $pid
		echo
	fi
}

code=1
until [[ $code != 1 ]]; do
	StartHub $*
	code=$?
	if [[ $code == 1 ]]; then
		echo Restarting in a while..
		sleep 5;
	else
		echo Stopping..
	fi;
done;

exit $code
