#!/usr/bin/env bash
# Build a code-only release zip to upload to cPanel.
# Run on your computer from the project folder:   bash deploy/make_release.sh
# It NEVER includes the database, uploaded pictures, private magazine PDFs or .env,
# so uploading it can't overwrite anything on the live site.
set -euo pipefail
cd "$(dirname "$0")/.."

STAMP=$(date +%Y%m%d-%H%M)
OUT="release/thereversetimes-$STAMP.zip"
mkdir -p release

# Only these files and folders are code.
INCLUDE=(apps reverse_times templates static locale deploy manage.py passenger_wsgi.py requirements.txt .htaccess .env.example DEPLOY.md)

echo "Checking the code before packing…"
venv/bin/python -W ignore manage.py makemigrations --check --dry-run >/dev/null || {
  echo "✗ There are model changes without a migration. Run: venv/bin/python manage.py makemigrations"; exit 1; }
venv/bin/python -W ignore manage.py test --noinput >/dev/null 2>&1 || {
  echo "✗ Tests failed. Fix them before deploying (run: venv/bin/python manage.py test)."; exit 1; }

zip -rq "$OUT" "${INCLUDE[@]}" \
  -x '*/__pycache__/*' '*.pyc' '*.DS_Store' 'templates/articles/test.html'

# Safety net: refuse to ship anything that holds live data or secrets.
if unzip -l "$OUT" | grep -E '(db\.sqlite3|(^|/)\.env$|(^| )media/|private_media/|backups/)' >/dev/null; then
  echo "✗ The zip contains data or secrets — not safe to upload."; rm -f "$OUT"; exit 1
fi

echo "✓ Release ready: $OUT  ($(du -h "$OUT" | cut -f1))"
echo "  Upload it to the app folder on cPanel, then run:  bash deploy/update_live.sh $(basename "$OUT")"
