#!/bin/bash
# ═══════════════════════════════════════════════════
# 铸渊图片工作室 · 部署脚本
# ═══════════════════════════════════════════════════
# 用法: bash deploy/setup.sh
# 在目标服务器上运行

set -e

echo "🎨 铸渊图片工作室 · 部署开始"

# 1. 安装系统依赖
echo "📦 安装系统依赖..."
apt-get update -qq
apt-get install -y -qq \
  ca-certificates fonts-liberation libappindicator3-1 \
  libasound2 libatk-bridge2.0-0 libatk1.0-0 libcups2 \
  libdbus-1-3 libgdk-pixbuf2.0-0 libnspr4 libnss3 \
  libx11-xcb1 libxcomposite1 libxdamage1 libxrandr2 \
  xdg-utils libgbm1 libxkbcommon0 > /dev/null 2>&1

echo "   ✅ 系统依赖安装完成"

# 2. 安装 Node 依赖
echo "📦 安装 Node 依赖..."
cd "$(dirname "$0")/.."
npm install --production 2>&1 | tail -1

# 3. 创建输出目录
mkdir -p output

# 4. 启动服务
echo "🚀 启动图片工作室服务..."
pm2 delete zhuyuan-image-studio 2>/dev/null || true
pm2 start server.js --name zhuyuan-image-studio -- --port 3912
pm2 save

echo ""
echo "✅ 部署完成！"
echo "   服务端口: 3912"
echo "   预览地址: http://<服务器IP>:3912"
echo ""
echo "   Nginx 配置参考："
echo "   location /images {"
echo "       proxy_pass http://127.0.0.1:3912;"
echo "       proxy_set_header Host \$host;"
echo "       proxy_set_header X-Real-IP \$remote_addr;"
echo "   }"
