#!/bin/bash

##############################################################################################################
##
##  Copy a file to all hosts in jaguar cluster
##
##  ./scpall SOURCE_FILE   DEST_PATH
##
##
##############################################################################################################

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

if (( $# < 1 )); then
	echo "Usage:  $0  SOURCE_FILE  DEST_PATH"
	exit 1
fi

unset LD_LIBRARY_PATH

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

myips=`hostname -I`

srcfile=$1
destpath=$2

if [[ "x$destpath" = "x" ]]; then
	echo "Usage:  $0  SOURCE_FILE  DEST_PATH ..."
	exit 1
fi

((rc=0))
for h in $allhosts; do
    if echo $myips | grep -q $h; then
        echo "Local host $h no copy"
    else
        echo "scp $srcfile $h:$destpath"
        scp $srcfile $h:$destpath &
        sleep 1
    fi
	echo "================================================================"
done

wait
