#!/bin/bash

###############################################################################
# 捐款证书中文乱码修复脚本
# 用途：在运行中的Docker容器内安装中文字体
# 适用于：无法重新构建镜像的紧急情况
###############################################################################

set -e  # 遇到错误立即退出

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 容器名称（可根据实际情况修改）
CONTAINER_NAME="websoft-api-container"

echo -e "${GREEN}================================${NC}"
echo -e "${GREEN}中文字体修复脚本${NC}"
echo -e "${GREEN}================================${NC}"
echo ""

# 检查容器是否存在
echo -e "${YELLOW}步骤 1/6: 检查容器状态...${NC}"
if ! docker ps | grep -q "$CONTAINER_NAME"; then
    echo -e "${RED}错误：容器 $CONTAINER_NAME 未运行！${NC}"
    echo "当前运行的容器："
    docker ps --format "table {{.Names}}\t{{.Status}}"
    echo ""
    read -p "请输入正确的容器名称: " CONTAINER_NAME
    if [ -z "$CONTAINER_NAME" ]; then
        echo -e "${RED}容器名称不能为空，退出。${NC}"
        exit 1
    fi
fi
echo -e "${GREEN}✓ 容器正在运行${NC}"
echo ""

# 检查是否已安装字体
echo -e "${YELLOW}步骤 2/6: 检查是否已安装中文字体...${NC}"
if docker exec "$CONTAINER_NAME" fc-list :lang=zh 2>/dev/null | grep -q "WenQuanYi"; then
    echo -e "${GREEN}✓ 中文字体已安装${NC}"
    docker exec "$CONTAINER_NAME" fc-list :lang=zh
    echo ""
    read -p "是否重新安装？(y/N): " REINSTALL
    if [[ ! "$REINSTALL" =~ ^[Yy]$ ]]; then
        echo "跳过安装，退出。"
        exit 0
    fi
else
    echo -e "${YELLOW}未检测到中文字体，开始安装...${NC}"
fi
echo ""

# 安装字体工具
echo -e "${YELLOW}步骤 3/6: 安装字体工具...${NC}"
docker exec -u root "$CONTAINER_NAME" sh -c "apk add --no-cache fontconfig ttf-dejavu wget" || {
    echo -e "${RED}✗ 字体工具安装失败${NC}"
    exit 1
}
echo -e "${GREEN}✓ 字体工具安装成功${NC}"
echo ""

# 下载中文字体
echo -e "${YELLOW}步骤 4/6: 下载文泉驿微米黑字体...${NC}"
echo "正在从GitHub下载（约10MB），请稍候..."
docker exec -u root "$CONTAINER_NAME" sh -c "
    wget -O /tmp/wqy-microhei.ttc https://github.com/anthonyfok/fonts-wqy-microhei/raw/master/wqy-microhei.ttc 2>&1 | grep -E 'Connecting|Length|saved' || true
" || {
    echo -e "${YELLOW}GitHub下载失败，尝试使用代理...${NC}"
    docker exec -u root "$CONTAINER_NAME" sh -c "
        wget -O /tmp/wqy-microhei.ttc https://ghproxy.com/https://github.com/anthonyfok/fonts-wqy-microhei/raw/master/wqy-microhei.ttc
    " || {
        echo -e "${RED}✗ 字体下载失败${NC}"
        echo "请检查网络连接或手动下载字体文件。"
        exit 1
    }
}
echo -e "${GREEN}✓ 字体下载成功${NC}"
echo ""

# 安装字体
echo -e "${YELLOW}步骤 5/6: 安装字体文件...${NC}"
docker exec -u root "$CONTAINER_NAME" sh -c "
    mkdir -p /usr/share/fonts/truetype/wqy && \
    mv /tmp/wqy-microhei.ttc /usr/share/fonts/truetype/wqy/ && \
    fc-cache -fv
" || {
    echo -e "${RED}✗ 字体安装失败${NC}"
    exit 1
}
echo -e "${GREEN}✓ 字体安装成功${NC}"
echo ""

# 验证安装
echo -e "${YELLOW}步骤 6/6: 验证字体安装...${NC}"
FONT_COUNT=$(docker exec "$CONTAINER_NAME" fc-list :lang=zh | wc -l)
if [ "$FONT_COUNT" -gt 0 ]; then
    echo -e "${GREEN}✓ 中文字体验证成功！${NC}"
    echo "已安装的中文字体："
    docker exec "$CONTAINER_NAME" fc-list :lang=zh
else
    echo -e "${RED}✗ 字体验证失败${NC}"
    exit 1
fi
echo ""

# 完成提示
echo -e "${GREEN}================================${NC}"
echo -e "${GREEN}修复完成！${NC}"
echo -e "${GREEN}================================${NC}"
echo ""
echo "后续步骤："
echo "1. 不需要重启容器，字体已生效"
echo "2. 重新生成捐款证书即可看到效果"
echo "3. 如果仍有问题，请查看容器日志："
echo "   docker logs -f $CONTAINER_NAME"
echo ""
echo -e "${YELLOW}注意：${NC}"
echo "- 此修复方法在容器重启后会失效"
echo "- 建议后续使用更新后的Dockerfile重新构建镜像"
echo "- 详细文档请参考：docs/chinese-font-fix-guide.md"
echo ""
