#!/bin/bash

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

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

key=$1

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

for h in $allhosts; do
    if echo $myips |grep -q $h; then
        jh=$JAGUAR_HOME
        echo "local host $h $jh"
        if [[ -z $key ]]; then
            grep '=' $jh/conf/server.conf |grep -v grep|grep -v '#'
        else
            grep $key $jh/conf/server.conf | grep '=' |grep -v grep|grep -v '#'
        fi
    else
        jh=`ssh $h 'if [ -f ~/.jaguarhome ]; then cat ~/.jaguarhome; else echo $HOME/jaguar; fi'`
        echo "ssh $h $jh"

        if [[ -z $key ]]; then
            ssh $h "grep '=' $jh/conf/server.conf |grep -v grep|grep -v '#'"  
        else
            ssh $h "grep $key $jh/conf/server.conf|grep '=' |grep -v grep|grep -v '#'"  
        fi

    fi
    echo
done

exit 0
