#!/bin/bash
# Copyright Security Onion Solutions LLC and/or licensed to Security Onion Solutions LLC under one
# or more contributor license agreements. Licensed under the Elastic License 2.0 as shown at
# https://securityonion.net/license; you may not use this file except in compliance with the
# Elastic License 2.0.

. /usr/sbin/so-common


RETURN_CODE=0
ELASTICSEARCH_HOST=$1
ELASTICSEARCH_PORT=9200

# Define a default directory to load pipelines from
ELASTICSEARCH_INGEST_PIPELINES="/opt/so/conf/elasticsearch/ingest/"

# Wait for ElasticSearch to initialize

if [ ! -f /opt/so/state/espipelines.txt ]; then

  echo -n "Waiting for ElasticSearch..."
  COUNT=0
  ELASTICSEARCH_CONNECTED="no"
  while [[ "$COUNT" -le 240 ]]; do
      curl -K /opt/so/conf/elasticsearch/curl.config -k --output /dev/null --silent --head --fail -L https://"$ELASTICSEARCH_HOST":"$ELASTICSEARCH_PORT"
	  if [ $? -eq 0 ]; then
	  	ELASTICSEARCH_CONNECTED="yes"
		echo "connected!"
		break
	  else
		((COUNT+=1))
		sleep 1
		echo -n "."
	  fi
    done
  if [ "$ELASTICSEARCH_CONNECTED" == "no" ]; then
	echo
	echo -e "Connection attempt timed out.  Unable to connect to ElasticSearch.  \nPlease try: \n  -checking log(s) in /var/log/elasticsearch/\n  -running 'sudo docker ps' \n  -running 'sudo so-elastic-restart'"
	echo
  fi

  cd ${ELASTICSEARCH_INGEST_PIPELINES}

  echo "Loading pipelines..."
  for i in .[a-z]* *; do echo $i; RESPONSE=$(curl -K /opt/so/conf/elasticsearch/curl.config -k -XPUT -L https://${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}/_ingest/pipeline/$i -H 'Content-Type: application/json' -d@$i 2>/dev/null); echo $RESPONSE; if [[ "$RESPONSE" == *"error"* ]]; then RETURN_CODE=1; fi; done
  echo

  cd - >/dev/null
  if [[ "$RETURN_CODE" != "1" ]]; then
    touch /opt/so/state/espipelines.txt
  fi  
else 
  exit $RETURN_CODE
fi
