#!/bin/bash

####################################################################################
##
##  Remove Jaguar Server Any Config Paramter On All Hosts
##
##  ./remove_config_on_all_hosts.sh   KEY  
##
#####################################################################################

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

unset LD_LIBRARY_PATH

myips=`hostname -I`

KEY=$1
if [[ "x$KEY" = "x" ]]; then
	echo "Usage: $0  KEY "
	exit 1
fi

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

for h in $allhosts
do
    if echo $myips | grep -q $h; then
        jh=$JAGUAR_HOME
        echo "Local host $h $jh "
        sed -i "s/.*$KEY*=/#$KEY=/g" $jh/conf/server.conf
    else
        jh=`ssh $h 'if [ -f ~/.jaguarhome ]; then cat ~/.jaguarhome; else echo $HOME/jaguar; fi'`
        echo "ssh $h $jh "
        ssh $h "sed -i 's/.*$KEY*=/#$KEY=/g' $jh/conf/server.conf"
    fi
done

echo "Jaguar config $KEY is commented out on all hosts"
exit 0

