#!/bin/bash

##############################################################################################################
##
##  Server Uninstallation Script
##
##  ./uninstall_jaguar_database_on_all_hosts.sh
##
##############################################################################################################


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

echo
echo "======================================================================================================================================"
echo "!!! Warning! This operation is extremely dangerous since it will wipe out your JaguarDB program and data on all hosts completely !!!"
echo "======================================================================================================================================"
echo

jaguarhome=`cat $HOME/.jaguarhome`
echo "Jaguar home directory is $jaguarhome"

myip=`hostname -I|cut -d' ' -f1`
myips=`hostname -I`

JAGUAR_HOME=$jaguarhome
if [[ -e "$JAGUAR_HOME" ]]; then
    echo "OK, found Jaguar home directory $JAGUAR_HOME ..."
else
    echo "JAGUAR_HOME $JAGUAR_HOME does not exist, exit"
    exit 1
fi

if [[ "x$JAGUAR_HOME" = "x$HOME" ]]; then
	echo "Wrong $JAGUAR_HOME exit"
	exit 1
fi

clusterfile="$JAGUAR_HOME/conf/cluster.conf"
if [[ -f "$clusterfile" ]]; then
	/bin/true
else
    echo "$clusterfile is not found, exit"
    exit 1
fi


allhosts=`grep -v '#' $clusterfile | grep -v '@'| awk '{print $1}' `

((deleteall=0))
echo
echo -n "Do you want to delete all data in the database system too? (y|n) "
read ans
if [[ "x$ans" = "xy" ]]; then
	echo -n "Are you sure you want to delete the data? (y|n) "
	read ans2
	if [[ "x$ans2" = "xy" ]]; then
		((deleteall=1))
		echo "OK. Data will be deleted too ..."
	else
		((deleteall=0))
		echo "OK. Data will be kept and not deleted ..."
	fi
else
	((deleteall=0))
	echo "OK. Data will be kept and not deleted ..."
fi


if [[ -f "$JAGUAR_HOME/bin/jaguarstop_on_all_hosts.sh" ]]; then
    echo
    echo "Stop Jaguar on all hosts ..."
    echo "$JAGUAR_HOME/bin/jaguarstop_on_all_hosts.sh"
    $JAGUAR_HOME/bin/jaguarstop_on_all_hosts.sh
    echo
fi


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

   	if ((deleteall==0)); then
   		cmd="/bin/rm -rf $jh/include $jh/lib $jh/bin $jh/doc $jh/tmp"
   	else
   		cmd="/bin/rm -rf $jh/*"
   	fi
    cmdhm="/bin/rm -f $hmdir/.jaguarhome"

    if echo $myips |grep -q $h; then
        echo "On local host:" 
        echo "You can manually run:  $cmd"
        echo "You can manually run:  $cmdhm"
    else
        echo ssh $h $cmd

        echo ssh $h $cmd 
        ssh $h "$cmd" &

        echo ssh $h $cmdhm
        ssh $h "$cmdhm" &

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

echo "Jaguar database has been uninstalled on all hosts"

