#!/bin/bash
#
# read required env variables
BASEDIR="$PWD"
source $BASEDIR/.env/ssdt-document-store-shared.properties
source $BASEDIR/.env/ssdt-document-store.properties

# define entityId based environment variables
ES_SETTING_CLUSTER_NAME=${entityId}-document-store
ES_SETTING_NODE_NAME=${entityId}-ds-01
DOCUMENTSTORE_ELASTIC_HOST=${entityId}-document
ELASTIC_NETWORK=${entityId}-document

# create backup directory if it does not exist
BACKUP_DIR="$BASEDIR/backup"
if [ ! -d "$BACKUP_DIR" ]
then
  mkdir "$BACKUP_DIR"
fi

# create docker network if it does not exist
if [ -z $(docker network ls --filter name=^${ELASTIC_NETWORK}$ --format="{{ .Name }}") ]
then
     docker network create "${ELASTIC_NETWORK}";
fi

# docker login
echo "Attempting to login to $artifactUrl"
docker login $artifactUrl -u $artifactUser -p $artifactPassword
# docker pull image
echo "Attempting to pull $artifactUrl/document-store:$DS_VERSION"
docker pull $artifactUrl/document-store:$DS_VERSION

# build docker run command based on environment variables provided
DOCKER_RUN="docker run --name=$ES_SETTING_NODE_NAME --restart=unless-stopped --log-driver local \
  --env-file $BASEDIR/.env/ssdt-document-store.properties \
  -e ELASTIC_PASSWORD=$ELASTIC_PASSWORD \
  --network=$ELASTIC_NETWORK \
  --ulimit memlock=-1:-1 \
  --ulimit nofile=65535:65535 \
  --group-add 0 \
  --mount type=bind,source=$BACKUP_DIR,destination=/usr/share/elasticsearch/backup/ \
  --mount type=volume,source=$entityId-document-data,destination=/usr/share/elasticsearch/data/ \
  --mount type=volume,source=$entityId-document-config,destination=/usr/share/elasticsearch/config/ \
  -p $DOCUMENTSTORE_ELASTIC_PORT:9200 \
  -p $DOCUMENTSTORE_TRANSPORT_PORT:9300 \
  "
if [ -d "$BASEDIR/jvm.options.d" ]; then
  DOCKER_RUN="$DOCKER_RUN --mount type=bind,source=$BASEDIR/jvm.options.d,destination=/usr/share/elasticsearch/config/jvm.options.d/ "
fi

#echo "Executing docker run command $DOCKER_RUN"
DOCKER_RUN="$DOCKER_RUN -d docker-images.ssdt-ohio.org/document-store:${DS_VERSION:-latest}"
$DOCKER_RUN

#wait for instance to start
attempt=0
while [ $attempt -le 15 ]; do
    attempt=$(( attempt + 1 ))
    echo "Waiting for server to be up (attempt: $attempt)..."
    result=$(docker inspect --format='{{.State.Health.Status}}' "$ES_SETTING_NODE_NAME")
    if [ "$result" = "healthy" ] ; then
      echo "Elasticsearch is up, preparing to copy certificates and generated properties"
      break
    fi
    sleep 10
done

# copy elasticsearch certificate to basedir for further use if necessary
docker cp "$ES_SETTING_NODE_NAME":/usr/share/elasticsearch/config/certs/http_ca.crt "$BASEDIR"

# copy generated properites i.e. API keys etc to basedir for use in product integration
attempt=0
generated_contents=$(docker exec -it "$ES_SETTING_NODE_NAME" sed -rn 's/^DOCUMENTSTORE_ELASTIC_FINGERPRINT=([^\n]+)$/\1/p' /usr/share/elasticsearch/config/ssdt-document-store-generated.properties)
while [ "$generated_contents" == "" ] && [ $attempt -le 15 ]; do
  attempt=$(( attempt + 1 ))
  echo "Waiting for generated properties to be created (attempt: $attempt)..."
  sleep 10
  generated_contents=$(docker exec -it "$ES_SETTING_NODE_NAME" sed -rn 's/^DOCUMENTSTORE_ELASTIC_FINGERPRINT=([^\n]+)$/\1/p' /usr/share/elasticsearch/config/ssdt-document-store-generated.properties)
done
docker cp "$ES_SETTING_NODE_NAME":/usr/share/elasticsearch/config/ssdt-document-store-generated.properties "$BASEDIR"/.env


echo "Elasticsearch is up and running, certificate & generated properties copied"
