#!/usr/bin/with-contenv bash
# Wazuh App Copyright (C) 2020 Wazuh Inc. (License GPLv2)

set -e

WAZUH_FILEBEAT_MODULE=wazuh-filebeat-0.1.tar.gz

if [ "$ELASTICSEARCH_URL" != "" ]; then
  >&2 echo "Customize Elasticsearch ouput IP"
  sed -i 's|http://elasticsearch:9200|'$ELASTICSEARCH_URL'|g' /etc/filebeat/filebeat.yml
fi

# Configure filebeat.yml security settings

if [ "$SSL_CERTIFICATE_AUTHORITIES" != "" ]; then
  >&2 echo "Configuring Certificate Authorities."
  sed -i 's|#ssl.certificate_authorities:|'ssl.certificate_authorities:\ [\"$SSL_CERTIFICATE_AUTHORITIES\"]'|g' /etc/filebeat/filebeat.yml
fi

if [ "$USERNAME" != "" ]; then
  >&2 echo "Configuring username."
  sed -i 's|#username:|'username:\ \"$USERNAME\"'|g' /etc/filebeat/filebeat.yml
fi

if [ "$PASSWORD" != "" ]; then
  >&2 echo "Configuring password."
  sed -i 's|#password:|'password:\ \"$PASSWORD\"'|g' /etc/filebeat/filebeat.yml
fi

chmod go-w /etc/filebeat/filebeat.yml || true
chown root: /etc/filebeat/filebeat.yml || true
