#!/bin/sh
set -e

echo "=========================================="
echo "泉友米车友会 - 启动中..."
echo "=========================================="

# 替换前端 index.html 中的环境变量占位符
if [ -f "/app/public/index.html" ]; then
    echo "配置高德地图 Key..."
    
    # 使用环境变量替换占位符（直接修改文件）
    AMAP_KEY="${AMAP_KEY:-YOUR_AMAP_KEY_PLACEHOLDER}"
    AMAP_SECURITY_CODE="${AMAP_SECURITY_CODE:-YOUR_AMAP_SECURITY_CODE_PLACEHOLDER}"
    
    # 使用 sed -i 直接修改文件
    sed -i "s|YOUR_AMAP_KEY_PLACEHOLDER|${AMAP_KEY}|g" /app/public/index.html
    sed -i "s|YOUR_AMAP_SECURITY_CODE_PLACEHOLDER|${AMAP_SECURITY_CODE}|g" /app/public/index.html
    
    echo "环境变量注入完成："
    echo "  AMAP_KEY: ${AMAP_KEY}"
    echo "  AMAP_SECURITY_CODE: ${AMAP_SECURITY_CODE}"
    
    if [ "$AMAP_KEY" != "YOUR_AMAP_KEY_PLACEHOLDER" ] && [ -n "$AMAP_KEY" ]; then
        echo "✓ 高德地图 Key 已配置"
    else
        echo "⚠ 警告: 高德地图 Key 未配置，地图功能将不可用"
        echo "  请在 docker-compose.yml 中设置 AMAP_KEY 环境变量"
    fi
fi

echo "=========================================="
echo "启动应用服务..."
echo "=========================================="

# 使用 dumb-init 启动应用
exec dumb-init "$@"
