#!/bin/bash
# 啟用維護模式腳本

echo "=========================================="
echo "啟用維護模式"
echo "=========================================="

# 備份當前 Nginx 配置
echo "1. 備份 Nginx 配置..."
sudo cp /etc/nginx/sites-available/momo /etc/nginx/sites-available/momo.backup

# 創建維護模式配置
echo "2. 創建維護模式配置..."
sudo tee /etc/nginx/sites-available/momo-maintenance > /dev/null << 'EOF'
server {
    listen 443 ssl;
    server_name mo.wooo.work;

    ssl_certificate /etc/letsencrypt/live/mo.wooo.work/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mo.wooo.work/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    root /home/ogt/momo_pro_system;

    location / {
        try_files /maintenance.html =503;
    }
}

server {
    listen 80;
    server_name mo.wooo.work;
    return 301 https://$host$request_uri;
}
EOF

# 切換到維護模式
echo "3. 切換到維護模式..."
sudo ln -sf /etc/nginx/sites-available/momo-maintenance /etc/nginx/sites-enabled/momo

# 測試並重載 Nginx
echo "4. 重載 Nginx..."
sudo nginx -t && sudo systemctl reload nginx

echo ""
echo "=========================================="
echo "✅ 維護模式已啟用"
echo "=========================================="
echo "網址：https://mo.wooo.work"
echo "用戶將看到「系統維護中」頁面"
echo ""
echo "恢復正常服務："
echo "  ./disable_maintenance.sh"
echo "=========================================="
