#!/bin/bash

####################################################################################
##
##  Check if Jaguar server and jql  binaries are same on all hosts
##
##  ./check_binaries_on_all_hosts.sh
##
#####################################################################################

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

unset LD_LIBRARY_PATH

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

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

myips=`hostname -I`

function checkit()
{
	prog=$1
	h=$2

    if echo $myips|grep -q $h; then
        jh=$JAGUAR_HOME
        echo "Local host: md5sum $jh/bin/$prog"
        md5=`md5sum $jh/bin/$prog|cut -d' ' -f1`
        echo "$h md5sum of $jh/bin/$prog is $md5"
    else
        jh=`ssh $h 'if [ -f ~/.jaguarhome ]; then cat ~/.jaguarhome; else echo $HOME/jaguar; fi'`
   	    md5=`ssh $h "md5sum  $jh/bin/$prog|cut -d' ' -f1"` 
        echo "$h md5sum of $jh/bin/$prog is $md5"
    fi
}


for h in $allhosts
do
    checkit jaguar.bin $h &
done
wait
echo

for h in $allhosts
do
    checkit jql.bin $h &
done
wait

exit 0

