#!/bin/bash

# 初始化目录脚本

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$TEST_DIR"

# 加载 .env 文件（如果存在）
if [ -f .env ]; then
    echo "加载 .env 配置文件..."
    source .env
fi

# 默认配置
ARGUS_BUILD_UID=${ARGUS_BUILD_UID:-2133}
ARGUS_BUILD_GID=${ARGUS_BUILD_GID:-2015}
DATA_ROOT=${DATA_ROOT:-/private}

echo "开始初始化目录结构..."
echo "数据根目录: ${DATA_ROOT}"
echo "统一 UID: ${ARGUS_BUILD_UID}"
echo "统一 GID: ${ARGUS_BUILD_GID}"

# 创建基础目录结构
echo "创建基础目录结构..."
sudo mkdir -p ${DATA_ROOT}/argus/metric
sudo mkdir -p ${DATA_ROOT}/argus/etc
sudo mkdir -p ${DATA_ROOT}/argus/agent

# 创建 FTP 目录
echo "创建 FTP 目录..."
sudo mkdir -p ${DATA_ROOT}/argus/metric/ftp/share

# 创建 Prometheus 目录
echo "创建 Prometheus 目录..."
sudo mkdir -p ${DATA_ROOT}/argus/metric/prometheus/{data,rules,targets}

# 创建 Grafana 目录
echo "创建 Grafana 目录..."
sudo mkdir -p ${DATA_ROOT}/argus/metric/grafana/{data,logs,plugins,provisioning/datasources,provisioning/dashboards,data/sessions,data/dashboards,config}

# 统一设置所有目录权限
echo "设置目录权限..."
sudo chown -R ${ARGUS_BUILD_UID}:${ARGUS_BUILD_GID} ${DATA_ROOT}/argus/metric
sudo chmod -R 755 ${DATA_ROOT}/argus/metric

echo "目录初始化完成！"
echo ""
echo "目录结构："
echo "  ${DATA_ROOT}/"
echo "  ├── argus/                 (UID:${ARGUS_BUILD_UID}, GID:${ARGUS_BUILD_GID})"
echo "  │   ├── metric/"
echo "  │   │   ├── ftp/"
echo "  │   │   ├── prometheus/"
echo "  │   │   └── grafana/"
echo ""
echo "您现在可以运行 'docker-compose up -d' 来启动所有服务"

