#!/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)

if [ ! -f CURRENT_TOPOLOGY ]
then
    echo "This command requires an installed cluster"
    exit 1
fi

TOPOLOGY=$(echo $(cat CURRENT_TOPOLOGY) | tr '[a-z]' '[A-Z]')

NODES=NODES_$TOPOLOGY.sh

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

. $cookbook_dir/BOOTSTRAP.sh $NODES

echo $LONG_LINE
echo "Clearing up cluster with installed topology '$TOPOLOGY'"
# echo "---------------------------------------------------------------"
# check_current_topology $TOPOLOGY

are_you_sure_you_want_to_clear
if [ ! -f $MY_COOKBOOK_CNF ]
then
    write_my_cookbook_cnf
fi

if [ -f $cookbook_dir/data_loading ]
then
    $cookbook_dir/load_data stop
fi
 
TMPDIR=$PWD
# MYSQL is defined in BOOTSTRAP.sh
for NODE in ${ALL_NODES[*]} 
do 
    clear_remote_node=$TMPDIR/clear_node_$NODE.sh
    clear_remote_node_sql=$TMPDIR/clear_node_sql_$NODE.sh
    echo "#!/bin/bash" > $clear_remote_node 
   if [ "$STOP_REPLICATORS" == "1" ]
    then
        echo "ssh $NODE 'if [ -x $REPLICATOR ] ; then $REPLICATOR stop;  fi'" >> $clear_remote_node
    fi
    if [ "$REMOVE_TUNGSTEN_BASE" == "1" ]
    then
        echo "ssh $NODE 'rm -rf $TUNGSTEN_BASE/*'" >> $clear_remote_node
    fi  
    if [ "$REMOVE_SERVICE_SCHEMA" == "1" ]
    then
        for D in $($MYSQL -h $NODE -BN -e 'show schemas like "tungsten%"' )
        do
            echo "$MYSQL -h $NODE -e 'drop schema if exists $D'" >> $clear_remote_node_sql
        done
    fi
    if [ "$REMOVE_TEST_SCHEMAS" == "1" ]
    then
        echo "$MYSQL -h $NODE -e 'drop schema if exists test'" >> $clear_remote_node_sql
        for D in $($MYSQL -h $NODE -BN -e 'show schemas like "evaluator%"' )
        do
            echo "$MYSQL -h $NODE -e 'drop schema if exists $D'" >> $clear_remote_node_sql
        done
    fi
    if [ "$REMOVE_DATABASE_CONTENTS" == "1" ]
    then
        for D in $($MYSQL -h $NODE -BN -e 'show schemas ' | grep -v -w 'mysql\|information_schema\|performance_schema'  )
        do
            echo "$MYSQL -h $NODE -e 'drop schema if exists $D'" >>$clear_remote_node_sql
        done
    fi
    if [ "$CLEAN_NODE_DATABASE_SERVER" == "1" ]
    then
        echo "
        $MYSQL -h $NODE -e 'create schema if not exists test'
        $MYSQL -h $NODE -e 'set global read_only=0'
        $MYSQL -h $NODE -e 'set global binlog_format=mixed'
        $MYSQL -h $NODE -e 'reset master'" >> $clear_remote_node_sql
    fi
    echo "ssh $NODE 'if [ -f $TUNGSTEN_BASE/tungsten/CURRENT_TOPOLOGY ] ; then rm -f $TUNGSTEN_BASE/tungsten/CURRENT_TOPOLOGY ; fi'" >> $clear_remote_node
    chmod +x $clear_remote_node
    chmod +x $clear_remote_node_sql
    #echo "##############"
    #cat $clear_remote_node
    #cat $clear_remote_node_sql
    #echo "##############"
    $clear_remote_node > $TMPDIR/results$NODE &
done
wait

for NODE in ${ALL_NODES[*]} 
do 
    cat $TMPDIR/results$NODE
    $TMPDIR/clear_node_sql_$NODE.sh
    rm $TMPDIR/results$NODE
    rm $TMPDIR/clear_node_$NODE.sh
    rm $TMPDIR/clear_node_sql_$NODE.sh
done

for F in $INSTALL_LOG $CURRENT_TOPOLOGY $cookbook_dir/my.cookbook.cnf $cookbook_dir/db_use $cookbook_dir/current_install.summary
do
    [ -f $F ] && rm -f $F
done

if [ -n "$STAGING_INFO" ]
then
    for NODE in ${ALL_NODES[*]} 
    do 
        ssh $NODE "if [ -f $STAGING_INFO ] ; then  rm -f $STAGING_INFO ; fi"
    done
fi
