#!/bin/bash
set -euo pipefail

echo "[INFO] Starting Kibana under supervisor..."

mkdir -p /private/argus/log/kibana

# 创建软链接到Kibana预期的数据目录
if [ -L /usr/share/kibana/data ]; then
    rm /usr/share/kibana/data
elif [ -d /usr/share/kibana/data ]; then
    rm -rf /usr/share/kibana/data
fi

ln -sf /private/argus/log/kibana /usr/share/kibana/data

echo "[INFO] Data directory linked: /usr/share/kibana/data -> /private/argus/log/kibana"

# 记录容器ip地址
DOMAIN=kibana.log.argus.com
IP=`ifconfig | grep -A 1 eth0 | grep inet | awk '{print $2}'`
echo current IP: ${IP}
echo ${IP} > /private/argus/etc/${DOMAIN}

# 设置环境变量
export ELASTICSEARCH_HOSTS="${ELASTICSEARCH_HOSTS:-"http://es:9200"}"

echo "[INFO] Connecting to Elasticsearch at: $ELASTICSEARCH_HOSTS"

# 启动后台配置任务
echo "[INFO] Starting background post-start configuration..."
/usr/local/bin/kibana-post-start.sh &

echo "[INFO] Starting Kibana process..."

# 启动原始的Kibana entrypoint
exec /usr/local/bin/kibana-docker
