#!/bin/bash
# (C) Copyright 2012,2013 Continuent, Inc - Released under the New BSD License
# Version 1.0.9 - 2013-12-18

cookbook_dir=$(dirname $0)
cd "$cookbook_dir/../"

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

check_installed
check_if_nodes_are_reachable
check_for_deprecated_installer

if [ -z "$HUB" ]
then
    echo "HUB undefined. Please update $cookbook_dir/NODES_STAR.sh"
    exit 1
fi

#$cookbook_dir/clear_cluster

echo "installing STAR" >$INSTALL_LOG
date >> $INSTALL_LOG

if [ -n "$DRYRUN" ]
then
    echo "WARNING: DRYRUN is deprecated - Future versions of Tungsten Cookbook will only support tpm-based installations"
    [ -z "$VERBOSE" ] && VERBOSE=1
fi 

COMMAND_SEQUENCE=1
if [ -n "$USE_TPM" ]
then
    check_security
    INSTALL_COMMAND="./tools/tpm install star \
        --reset \
        --log=timestamp \
        --replication-port=$DATABASE_PORT \
        --replication-user=$DATABASE_USER \
        --replication-password=$DATABASE_PASSWORD \
        --datasource-log-directory=$BINLOG_DIRECTORY \
        --home-directory=$TUNGSTEN_BASE \
        --datasource-mysql-conf=$MY_CNF \
        --datasource-log-directory=$BINLOG_DIRECTORY \
        -a --datasource-boot-script=$MYSQL_BOOT_SCRIPT \
        --repl-disable-relay-logs=true \
        --rmi-port=$RMI_PORT \
        --thl-port=$THL_PORT \
        --masters=$MASTERS_LIST \
        --hub=$HUB \
        --hub-service=$HUB_SERVICE \
        --master-services=$MASTER_SERVICES_LIST \
        --topology=star \
        $MORE_OPTIONS $MORE_TPM_OPTIONS $SECURITY_OPTIONS --$START_OPTION"

#        --members=$HOSTS_LIST \

    run_command $INSTALL_COMMAND 

else
# install STAR
INDEX=0
for NODE in ${MASTERS[*]}
do
  INSTALL_COMMAND="./tools/tungsten-installer \
    --master-slave \
    --master-host=$NODE \
    --datasource-port=$DATABASE_PORT \
    --datasource-user=$DATABASE_USER \
    --datasource-password=$DATABASE_PASSWORD \
    --datasource-log-directory=$BINLOG_DIRECTORY \
    --service-name=${MM_SERVICES[$INDEX]} \
    --home-directory=$TUNGSTEN_BASE \
    --cluster-hosts=$NODE \
    --disable-relay-logs \
    --datasource-mysql-conf=$MY_CNF \
    --datasource-log-directory=$BINLOG_DIRECTORY \
    -a --datasource-boot-script=$MYSQL_BOOT_SCRIPT \
    --property=replicator.service.comments=true \
    --rmi-port=$RMI_PORT \
    --thl-port=$THL_PORT \
    $MORE_OPTIONS $START_OPTION "

    echo "## $COMMAND_SEQUENCE (host: $NODE)" >> $INSTALL_LOG
    echo $INSTALL_COMMAND | perl -pe 's/--/\\\n\t--/g' >> $INSTALL_LOG
    if [ -n "$VERBOSE" ]
    then
        echo "## $COMMAND_SEQUENCE (host: $NODE)"
        echo $INSTALL_COMMAND | perl -pe 's/--/\\\n\t--/g'
    fi
    COMMAND_SEQUENCE=$(($COMMAND_SEQUENCE+1))
    if  [ -z "$DRYRUN" ]
    then
        $INSTALL_COMMAND
        if [ "$?" != "0"  ]
        then
            exit 1
        fi
    fi
    INDEX=$(($INDEX+1))
done


TUNGSTEN_TOOLS=$TUNGSTEN_BASE/tungsten/tools
HUB_DS=`echo $HUB|perl -lpe's/\W/_/g'`

# set -x
INDEX=0
for NODE in ${MASTERS[*]}
do
    if [ "$NODE" != "$HUB" ]
    then
        # setting a slave service in the spoke
        SPOKE_DS=`echo $NODE|perl -lpe's/\W/_/g'`

        INSTALL_COMMAND="$TUNGSTEN_TOOLS/configure-service \
            -C --quiet \
            --host=$NODE \
            --datasource=$SPOKE_DS \
            --local-service-name=${MM_SERVICES[$INDEX]} \
            --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 "## $COMMAND_SEQUENCE (host: $NODE)" >> $INSTALL_LOG
        echo $INSTALL_COMMAND | perl -pe 's/--/\\\n\t--/g' >> $INSTALL_LOG

        if [ -n "$VERBOSE" ]
        then
            echo "## $COMMAND_SEQUENCE (host: $NODE)"
            echo $INSTALL_COMMAND | perl -pe 's/--/\\\n\t--/g'
        fi
        COMMAND_SEQUENCE=$(($COMMAND_SEQUENCE+1))

        if  [ -z "$DRYRUN" ]
        then
            $INSTALL_COMMAND
            if [ "$?" != "0"  ]
            then
                exit 1
            fi
        fi
        # Setting a slave service on the hub
        INSTALL_COMMAND="$TUNGSTEN_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 \
            --master-thl-port=$THL_PORT \
            --svc-$START_OPTION \
            ${MM_SERVICES[$INDEX]}"

        echo "## $COMMAND_SEQUENCE (host: $HUB)" >> $INSTALL_LOG
        echo $INSTALL_COMMAND | perl -pe 's/--/\\\n\t--/g' >> $INSTALL_LOG

        if [ -n "$VERBOSE" ]
        then
            echo "## $COMMAND_SEQUENCE (host: $HUB)"
            echo $INSTALL_COMMAND | perl -pe 's/--/\\\n\t--/g'
        fi
        COMMAND_SEQUENCE=$(($COMMAND_SEQUENCE+1))

        if  [ -z "$DRYRUN" ]
        then
            $INSTALL_COMMAND

            if [ "$?" != "0"  ]
            then
                exit 1
            fi
        fi
    fi
    INDEX=$(($INDEX+1))
done
fi

# set +x

if  [ -n "$DRYRUN" ]
then
    echo "## $COMMAND_SEQUENCE (host: $(hostname)"
    echo "echo 'star' > $CURRENT_TOPOLOGY"
    exit
fi
echo "star" > $CURRENT_TOPOLOGY
$cookbook_dir/show_cluster 
post_installation
