#!/bin/sh

# runs command (command line argument) on cluster defined in ./par/machinefile
# Usage: runall [-b] hostname
# The -b option runs each command in the background

backgroundJobs=0

displayUsage()
{
	echo "Usage: runall [-b] <command to run on nodes>"
}

if [ $# -lt 1 ]; then
	displayUsage 1>&2
	exit 1
fi

if [ "$1" = "-b" ] && [ $# -lt 2 ]; then
	displayUsage 1>&2
	exit 1
fi

if [ "$1" = "-b" ]; then
	backgroundJobs=1
	shift
fi

while read nodename rest  ; do
	if [ $backgroundJobs -ne 0 ]; then
		rsh $nodename "$@" < /dev/null &
	else
		echo -n "[$nodename]: "
		rsh $nodename "$@" < /dev/null;
	fi
done < machinefile_100

if [ $backgroundJobs -ne 0 ]; then
	wait
fi
