#!/bin/bash
#
# Move one install from Django 5.1.4 to the pinned version, with a way back.
#
# 5.1.4 is from December 2024. The 5.1 branch reached end of extended support
# on 2 December 2025, with 5.1.15 as its last security release, so this install
# is missing every security fix issued since. It is not an optional upgrade.
#
# There is a second problem this fixes. ft-tenant-install.sh runs
# pip install -r requirements.txt, and that file pins 5.2.17. So every tenant
# installed recently is already on 5.2.17 while Final Target runs 5.1.4. The
# estate is split across two frameworks and nobody decided that.
#
# Run it on paneltest first. Only when paneltest is clean does finaltarget go.
#
# Usage, on the server:
#   bash deploy/upgrade-django.sh paneltest
#   bash deploy/upgrade-django.sh finaltarget
#
set -uo pipefail

ACCOUNT="${1:-}"
if [ -z "$ACCOUNT" ]; then
  echo "Which install? e.g. bash deploy/upgrade-django.sh paneltest"; exit 1
fi
APP="/home/${ACCOUNT}/public_html/backend"
PY="${APP}/venv/bin/python"
PIP="${APP}/venv/bin/pip"
[ -x "$PY" ] || { echo "No venv at ${APP}/venv"; exit 1; }

STAMP=$(date +%F-%H%M)
FREEZE="/root/pip-freeze-${ACCOUNT}-${STAMP}.txt"

echo "── 1. Record what is installed now, so there is a way back ─────────────"
"$PIP" freeze > "$FREEZE"
chmod 600 "$FREEZE"
echo "  saved $FREEZE"
CURRENT=$("$PY" -c "import django; print(django.get_version())")
TARGET=$(grep -iE '^django==' "${APP}/requirements.txt" | cut -d= -f3)
echo "  installed: $CURRENT    pinned: $TARGET"
[ "$CURRENT" = "$TARGET" ] && { echo "  already there, nothing to do"; exit 0; }

echo
echo "── 2. Upgrade ──────────────────────────────────────────────────────────"
"$PIP" install -q "Django==${TARGET}" || { echo "  pip failed, nothing changed"; exit 1; }
"$PY" -c "import django; print('  now on', django.get_version())"

echo
echo "── 3. Does it still start, and does the schema still match ─────────────"
cd "$APP" || exit 1
"$PY" manage.py check --settings=config.settings.production || echo "  CHECK FAILED"
"$PY" manage.py migrate --check --settings=config.settings.production \
  && echo "  schema matches" || echo "  migrations pending, look before restarting"

echo
echo "── 4. Restart and sweep the pages ──────────────────────────────────────"
systemctl restart "$ACCOUNT" && sleep 3 && systemctl is-active "$ACCOUNT"
HOST=$("$PY" -c "
import os, django
os.environ.setdefault('DJANGO_SETTINGS_MODULE','config.settings.production')
django.setup()
from apps.tenants.models import Tenant
t = Tenant.objects.filter(is_master=True).first()
print(t.cms_subdomain if t else '')
" 2>/dev/null)
for path in / /system/ /tickets/ /settings/ /tenants/; do
  [ -n "$HOST" ] && curl -s -o /dev/null -w "  %{http_code}  https://${HOST}${path}\n" "https://${HOST}${path}"
done

cat <<NOTE

If anything above is a 500, roll back with:

  ${PIP} install -q -r ${FREEZE}
  systemctl restart ${ACCOUNT}

That freeze file holds no secrets, only package versions, but it is 600
anyway. Delete it once you are happy.
NOTE
