#!/bin/bash
# panel-site-pause — take a site offline for a while without deleting anything.
# Visitors and crawlers get a short "paused" page (HTTP 503, "try again later"), so the site uses no PHP or database
# time; its cron jobs stop too. Files, database, SSL and DNS stay exactly as they are.
#   panel-site-pause on <domain>     pause
#   panel-site-pause off <domain>    resume
#   panel-site-pause list            paused domains on this server, one per line
#   panel-site-pause on-server <server> on|off|list [<domain>]   the same on a connected server
set -euo pipefail
. /opt/panel/bin/panel-lib.sh
EXTRA=/etc/panel/vhost-extra; install -d -m 755 "$EXTRA"
TAG='# panel-site-pause'
act="${1:-}"
if [ "$act" = on-server ]; then
  srv="${2:-}"; ip=$(node_ip "$srv") || die "unknown server: $srv"; shift 2
  [[ "${1:-}" =~ ^(on|off|list)$ ]] || die "not allowed there: ${1:-}"
  exec timeout 60 ssh "${NODE_SSH_OPTS[@]}" "root@$ip" "/opt/panel/bin/panel-site-pause $(printf '%q ' "$@")"
fi
if [ "$act" = list ]; then grep -ls "^$TAG" "$EXTRA"/*.conf 2>/dev/null | xargs -r -n1 basename | sed 's/\.conf$//' || true; exit 0; fi
d="${2:-}"; valid_domain "$d" || die "invalid domain: $d"
[ -f "$(site_conf "$d")" ] || die "no site $d on this server"
f="$EXTRA/$d.conf"; u=$(site_user "$d"); root=$(site_docroot "$d")
grep -q "IncludeOptional $EXTRA/$d.conf" "$(site_conf "$d")" || /opt/panel/bin/panel-site-add "$d" "$root" "$u" >/dev/null
cron_lines() { grep -F -e "$root/" -e "$root " -e "$root;" -e "$root'" -e "$root\"" -e "panel-cron-run $d "; }
case "$act" in
  on)
    if [ -f "$f" ] && grep -q "^$TAG" "$f"; then echo "$d is already paused"; exit 0; fi
    [ ! -f "$f" ] || ! grep -q '^# panel-move-site' "$f" || die "$d is being moved or forwarded to another server right now — pause it there"
    if [ -f "$f" ]; then cp -p "$f" "$f.prepause"; else : > "$f.prepause"; fi
    cat > "$f" <<M
$TAG: paused in the panel on $(date -u '+%F %H:%M') UTC
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/\.well-known/
RewriteRule ^ - [R=503,L]
ErrorDocument 503 "<!doctype html><meta charset=utf-8><meta name=viewport content='width=device-width'><meta name=robots content=noindex><title>Paused</title><body style='font:17px system-ui,sans-serif;text-align:center;padding:14vh 20px;color:#333'><h1>This site is paused</h1><p>It will be back. Please try again later.</p>"
Header always set Retry-After "3600"
M
    if ! apachectl -t >/dev/null 2>&1; then mv -f "$f.prepause" "$f"; [ -s "$f" ] || rm -f "$f"; die "web server did not accept the change — nothing was paused"; fi
    systemctl reload httpd
    # its cron jobs wait too
    if [ -n "$u" ]; then
      cur=$(crontab -u "$u" -l 2>/dev/null || true)
      mine=$(grep -v '^[[:space:]]*#' <<<"$cur" | cron_lines || true)
      [ -z "$mine" ] || CRON="$mine" python3 -c '
import os, sys
mine = set(l for l in os.environ["CRON"].split("\n") if l)
for l in sys.stdin.read().splitlines(): print("#paused-site " + l if l in mine else l)' <<<"$cur" | crontab -u "$u" - >/dev/null 2>&1
    fi
    log "$d paused"; echo "$d is paused. Visitors see a short \"paused\" page; its cron jobs are stopped." ;;
  off)
    [ -f "$f" ] && grep -q "^$TAG" "$f" || { echo "$d is not paused"; exit 0; }
    if [ -s "$f.prepause" ]; then mv -f "$f.prepause" "$f"; else rm -f "$f" "$f.prepause"; fi
    apachectl -t >/dev/null 2>&1 && systemctl reload httpd
    if [ -n "$u" ]; then
      cur=$(crontab -u "$u" -l 2>/dev/null || true)
      if grep '^#paused-site ' <<<"$cur" | cron_lines >/dev/null; then
        awk -v r="$root" -v dd="panel-cron-run $d " '{ if (index($0, "#paused-site ") == 1 && (index($0, r) || index($0, dd))) print substr($0, 14); else print }' <<<"$cur" | crontab -u "$u" - >/dev/null 2>&1
      fi
    fi
    log "$d resumed"; echo "$d is running again." ;;
  *) sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//'; exit 1 ;;
esac
