#!/bin/sh
set -e

# Apply pending database migrations before the server accepts traffic.
# Disable for multi-replica deploys (run migrations as a separate one-off job)
# by setting RUN_MIGRATIONS_ON_START=false.
if [ "${RUN_MIGRATIONS_ON_START:-true}" = "true" ]; then
  echo "[entrypoint] Running database migrations..."
  node scripts/migrate.mjs
else
  echo "[entrypoint] RUN_MIGRATIONS_ON_START=false — skipping migrations."
fi

echo "[entrypoint] Starting Next.js on ${HOSTNAME:-0.0.0.0}:${PORT:-3000}"
exec node server.js
