#!/bin/bash

##############################################################################################################
##
##  Start Jaguar Server On All Hosts
##
##  ./jaguarstart_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`
clusterfile="$JAGUAR_HOME/conf/cluster.conf"
allhosts=`cat $clusterfile|grep -v '#'|grep -v '@'`

echo

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

if [[ -f "$clusterfile" ]]; then
    ## echo "OK, $clusterfile is found"
	/bin/true
else
    echo "$clusterfile is not found, exit"
    exit 1
fi

myips=`hostname -I`

((hasExtra=0))
for h in $allhosts; do
    if echo $myips|grep -q $h; then 
        $JAGUAR_HOME/bin/jaguarstart 
    else
        jh=`ssh $h 'if [ -f ~/.jaguarhome ]; then cat ~/.jaguarhome; else echo $HOME/jaguar; fi'`
        echo "ssh $h jaguarstart"
        ssh $h "$jh/bin/jaguarstart" &
        ((hasExtra=1))
    fi

done
((hasExtra==1)) && wait

echo "Jaguar database server is being started on all hosts ..."
echo  " "

