#!/bin/bash
# 一键恢复站点备份并启动 hugo server（Windows 侧运行: 双击或用 WSL 执行）
# 在 Windows 上通过 WSL 运行:
#   MSYS_NO_PATHCONV=1 wsl.exe bash /mnt/c/Users/helltractor/restore_site.sh
set -e
BACKUP="/mnt/c/Users/helltractor/helltractor_site_backup_20260810.tar.gz"
SITE=/opt/helltractor

echo "=== 1. 检查备份文件 ==="
[ -f "$BACKUP" ] || { echo "找不到备份: $BACKUP"; exit 1; }

echo "=== 2. 站点已存在则移走旧版(留底) ==="
if [ -d "$SITE" ]; then
  OLD="${SITE}_pre_restore_$(date +%s)"
  echo "移走现有站点 -> $OLD"
  mv "$SITE" "$OLD"
fi

echo "=== 3. 解压备份 ==="
cd /opt
tar xzf "$BACKUP"

echo "=== 4. 启动 hugo server ==="
pkill -f "hugo server" 2>/dev/null || true
sleep 1
cd "$SITE"
nohup hugo server --bind 0.0.0.0 --port 1313 --disableFastRender > /tmp/hugo.log 2>&1 &
sleep 3
echo "已启动。访问 http://localhost:1313 预览"
tail -5 /tmp/hugo.log
