#!/bin/bash

##############################################################################################################
##
##  Execute a command on all jaguar hosts
##
##  ./sshall "command"
##
##
##############################################################################################################

if [[ ! -f "$HOME/.jaguarhome" ]]; then
	echo "Jaguar has not been installed, quit"
	exit 1
fi

if (( $# < 1 )); then
	echo "Usage:  $0 command arg1 arg2 ..."
	exit 1
fi

unset LD_LIBRARY_PATH

myips=`hostname -I`

JAGUAR_HOME=`cat $HOME/.jaguarhome`
clusterfile="$JAGUAR_HOME/conf/cluster.conf"
allhosts=`grep -iv '#' $clusterfile|grep -v '@'`

if [[ -f "$clusterfile" ]]; then
	/bin/true
else
    echo "$clusterfile is not found, exit"
    exit 1
fi

((rc=0))
for h in $allhosts; do
    if echo $myips |grep -q $h; then
   	    echo "Local host $@"
        $@
    else
   	    echo "ssh [$h] $@"
   	    ssh $h "$@" &
    fi
	echo "================================================================"
done
wait
