if [ -x /bin/which ] ; then which=/bin/which
elif [ -x /usr/bin/which ] ; then which=/usr/bin/which
else which=which ; fi

dirname=`$which dirname`
[ -x "$dirname" ] || dirname=/bin/dirname
cfg="`$dirname \"$0\"`/config"

usage() {
	echo "Usage: start [-h HOST] UUID < SCRIPT" >&2
	exit 1
}

if [ "$1" = "-h" ] ; then
	shift

	## remote execution
	if [ $# -ne 2 ] ; then
		usage
	fi

	host=$1 ; shift
	id=$1; shift

	. "$cfg"

	heredoctag="61992cf306e537684fbfec0963885a45"
	(
		echo "id=\"$id\"" ;
		echo "prefix=\"$rprefix\"" ;
		echo "cat > \"$prefix..SCRIPT\" <<'$heredoctag'" ; 
		echo "echo \$\$ > \"$prefix..SCRIPT.PID\"" ;
		cat ; 
		echo "$heredoctag" ; 
		cat "$bindir/start.sh"
	) | eval $sshcmd
	exitcode=$?
	if [ $exitcode -eq 255 ] ; then
		echo "SSH client has failed for '$host'. Check that user and host names are correct, and the host is reachable. Click on 'Push SSH Key' button in Table View to verify remote host identity." >&2
		exit 255
	else
		exit $exitcode
	fi
elif [ $# -eq 1 ] ; then

	## local execution

	id="$1" ; shift

	. "$cfg"

	echo "echo \$\$ > \"$prefix..SCRIPT.PID\"" > "$prefix..SCRIPT"
	cat >> "$prefix..SCRIPT"

	## The worker script will use $id, $prefix variables, and $prefix.SCRIPT file
	. "$bindir/start.sh"
else
	usage
fi
