#!/bin/sh
# Put the exported platform onto this server, over an empty (or throw-away) installation.
#
#   cd platform/moodle && sh ../../deploy/import-platform.sh /path/to/_export/2026-09-20
#
# Everything the platform holds is replaced: the database and moodledata. Nothing else is touched — .env, the
# images and the plugins stay as they are on this machine.
set -e

# Git Bash on Windows rewrites a container path like /app/media into a path on the C: drive
# before docker ever sees it. Linux has no such habit, so this is harmless on the server.
MSYS_NO_PATHCONV=1
export MSYS_NO_PATHCONV

# A value from .env. Windows line endings travel with the file, and a trailing carriage return turns
# «moodle» into a name Postgres has never heard of, so it is stripped here once.
envval() {
    grep "^$1=" .env | head -1 | cut -d= -f2- | tr -d '\015'
}

src=$1
[ -d "$src" ] || { echo "usage: sh import-platform.sh <folder with moodle-db.dump and moodledata.tgz>"; exit 1; }
[ -f .env ] || { echo "run this from platform/moodle"; exit 1; }

db_user=$(envval MOODLE_DB_USER)
db_name=$(envval MOODLE_DB_NAME)
wwwroot=$(envval MOODLE_WWWROOT)

echo "This replaces the database «$db_name» and moodledata on THIS machine."
echo "wwwroot here is: $wwwroot"
printf 'type yes to go on: '
read answer
[ "$answer" = "yes" ] || exit 1

echo "stopping the web side (the database keeps running) …"
docker compose stop moodle cron web 2>/dev/null || true
docker compose up -d db
sleep 5

echo "database …"
docker compose exec -T db psql -U "$db_user" -d "$db_name" -c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public;'
docker compose exec -T db pg_restore -U "$db_user" -d "$db_name" --no-owner --no-privileges < "$src/moodle-db.dump"

echo "moodledata …"
docker compose run --rm -T --no-deps -v "$(cd "$src" && pwd):/backup:ro" moodle sh -c \
  'rm -rf /var/www/moodledata/* /var/www/moodledata/.[!.]* 2>/dev/null; tar xzf /backup/moodledata.tgz -C /var/www/moodledata; chown -R www-data:www-data /var/www/moodledata'

echo "starting …"
docker compose up -d
sleep 10

# The theme and the plugins are code, not content: they travel with the repository, not with the dump.
# Copying the files across is only half of it — they still have to be put where the application reads them.
# Both containers need it: the cron one keeps its own copy of the code, and refuses to run any task while
# the version it can see is older than the database's ("Moodle upgrade pending, cron execution suspended").
echo "plugins …"
docker compose exec -T moodle sync-plugins --live
docker compose exec -T cron sync-plugins --live

echo "Moodle's own upgrade step (no-op when the versions match) …"
docker compose exec -T -u www-data moodle php /var/www/html/admin/cli/upgrade.php --non-interactive || true

echo "rewriting the addresses inside the content …"
old=$(envval MIGRATE_OLD_WWWROOT)
old=${old:-http://localhost:8891}
# Moodle 5 serves from public/, and that is where the tools live; the few wrappers at the top
# (admin/cli/upgrade.php, admin/cli/purge_caches.php) are the exception
docker compose exec -T -u www-data moodle php /var/www/html/public/admin/tool/replace/cli/replace.php \
  --search="$old" --replace="$wwwroot" --non-interactive

echo "settings that come from .env, and the caches …"
docker compose exec -T -u www-data moodle php /var/www/html/public/local/thinkera/cli/set_mail.php || true
docker compose exec -T -u www-data moodle php /var/www/html/public/local/thinkera/cli/set_bbb.php || true
docker compose exec -T -u www-data moodle php /var/www/html/public/local/thinkera/cli/set_whatsapp.php || true
docker compose exec -T -u www-data moodle php /var/www/html/public/local/thinkera/cli/set_analytics.php || true
docker compose exec -T -u www-data moodle php /var/www/html/public/theme/thinkera/cli/rebrand_strings.php || true
docker compose exec -T -u www-data moodle php /var/www/html/admin/cli/purge_caches.php

echo
echo "done. Open $wwwroot and sign in."
