#!/bin/bash
# frozen_string_literal: true
#
# 制作docker安装包脚本 - 打包已构建的镜像
#

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
OUTPUT_DIR="${SCRIPT_DIR}/release/qingxun_docker"
IMAGE_NAME="qingxun-client"
mkdir -p "$OUTPUT_DIR/data"
mkdir -p "$OUTPUT_DIR/log"
mkdir -p "$OUTPUT_DIR/db"
echo "=== Qingxun Client 安装包制作脚本 ==="
echo ""

# 2. 拷贝源码
echo "2. 拷贝源码..."
rsync -av --exclude='.git' \
        --exclude='Dockerfile' \
        --exclude='docker-compose*.yml' \
        --exclude='.dockerignore' \
        --exclude='deploy.sh' \
        --exclude='package.sh' \
        --exclude='release' \
        --exclude='release_windows' \
        --exclude='data' \
        --exclude='*.tar.gz' \
        --exclude='tmp' \
        --exclude='log' \
        --exclude='logs' \
        --exclude='gems' \
        --exclude='vendor' \
        --exclude='Gemfile*' \
        --exclude='.claude' \
        --exclude='.gitignore' \
        --exclude='.ruby-lsp' \
        --exclude='.sisyphus' \
        --exclude='.vscode' \
        --exclude='docs' \
        --exclude='spec' \
        --exclude='*.md' \
        --exclude='*deploy_app.sh' \
        --exclude='.bundle' \
        "$SCRIPT_DIR/" "$OUTPUT_DIR/data/app/"

# 手动复制 public/vendor（排除 vendor 会排除所有 vendor 目录）
if [ -d "$SCRIPT_DIR/public/vendor" ]; then
    echo "   复制 public/vendor 目录..."
    rsync -av "$SCRIPT_DIR/public/vendor/" "$OUTPUT_DIR/data/app/public/vendor/"
fi
rsync -av "$SCRIPT_DIR/vendor/bundle/" "$OUTPUT_DIR/data/"
echo "   ✓ 源码已拷贝到 data/app/"

# 2.5 清空 settings.yml 中的敏感信息
echo "2.5 清空配置中的敏感信息..."
SETTINGS_FILE="$OUTPUT_DIR/data/app/config/settings.yml"
if [ -f "$SETTINGS_FILE" ]; then
    sed -i 's/:api_token: .*/:api_token: ""/' "$SETTINGS_FILE"
    sed -i 's/:access_token: .*/:access_token: ""/' "$SETTINGS_FILE"
    sed -i 's/:access_token_expires_at: .*/:access_token_expires_at: ""/' "$SETTINGS_FILE"
    echo "   ✓ 已清空 api_token 和 access_token"
fi

# 3. 创建使用说明
echo "3. 创建使用说明..."
cat > "$OUTPUT_DIR/README.txt" << 'EOF'
清巡 Client 安装包
====================

目录结构:
  .
  ├── qingxun-client.tar.gz   # Docker 镜像
  ├── install.sh              # 部署脚本
  ├── docker-compose.yml     # Docker 配置
  └── data/                  # 持久化目录
      ├── app/               # 源码
      ├── db/                # 数据库
      └── log/               # 日志

快速部署:
  1. 解压（如需要）:
     tar -xzf qingxun-client-package.tar.gz

  2. 导入镜像:
     docker load < qingxun-client.tar.gz

  3. 安装:
     ./install.sh

  4. 访问:
     http://localhost:4567

如需更新需要重启:
  docker-compose restart

如需修改端口:
  HOST_PORT=8080 docker-compose up -d
EOF

# 4. 打包整个发布包
echo "4. 打包安装包..."
PACKAGE_FULL_NAME="qingxun-client-$(date +%Y%m%d-%H%M%S).tar.gz"
PACKAGE_PATH="${SCRIPT_DIR}/release/${PACKAGE_FULL_NAME}"
cd "${SCRIPT_DIR}/release"
tar -czf "${PACKAGE_PATH}" "$(basename "${OUTPUT_DIR}")"
echo "   ✓ 安装包: ${PACKAGE_PATH}"

# 5. 替换 public/download_client/qingxun_client_linux.tar.gz
echo "5. 更新下载链接文件..."
TARGET_FILE="/home/zb/net_cat/public/download_client/qingxun_client_linux.tar.gz"
mkdir -p "$(dirname "${TARGET_FILE}")"
cp -f "${PACKAGE_PATH}" "${TARGET_FILE}"
echo "   ✓ 已更新: ${TARGET_FILE}"

# 6. 统计大小
echo ""
echo "=== 打包完成 ==="
echo "安装包: $(du -h "${PACKAGE_PATH}" | cut -f1)"
echo ""
echo "安装包内容:"
ls -la "${OUTPUT_DIR}/"
cd ..