#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

# test that we can insert and retrieve a simple record

# start zookeeper on localhost:2183
source setup_env.inc

#zookeeper_server_ports="localhost:2188,localhost:2189,localhost:2190"
zookeeper_server_ports="localhost:2188"
# default datadir integration_test/var/work/zookeeper/data/1
# start the zookeeper cluster
$SCRIPT_DIR/cm_driver.py -c zookeeper -o start --zookeeper_reset --zookeeper_server_ports=${zookeeper_server_ports}  --cmdline_props="tickTime=2000;initLimit=5;syncLimit=2" 

# create storage cluster
#./clm_console.sh -zkSvr localhost:2188 -addCluster storage-cluster-12345
$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addCluster storage-cluster-12345"

#./clm_console.sh -zkSvr localhost:2188 -addCluster relay-cluster-12345
$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addCluster relay-cluster-12345"

#./clm_console.sh -zkSvr localhost:2188 -addResourceGroup storage-cluster-12345 db-12345 120
$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addResourceGroup storage-cluster-12345 db-12345 120"

#./clm_console.sh -zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8900
$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8900"

#./clm_console.sh -zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8901
$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8901"

#./clm_console.sh -zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8902
$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8902"

#./clm_console.sh -zkSvr localhost:2188 -rebalance storage-cluster-12345 db-12345 2
$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -rebalance storage-cluster-12345 db-12345 2"

#Launch mock storage nodes
$SCRIPT_DIR/cm_driver.py -c mock-storage -o start -l $VIEW_ROOT/integration-test/config/log4j-info.properties --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345 -host localhost -port 8900 -relayCluster relay-cluster-12345" 

$SCRIPT_DIR/cm_driver.py -c mock-storage -o start -l $VIEW_ROOT/integration-test/config/log4j-info.properties --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345 -host localhost -port 8901 -relayCluster relay-cluster-12345" 

$SCRIPT_DIR/cm_driver.py -c mock-storage -o start -l $VIEW_ROOT/integration-test/config/log4j-info.properties --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345 -host localhost -port 8902 -relayCluster relay-cluster-12345" 
#Launch the cluster manager
sleep 2

$SCRIPT_DIR/cm_driver.py -c cluster-manager -o start -l $VIEW_ROOT/integration-test/config/log4j-info.properties --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345"

exit 0
sleep 6

$SCRIPT_DIR/cm_driver.py -c cluster-manager -o stop

#verify cluster state
$SCRIPT_DIR/cm_driver.py -c cluster-state-verifier -o start --logfile=$verifier_1_output -l "integration-test/config/log4j2.properties" --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345"

$SCRIPT_DIR/cm_driver.py -c mock-storage -o stop

echo == GREP SUCCEED ==
#find ~/EspressoLogs/ -mmin -10 -print | xargs grep succeed
grep Successful $verifier_1_output

source report_pass_fail.inc

exit $all_stat


# do not stop for now so that people can view the state
#$SCRIPT_DIR/cm_driver.py -c zookeeper -o start
#
