#!/bin/bash
# Full, repeatable D7 -> D10 migration run for OAGMAC (local).
# Usage: scripts/migrate_full.sh [--keep-files]
set -u
cd "$(dirname "$0")/.."
export PATH="/Applications/MAMP/bin/php/php8.4.17/bin:/Applications/MAMP/Library/bin/mysql80/bin:$PATH"
DRUSH="php -d memory_limit=2048M vendor/bin/drush.php"
SNAP=/Users/kyawsoemin/Documents/oagmac_archive/d10_snapshots/01_ready_to_migrate.sql.gz
LOG=/Users/kyawsoemin/Documents/oagmac_archive/d10_snapshots/migrate_$(date +%Y%m%d_%H%M).log

echo "== restore clean snapshot" | tee -a "$LOG"
$DRUSH sql:drop -y >/dev/null 2>&1
gunzip -c "$SNAP" | $($DRUSH sql:connect) 2>/dev/null
$DRUSH cr >/dev/null 2>&1

run() {
  echo "== $*" | tee -a "$LOG"
  $DRUSH migrate:import "$@" --continue-on-failure 2>&1 | grep -E "\[error\]|done with" | sed -E 's/^ *[0-9]+\/[0-9]+ \[[=>-]*\] *[0-9]+%//' >> "$LOG"
}

run --group=migrate_drupal_7 --tag=Configuration
run upgrade_d7_user_role,upgrade_d7_user

# Views must exist before menu links (links to view paths fail validation).
$DRUSH en views_migration -y >/dev/null 2>&1
$DRUSH cr >/dev/null 2>&1
run d7_views_migration
$DRUSH scr scripts/post_views_fixes.php 2>&1 | tee -a "$LOG"

# Everything else (files, terms, nodes, then url_alias/menu links) in
# dependency order.
run --group=migrate_drupal_7

echo "== post-migration fixes" | tee -a "$LOG"
# Unmanaged files (images referenced directly in block/node bodies).
rsync -a --ignore-existing --exclude styles/ --exclude css/ --exclude js/ --exclude ctools/ --exclude php/ \
  --exclude languages/ --exclude xmlsitemap/ --exclude '*.php' --exclude .htaccess \
  /Users/kyawsoemin/Documents/oagmac/sites/default/files/ web/sites/default/files/
# D7 editors uploaded PDFs/fonts/images by hand into styles/oag/ and
# styles/flash/ (not image styles; body iframes/links point there).
rsync -a --ignore-existing \
  /Users/kyawsoemin/Documents/oagmac/sites/default/files/styles/oag \
  /Users/kyawsoemin/Documents/oagmac/sites/default/files/styles/flash \
  web/sites/default/files/styles/
$DRUSH scr scripts/sync_image_styles.php 2>&1 | tee -a "$LOG"
$DRUSH scr scripts/fix_missing_image_styles.php 2>&1 | tee -a "$LOG"
$DRUSH scr scripts/site_setup.php 2>&1 | tee -a "$LOG"
$DRUSH scr scripts/fix_menu_translations.php 2>&1 | tee -a "$LOG"
$DRUSH en colorbox -y >/dev/null 2>&1
$DRUSH scr scripts/colorbox_setup.php 2>&1 | tee -a "$LOG"
$DRUSH scr scripts/pdf_reader_setup.php 2>&1 | tee -a "$LOG"
$DRUSH scr scripts/optimize_logo.php 2>&1 | tee -a "$LOG"
$DRUSH scr scripts/perf_setup.php 2>&1 | tee -a "$LOG"
$DRUSH scr scripts/fix_changed_dates.php 2>&1 | tee -a "$LOG"
# Admin theme + CKEditor 5 (run twice: installing Claro ends the first run).
$DRUSH scr scripts/admin_setup.php 2>&1 | tee -a "$LOG"
$DRUSH scr scripts/admin_setup.php 2>&1 | tee -a "$LOG"
$DRUSH scr scripts/convert_filtered_html.php 2>&1 | tee -a "$LOG"
$DRUSH scr scripts/role_permissions.php 2>&1 | tee -a "$LOG"
# D7 had no comments: drop the unused comment_body storage (stale bundles).
$DRUSH php:eval '$s = \Drupal\field\Entity\FieldStorageConfig::loadByName("comment", "comment_body"); if ($s) { $s->delete(); field_purge_batch(500); print "comment_body removed\n"; }' 2>&1 | tee -a "$LOG"

echo "== status (incomplete only)" | tee -a "$LOG"
$DRUSH migrate:status --group=migrate_drupal_7 --fields=id,total,imported,unprocessed --format=csv 2>/dev/null | awk -F, 'NR==1 || $4!=0' | tee -a "$LOG"
$DRUSH cr >/dev/null 2>&1
echo "LOG: $LOG"
