#!/usr/bin/env bash
# ----------------------------------------------------------------------------
# wp-pre-migration-check.sh
#
# Purpose: read-only pre-migration report for a WordPress site using WP-CLI:
#          WordPress and PHP versions, siteurl and home, active plugins and theme,
#          database size, uploads folder size, and scheduled cron events.
#          It only runs list, get and size commands. It changes nothing.
#
# Provided by cloud-migrations.au as is; test before use.
#
# Requirements: bash, WP-CLI (wp) on the PATH, du and find, and run it as the user
#               that owns the WordPress files.
#
# Usage:
#   ./wp-pre-migration-check.sh -p /var/www/contoso.com/public_html
#   ./wp-pre-migration-check.sh -p /var/www/contoso.com/public_html -o ~/contoso-before.txt -s
#
# Note: "wp cli info" shows the PHP binary WP-CLI uses on the command line. WP-CLI's
#       handbook notes the command-line php.ini is typically different from the web
#       server's, so confirm the web PHP version in your hosting control panel too.
# ----------------------------------------------------------------------------
set -u

WP_PATH="$(pwd)"
UPLOADS=""
OUTFILE="$(pwd)/wp-precheck_$(date +%Y%m%d-%H%M).txt"
SKIP=0

usage() {
  echo "Usage: $0 [-p wordpress-path] [-u uploads-path] [-o report-file] [-s]"
  exit 1
}

while getopts "p:u:o:sh" opt; do
  case "$opt" in
    p) WP_PATH="$OPTARG" ;;
    u) UPLOADS="$OPTARG" ;;
    o) OUTFILE="$OPTARG" ;;
    s) SKIP=1 ;;
    *) usage ;;
  esac
done

[ -z "$UPLOADS" ] && UPLOADS="${WP_PATH%/}/wp-content/uploads"

if ! command -v wp >/dev/null 2>&1; then
  echo "ERROR: WP-CLI (wp) is not on the PATH. Install it first." >&2
  exit 2
fi

WP=(wp "--path=${WP_PATH}")
[ "$SKIP" -eq 1 ] && WP+=(--skip-plugins --skip-themes)

if ! "${WP[@]}" core is-installed >/dev/null 2>&1; then
  echo "ERROR: no installed WordPress found at ${WP_PATH} (wp core is-installed failed)." >&2
  exit 2
fi

: > "$OUTFILE" || { echo "ERROR: cannot write ${OUTFILE}" >&2; exit 3; }

section() {
  # section "Heading" command args...
  local heading="$1"; shift
  echo "  ${heading}"
  {
    echo ""
    echo "=== ${heading} ==="
    if ! "$@" 2>&1; then
      echo "FAILED: $*"
    fi
  } >> "$OUTFILE"
}

{
  echo "WordPress pre-migration check"
  echo "Site path: ${WP_PATH}"
  echo "Taken:     $(date -u '+%Y-%m-%d %H:%M:%S') UTC on $(hostname)"
} >> "$OUTFILE"

echo "Checking WordPress at ${WP_PATH}..."
section "WP-CLI and PHP environment (command line)" wp cli info
section "WordPress version"          "${WP[@]}" core version --extra
section "WordPress core updates"     "${WP[@]}" core check-update
section "siteurl"                    "${WP[@]}" option get siteurl
section "home"                       "${WP[@]}" option get home
section "Active plugins"             "${WP[@]}" plugin list --status=active --fields=name,status,version,update
section "Must-use plugins"           "${WP[@]}" plugin list --status=must-use
section "Drop-ins"                   "${WP[@]}" plugin list --status=dropin
section "Active theme"               "${WP[@]}" theme list --status=active
section "Database size"              "${WP[@]}" db size
section "Database tables by size"    "${WP[@]}" db size --tables --human-readable --orderby=size --order=desc
section "Scheduled cron events"      "${WP[@]}" cron event list

# Uploads folder size and file count (plain shell, read-only)
echo "  Uploads folder"
{
  echo ""
  echo "=== Uploads folder (${UPLOADS}) ==="
  if [ -d "$UPLOADS" ]; then
    du -sh "$UPLOADS" 2>&1
    echo "Files: $(find "$UPLOADS" -type f 2>/dev/null | wc -l | tr -d ' ')"
  else
    echo "FAILED: folder not found. Use -u to give the uploads path."
  fi
} >> "$OUTFILE"

echo ""
echo "Report saved to: $(cd "$(dirname "$OUTFILE")" && pwd)/$(basename "$OUTFILE")"
