#!/bin/bash
# (C) Copyright 2012,2013 Continuent, Inc - Released under the New BSD License
# Version 1.0.5 - 2013-04-03

cookbook_dir=$(dirname $0)

echo "WARNING: This command is deprecated - Future versions of Tungsten Cookbook will only support tpm-based installations"

if [ ! -f $cookbook_dir/BOOTSTRAP.sh ]
then
    echo "$cookbook_dir/BOOTSTRAP.sh not found"
    exit 1
fi

if [ ! -f $cookbook_dir/utilities.sh ]
then
    echo "$cookbook_dir/utilities.sh not found"
    exit 1
fi

. $cookbook_dir/BOOTSTRAP.sh NODES_STAR.sh
. $cookbook_dir/utilities.sh

check_current_topology "star"

function find_free_node {
    FREE_COUNT=0
    USED_SERVICE_COUNT=0
    N_INDEX=0
    FREE=()
    USED_SERVICE=()
    for NODE in ${ALL_NODES[*]} 
    do 
        echo -n "$NODE "
        free=$($TREPCTL -host $NODE services |grep role | awk '{print $3}'| wc -l)
        if [ "$free" == "0" ]
        then
				FREE[$FREE_COUNT]=$NODE
	            FREE_SERVICE[$FREE_COUNT]=${MM_SERVICES[$N_INDEX]}
	            FREE_COUNT=$(($FREE_COUNT+1))
			    echo "free"
	    else
	            echo "running"
		fi
		N_INDEX=$(($N_INDEX+1))
    done
    export FREE=(${FREE[*]})
    export FREE_SERVICE=(${FREE_SERVICE[*]})
}

echo "Looking for a free node"
find_free_node
echo "Looking for a free serviceName"
find_used_serviceName
a1=${MM_SERVICES[@]}
a2=${USED_SERVICE_U[@]}
freeServiceName=($(diff "$a1" "$a2" ))

NODE_TO_ADD=${FREE[0]}
SERVICE_TO_ADD=${freeServiceName[0]}
DONOR=${SLAVES[0]}

if [ "$NODE_TO_ADD" == '' ]
then
    echo 'No free nodes to add into the cluster'
    exit 1
fi

if [ "$SERVICE_TO_ADD" == '' ]
then
    echo 'No free service names to add into the cluster'
    exit 1
fi


echo "Populating $NODE_TO_ADD with data from $DONOR"


MYSQL="mysql -u $DATABASE_USER -p$DATABASE_PASSWORD -P $DATABASE_PORT"
MYSQLDUMP="mysqldump -u $DATABASE_USER -p$DATABASE_PASSWORD -P $DATABASE_PORT"

$MYSQLDUMP --all-databases --single-transaction -h $DONOR > /tmp/donor.dmp
$MYSQL -h $NODE_TO_ADD < /tmp/donor.dmp

rm /tmp/donor.dmp

echo "Adding $NODE_TO_ADD into the cluster"

echo "Creating Master replicator on new node"
date >> $INSTALL_LOG

INSTALL_COMMAND="./tools/tungsten-installer \
    --master-slave \
    --master-host=$NODE_TO_ADD \
    --datasource-user=$DATABASE_USER \
    --datasource-password=$DATABASE_PASSWORD \
    --datasource-port=$DATABASE_PORT \
    --service-name=$SERVICE_TO_ADD \
    --home-directory=$TUNGSTEN_BASE \
    --cluster-hosts=$NODE_TO_ADD \
    --datasource-mysql-conf=$MY_CNF \
    --datasource-log-directory=$BINLOG_DIRECTORY \
    --rmi-port=$RMI_PORT \
    --thl-port=$THL_PORT \
    $MORE_OPTIONS --$START_OPTION"     

if [ -n "$VERBOSE" ]
then
    echo $INSTALL_COMMAND | perl -pe 's/--/\n\t--/g'
fi

echo $INSTALL_COMMAND >> $INSTALL_LOG

$INSTALL_COMMAND

if [ "$?" != "0"  ]
then
    exit
fi


 

#Add the slave replicator to the new host for hub
 # setting a slave service in the spoke
SPOKE_DS=`echo $NODE_TO_ADD|perl -lpe's/\W/_/g'`
HUB_DS=`echo $HUB|perl -lpe's/\W/_/g'`

        INSTALL_COMMAND="tools/configure-service \
            -C --quiet \
            --host=$NODE_TO_ADD \
            --datasource=$SPOKE_DS \
            --local-service-name=$SERVICE_TO_ADD \
            --role=slave \
            --service-type=remote \
            -a --svc-allow-any-remote-service=true \
            --release-directory=$TUNGSTEN_BASE/tungsten \
            --skip-validation-check=THLStorageCheck \
            --master-thl-host=$HUB \
            --master-thl-port=$THL_PORT \
            --svc-$START_OPTION  $HUB_SERVICE "

        echo $INSTALL_COMMAND | perl -pe 's/--/\n\t--/g' >> $INSTALL_LOG

        if [ -n "$VERBOSE" ]
		then
    		 echo $INSTALL_COMMAND | perl -pe 's/--/\n\t--/g'
		fi
$INSTALL_COMMAND
if [ "$?" != "0"  ]
then
    exit
fi

# Setting a slave service on the hub
INSTALL_COMMAND="tools/configure-service \
            --quiet -C \
            --host=$HUB \
            --local-service-name=$HUB_SERVICE \
            --role=slave \
            --datasource=$HUB_DS \
            --log-slave-updates=true \
            --service-type=remote \
            --release-directory=$TUNGSTEN_BASE/tungsten \
            --skip-validation-check=THLStorageCheck \
            --master-thl-host=$NODE_TO_ADD \
            --master-thl-port=$THL_PORT \
            --svc-$START_OPTION \
            $SERVICE_TO_ADD"

        echo $INSTALL_COMMAND | perl -pe 's/--/\n\t--/g' >> $INSTALL_LOG

        if [ -n "$VERBOSE" ]
		then
    		  echo $INSTALL_COMMAND | perl -pe 's/--/\n\t--/g'
        fi
        $INSTALL_COMMAND

        if [ "$?" != "0"  ]
        then
            exit
        fi

$cookbook_dir/show_cluster
