
export LANG=zh_CN.utf8
export LC_ALL=zh_CN.utf8

#改变工作目录到当前脚本所在路径
if [[ "$0" =~ / ]]; then cd "${0%/*}"; fi
export CWD=`pwd`

keyspace=$1

clearkeyspace() {
    if [[ "${keyspace}" != *"test"* ]]; then
        echo "can't clear keyspace ${keyspace}"
        exit 1
    fi

    datapath=`cat -n /opt/matrix/cassandra/conf/cassandra.yaml | grep "data_file_directories:" | awk '{print 1+$1}' | xargs -i% sed -n %p /opt/matrix/cassandra/conf/cassandra.yaml | awk '{print $2}'`

    echo
    echo "ls -l \"${datapath}\""
    eval "cd \"${datapath}\""

    ksroot=`ls | grep "^${keyspace}$"`
    ksbitlog=`ls | grep "^${keyspace}.*bitlog$"`
    ksnative=`ls | grep "^${keyspace}.*native$"`
    kstsdb=`ls | grep "^${keyspace}.*tsdb$"`

    echo ${ksroot}
    echo ${ksbitlog}
    echo ${ksnative}
    echo ${kstsdb}

    echo "ls -lR ${keyspace}* | wc"
    ls -lR ${keyspace}* | wc

    cd /opt/matrix/cassandra/bin
    ./cqlsh -u cassandra -p cassandra << EOF
desc keyspaces;
exit;
EOF

# drop keyspace
if [[ "$1" = "drop" ]]; then

    echo drop keyspace ${ksroot} ...
    ./cqlsh -u cassandra -p cassandra << EOF
drop keyspace ${ksroot};
exit;
EOF

    echo drop keyspace ${ksbitlog} ...
    ./cqlsh -u cassandra -p cassandra << EOF
drop keyspace ${ksbitlog};
exit;
EOF

    echo drop keyspace ${ksnative} ...
    ./cqlsh -u cassandra -p cassandra << EOF
drop keyspace ${ksnative};
exit;
EOF

    echo drop keyspace ${kstsdb} ...
    ./cqlsh -u cassandra -p cassandra << EOF
drop keyspace ${kstsdb};
exit;
EOF

    ./cqlsh -u cassandra -p cassandra << EOF
desc keyspaces;
exit;
EOF

fi # drop

    echo
    echo "ls -l \"${datapath}\""
    eval "cd \"${datapath}\""
    ls -l

    echo
    echo "removing keyspace ${keyspace} data ..."

    rm -rf ${ksroot}
    rm -rf ${ksbitlog}
    rm -rf ${ksnative}
    rm -rf ${kstsdb}

    ls -l
}

# 传播并执行脚本
spread() {
    args="$* "
    echo "args='$args'"

    host1=27.193.12.228
    host2=27.193.12.229
    host3=27.193.12.230
    host4=27.193.12.231
    host5=27.193.12.232
    host228=27.193.12.228
    host229=27.193.12.229
    host230=27.193.12.230
    host231=27.193.12.231
    host232=27.193.12.232

    while [[ "${args}" != "" ]]; do
        an=`echo $args | awk -F ' ' '{print $1}'`
        ip=`eval "echo $""{host${an}}"`
        args=`eval "echo $""{args:${#an}}"`
        echo "$an: $ip"
        if [[ "${ip}" != "" ]]; then
            ./msh p=matrix "sftp matrix@${ip}" "mkdir /opt/matrix/test" "cd /opt/matrix/test" "put initkeyspace.sh" "ls -lrt"
            ./msh p=matrix "ssh matrix@${ip}" "/opt/matrix/test/initkeyspace.sh ${keyspace} clear"
            sleep 2
        fi
    done
}

if [[ "$2" = "clear" ]]; then
    echo "clear keyspace ${keyspace}"
    clearkeyspace
    exit $?
elif [[ "${keyspace}" != "" ]]; then
    clearkeyspace drop
    cd "$CWD"
    if [[ "${SSH_CONNECTION}" = *"27.193.12.228"* ]]; then
        spread 2 3 4
    fi

    echo
    echo "init keyspace ${keyspace} ..."
    cd /opt/matrix/init
    ./init -keyspace=${keyspace} -silent
    exit $?
else
    echo "initkeyspace.sh <keyspacename>"
    exit 1
fi
