#!/bin/sh

set -e

service=cassandra

echo "Start service"
systemctl start ${service}

echo "Check if the service is active"
if ! systemctl is-active -q ${service}; then
    echo "The service is not active"
    exit 1
fi

echo "Wait for service to be ready, be patient..."
sleep 80s

echo "Print service logs"
systemctl status ${service} -n 500

echo "Print cqlsh version"
cqlsh -v

echo "Execute statement 'help' via cqlsh"
cqlsh -e HELP

echo "Check that the status is Up/Normal (UN)"
status=$(nodetool status)
if ! echo "$status" | sed '1,/^-- *Address/ d' | grep -q ^UN; then
    echo "Status is not Up/Normal (UN), see below:"
    echo "--------"; echo "$status"; echo "--------"
    exit 1
fi

echo "Stop service"
systemctl stop ${service}

echo "Check if the service active"
if systemctl is-active -q ${service}; then
    echo "The service is still active"
    exit 1
fi
