#!/bin/bash
# WSL 端：打包项目并临时提供 HTTP 下载
# 运行方式：bash scripts/pack_and_serve.sh

set -e

PROJECT_DIR="/home/rwu/WSLprojects/AICODE-2026"
CLAUDE_GLOBAL_DIR="/home/rwu/.claude"
PACK_DIR="/tmp/aicode_transfer"
ARCHIVE_NAME="aicode2026_transfer.tar.gz"
PORT=8899

echo "=== 准备打包 ==="
rm -rf "$PACK_DIR"
mkdir -p "$PACK_DIR"

echo "→ 打包项目文件..."
tar -czf "$PACK_DIR/project.tar.gz" \
  --exclude=".git" \
  --exclude="__pycache__" \
  --exclude="*.pyc" \
  -C "$(dirname "$PROJECT_DIR")" \
  "$(basename "$PROJECT_DIR")"

echo "→ 打包全局 Claude 配置..."
tar -czf "$PACK_DIR/claude_global.tar.gz" \
  --exclude=".git" \
  -C "$(dirname "$CLAUDE_GLOBAL_DIR")" \
  "$(basename "$CLAUDE_GLOBAL_DIR")"

echo "→ 合并为一个压缩包..."
tar -czf "/tmp/$ARCHIVE_NAME" -C "$PACK_DIR" .

FILESIZE=$(du -sh "/tmp/$ARCHIVE_NAME" | cut -f1)
LAN_IP=$(hostname -I | awk '{print $2}')

echo ""
echo "=== 打包完成 ==="
echo "文件大小：$FILESIZE"
echo ""
echo "=== 启动 HTTP 服务 ==="
echo "在 Mac Mini 上执行以下命令："
echo ""
echo "  bash <(curl -s http://$LAN_IP:$PORT/setup_on_mac.sh)"
echo ""
echo "或者分两步："
echo "  curl -O http://$LAN_IP:$PORT/$ARCHIVE_NAME"
echo "  curl -O http://$LAN_IP:$PORT/setup_on_mac.sh && bash setup_on_mac.sh"
echo ""
echo "按 Ctrl+C 停止服务"
echo "========================"

# 把 setup 脚本也放进服务目录
cp "$(dirname "$0")/setup_on_mac.sh" /tmp/
cp "/tmp/$ARCHIVE_NAME" /tmp/

cd /tmp
python3 -m http.server $PORT
