#!/bin/bash

################################################################################
#
# This script stops the local container for ElasticSearch if it's running.
# Cleans up the log file as well.
#
################################################################################

###
# if script dir is not specified, set it to cuurent dir
if [ $# == 1 ]
then
  SCRIPT_DIR="$1"
else
  SCRIPT_DIR="."
fi

###
# Fail fast
set -eau pipefail

###
# Set common vars
. ${SCRIPT_DIR}/es_container_vars.sh

echo
echo "==================== Cleaning up ElasticSearch docker container"

###
# It's useful to display all documents present in ElasticSearch before shutting down the docker image
# Uncomment it to help debugging
# curl -X GET 'http://localhost:9200/metric_runs/_search?size=100&q=*:*' || true
# A sleep allows time to fire postman requests at ElasticSearch
# sleep 60

###
# Cleanup : stop and remove the container, remove the log file
echo "----- Stopping container ${ES_CONTAINER_NAME}"
docker stop ${ES_CONTAINER_NAME} || true
echo "----- Removing container ${ES_CONTAINER_NAME}"
docker container rm ${ES_CONTAINER_NAME} || true
echo "----- Removing log file ${ES_LOG_FILE}"
rm -rf ${ES_LOG_FILE} || true
echo "==================== DONE Cleaning up ElasticSearch docker container"
echo


