#!/bin/bash
# =============================================================================
# Script de cutover DNS: feadulta.org → feadulta.com
# Ejecutar DESPUÉS de apuntar el DNS de feadulta.com al servidor de producción
# =============================================================================
#
# Este script reemplaza todas las URLs internas de feadulta.org por feadulta.com
# en la base de datos WordPress de producción.
#
# Servidor: 185.42.105.48
# DB: 278025353wordpress20260112013937 / myfeadultaa5 / KjyGU29h
# =============================================================================

set -e

DB_HOST="127.0.0.1"
DB_NAME="278025353wordpress20260112013937"
DB_USER="myfeadultaa5"
DB_PASS="KjyGU29h"
OLD_URL="http://feadulta.org"
NEW_URL="https://feadulta.com"

MYSQL="mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME"

echo "=== Cutover feadulta.org → feadulta.com ==="
echo "OLD: $OLD_URL"
echo "NEW: $NEW_URL"
echo ""
echo "Ejecutando en 5 segundos... (Ctrl+C para cancelar)"
sleep 5

echo "[1/6] Actualizando siteurl y home..."
$MYSQL -e "
  UPDATE wp_options SET option_value = '$NEW_URL' WHERE option_name = 'siteurl';
  UPDATE wp_options SET option_value = '$NEW_URL' WHERE option_name = 'home';
"

echo "[2/6] Reemplazando en post_content..."
$MYSQL -e "UPDATE wp_posts SET post_content = REPLACE(post_content, '$OLD_URL', '$NEW_URL');"

echo "[3/6] Reemplazando en guid..."
$MYSQL -e "UPDATE wp_posts SET guid = REPLACE(guid, '$OLD_URL', '$NEW_URL');"

echo "[4/6] Reemplazando en postmeta..."
$MYSQL -e "UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, '$OLD_URL', '$NEW_URL');"

echo "[5/6] Reemplazando en wp_options (no serializados)..."
$MYSQL -e "
  UPDATE wp_options SET option_value = REPLACE(option_value, '$OLD_URL', '$NEW_URL')
  WHERE option_name NOT IN ('wpseo', 'fgj2wp_save_posts', 'bsr_data')
    AND option_value LIKE '%feadulta.org%';
"

echo "[6/6] Actualizando wp-config.php..."
ssh feadultada@185.42.105.48 "
  sed -i \"s|define('WP_SITEURL','http://feadulta.org')|define('WP_SITEURL','https://feadulta.com')|\" /web/wp-config.php
  sed -i \"s|define('WP_HOME','http://feadulta.org')|define('WP_HOME','https://feadulta.com')|\" /web/wp-config.php
"

echo ""
echo "=== Verificación ==="
$MYSQL -e "SELECT option_name, option_value FROM wp_options WHERE option_name IN ('siteurl','home');"
$MYSQL -e "SELECT COUNT(*) as pendientes_feadulta_org FROM wp_posts WHERE post_content LIKE '%feadulta.org%';"

echo ""
echo "=== Cutover completado ==="
echo "IMPORTANTE: Limpiar caché de WordPress y Cloudflare después de este paso."
echo "IMPORTANTE: Activar plugins: AdSense, Wordfence, TTS."
echo "IMPORTANTE: Verificar redirects de feadulta.com/images/Musica/ (ya no hacen falta si los MP3 están en el mismo servidor)."
