#!/usr/bin/env bash
# WP Rocket Cache Rebuild — semua klien
# Trigger cache rebuild dengan curl homepage + wp-cron setiap domain
# Playbook (skill website-health-check + memory):
#   - curl WAJIB -4 (host tanpa IPv6, Cloudflare resolve IPv6-first → stall)
#   - --http2 + browser UA (default curl UA di-stall Cloudflare)
#   - max-time 45 (cold rebuild bisa 15-23s; budget 12-15s terlalu ketat)
#   - studiosoft.id TIDAK pakai -k lagi (cert Let's Encrypt renewed 2026-09-01, valid sd 2026-11-30)

set -u

UA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
LOG="/home/mantapman/.hermes/profiles/atun/workspace/logs/wp_rocket_cache_rebuild.log"

DOMAINS=(
  "studiosoft.id"
  "yasmanatura.co.id"
  "tslaw.id"
  "rsia.kendangsari.com"
  "theskincodeclinic.com"
  "everybeofficial.com"
  "dmark.id"
  "surabaya-skincentre.com"
  "abimataconsultant.com"
  "dasindonesia.com"
)

echo "=== WP Rocket Cache Rebuild — $(date -u '+%Y-%m-%d %H:%M UTC') ===" | tee -a "$LOG"
echo "" | tee -a "$LOG"

for domain in "${DOMAINS[@]}"; do
  echo "Processing: ${domain}"
  # Homepage dengan no-cache trigger
  res_home=$(curl -4 -sL -o /dev/null --http2 -A "$UA" --connect-timeout 5 --max-time 45 -w '%{http_code} %{time_total}s' "https://${domain}/?nocache=BEN_MS")
  home_exit=$?
  # wp-cron trigger
  res_cron=$(curl -4 -sL -o /dev/null --http2 -A "$UA" --connect-timeout 5 --max-time 45 -w '%{http_code} %{time_total}s' "https://${domain}/wp-cron.php?doing_wp_cron")
  cron_exit=$?
  line="  Homepage: ${res_home} (exit=${home_exit}) | wp-cron: ${res_cron} (exit=${cron_exit})"
  echo "$line"
  echo "${domain} | ${line}" >> "$LOG"
  echo ""
done

echo "=== Selesai: $(date -u '+%Y-%m-%d %H:%M UTC') ===" | tee -a "$LOG"
