#!/bin/sh

if [ "" = "$CONS_DIR" ]
then
	CONS_DIR=/usr/farm
	export CONS_DIR
fi

if [ "$#" = 0 ]
then
	set : -h
	shift
fi

case "$1" in
-h*|-\?*) echo "usage:	$0 farmlet 'cmd1' 'cmd2'...	-- run cmds on hosts"
	  echo "	$0 -l				-- list farmlets"
	  echo "	$0 -h				-- list print this help"
	  exit
	;;
-l)	cd $CONS_DIR/lib/farmlets
	for i in *
	do
		echo "$i:"
		cat $i
	done
	exit
	;;
-s)     ls $CONS_DIR/lib/farmlets
        exit
        ;;
esac
case $1 in

*:*)	listfile=/tmp/gang$$
	farmlist $1 > $listfile
	shift
	;;
*)
	if [ -f "$1" ]
	then
		listfile=$1
		shift
	elif [ -f $CONS_DIR/lib/farmlets/$1 ]
	then
		listfile=$CONS_DIR/lib/farmlets/$1
		shift
	else
		echo "$1: no such farmlet"
		exit
	fi
	;;
esac

for machine in `cat $listfile`
do
	netaddr=`grep $machine /etc/hosts | sed -e 's/ .*//'`
	gateway=`grep $machine /etc/hosts | sed -e 's/.*gateway //'`
	echo
	echo "- - - - - - - - - - - - - - $machine - - - - - - - - - - - - - -"
	for command in "$@"
	do
		eval rsh $machine \""$command"\"
	done
done 

rm -f /tmp/gang$$

echo `date` : $* >> $CONS_DIR/log/farm
