#!/bin/bash

####################################################################################
##
##  Check if cluster.conf contains same servers on all hosts
##
##  ./check_servers_on_all_hosts.sh
##
#####################################################################################

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

unset LD_LIBRARY_PATH

if [[ ! -f $HOME/.jaguarhome ]]; then
    echo "$HOME/.jaguarhome is not found, exit"
    exit 1
fi

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

if [[ ! -f "$clusterfile" ]]; then
    echo "$clusterfile is not found, exit"
    exit 1
fi

myips=`hostname -I`

function checkit()
{
	h=$1

    if echo $myips | grep -q $h; then
	    /bin/ls -l $JAGUAR_HOME/conf/cluster.conf
    else
        jh=`ssh $h 'if [ -f ~/.jaguarhome ]; then cat ~/.jaguarhome; else echo $HOME/jaguar; fi'`
	    ssh $h /bin/ls -l $jh/conf/cluster.conf
    fi
}


####### main ####
for h in $allhosts
do
    checkit "$h" &
done
wait

exit 0
