#!/usr/bin/env bash
#
# sync-prod-to-dev.sh
#
# 1. Dump the Production WordPress DB using mysqldump
# 2. Transfer the dump file to Dev (optional step if needed)
# 3. Import the dump into Dev using mysql
# 4. Use WP-CLI on Dev to run search-replace for the domain
#
# Assumes:
#   - You have MySQL credentials for both Prod and Dev
#   - WP-CLI is installed on Dev
#   - The Dev WordPress site is configured (wp-config.php) in the DEV_WP_PATH
#   - SSH is set up between local/Dev and Prod (if they are separate servers)
#
# Note: For dev environment, typical domain is "http://localhost:8080" or "http://dev.example.com".
#
# IMPORTANT: This will overwrite your Dev database each time it's run.
####################################
# COPY THEMES AND PLUGINS
####################################
echo "=== Copying Themes, plugins & uploads ==="
cp -r /var/www/oont/themes/ /var/www/html/wp-content
cp -r /var/www/oont/plugins/ /var/www/html/wp-content
cp -r /var/www/oont/uploads /var/www/html/wp-content
cp -r /var/www/oont/uploads-webpc/ /var/www/html/wp-content
####################################
# 1. CONFIGURE THESE VARIABLES
####################################

# --- Production DB ---
PROD_DB_HOST="${PROD_DB_HOST}"
PROD_DB_USER="${PROD_DB_USER}"
PROD_DB_PASS="${PROD_DB_PASS}"
PROD_DB_NAME="${PROD_DB_NAME}"


# --- Dev DB ---
DEV_DB_HOST="${WORDPRESS_DB_HOST}"
DEV_DB_USER="${WORDPRESS_DB_USER}"
DEV_DB_PASS="${WORDPRESS_DB_PASSWORD}"
DEV_DB_NAME="${WORDPRESS_DB_NAME}"


# Temporary SQL dump file
SQL_FILE="/tmp/prod-dump.sql"

# Old (production) and New (dev) domain
OLD_DOMAIN="${PROD_DOMAIN}"
NEW_DOMAIN="${DEV_DOMAIN}"



####################################
# 2. DUMP PRODUCTION DB
####################################
apt update && apt install mariadb-client
echo -e "\033[31mDo you want to dump the latest DB? [y/N]: \033[0m"
read -n 1 -r
echo    # move to a new line

# Check the response (if the first character is y/Y, proceed)
if [[ "$REPLY" =~ ^[Yy]$ ]]; then

  echo "=== Dumping Production DB from ${PROD_DB_HOST} ==="
  mysqldump --single-transaction \
    -h "${PROD_DB_HOST}" \
    -u "${PROD_DB_USER}" \
    -p"${PROD_DB_PASS}" \
    --add-drop-table \
    "${PROD_DB_NAME}" \
    > "${SQL_FILE}" &
  
  # Capture the PID of the mysqldump job
  DUMP_PID=$!
  
  echo "Mysqldump started (PID: $DUMP_PID). Monitoring file size..."
  
  # While the dump is still running, show file size every 5 seconds
  while kill -0 "$DUMP_PID" 2>/dev/null; do
    CURRENT_SIZE=$(du -h "${SQL_FILE}" 2>/dev/null | cut -f1)
    echo "Current dump size: $CURRENT_SIZE"
    sleep 5
  done
  
  # Wait for mysqldump to finish (or capture its exit code)
  wait "$DUMP_PID"
  EXIT_CODE=$?
  
  if [ $EXIT_CODE -eq 0 ]; then
    echo "Dump completed successfully!"
  else
    echo "Dump failed with exit code $EXIT_CODE"
  fi

else
  echo "Skipping database dump."
fi

####################################
# 3. IMPORT INTO DEV DB
####################################
echo "=== Importing dump into Dev DB on ${DEV_DB_HOST} ==="
mysql \
  -h "${DEV_DB_HOST}" \
  -u "${DEV_DB_USER}" \
  -p"${DEV_DB_PASS}" \
  "${DEV_DB_NAME}" \
  < "${SQL_FILE}"

if [ $? -ne 0 ]; then
  echo "ERROR: Failed to import into dev DB."
  exit 1
fi

####################################
# 4. SEARCH & REPLACE WITH WP-CLI
####################################
echo "=== Running WP-CLI search-replace to fix domain: ${OLD_DOMAIN} -> ${NEW_DOMAIN} ==="
cd "${DEV_WP_PATH}" || {
  echo "ERROR: Cannot cd into dev WP path: ${DEV_WP_PATH}"
  exit 1
}

# We do two replacements to handle both http and https references.
# If your production is strictly https, you can remove the http line.
# If your prod is strictly http, you can remove the https line.
# Otherwise, let's do both for completeness.

# Replace https://OLD_DOMAIN with http://NEW_DOMAIN
php -d memory_limit=512M /usr/local/bin/wp search-replace "https://${OLD_DOMAIN}" "http://${NEW_DOMAIN}" --skip-columns=guid --allow-root
php -d memory_limit=512M /usr/local/bin/wp search-replace "http://${OLD_DOMAIN}" "http://${NEW_DOMAIN}" --skip-columns=guid --allow-root
# If you want your dev site to be https too, adjust accordingly.

if [ $? -ne 0 ]; then
  echo "ERROR: WP-CLI search-replace failed."
  exit 1
fi

####################################
# 5. CLEANUP (OPTIONAL)
####################################
# echo "=== Removing SQL file: ${SQL_FILE} ==="
# rm -f "${SQL_FILE}"
#
echo "=== Done! Your Dev DB is now synced from Prod and domain references updated. ==="

