#!/bin/bash

####################################################################################
##
##  Display config parameter and values in server.conf on all hosts
##
##  ./list_configs_on_all_hosts.sh
##
#####################################################################################

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

unset LD_LIBRARY_PATH

myips=`hostname -I`

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

echo "==============================================================================================="
echo "== !!! Warning:  This script will copy the cluster.conf on this host to all other hosts !!!"
echo "== This operation will cause all other hosts to have the same cluster.conf file as this host."
echo "==============================================================================================="

echo
echo "The cluster.conf on your current host is:"
cat $clusterfile
echo

echo -n "Are you sure you want to proceed?  (y|n) "
read ans
if [[ "x$ans" = "xy" ]]; then
    echo "OK, you entered y, now we will proceed ..."
else
    echo "You did not enter y, quit now"
    exit 1
fi


for h in $allhosts; do
    if echo $myips |grep -q $h; then
        echo
    else
        jh=`ssh $h 'if [ -f ~/.jaguarhome ]; then cat ~/.jaguarhome; else echo $HOME/jaguar; fi'`
        echo "Copy $clusterfile to $h:$jh/conf/ ..."
        scp $clusterfile $h:$jh/conf/cluster.conf
    fi
    echo
done

exit 0
