#!/bin/bash
set -e

echo "Waiting for PostgreSQL..."
./wait-for-postgres.sh postgres-db

echo "Checking migration status..."
python manage.py showmigrations

echo "Attempting migration with error handling..."
if python manage.py migrate --check; then
    echo "No migrations needed"
elif python manage.py migrate --fake-initial; then
    echo "Migrations applied successfully"
else
    echo "Migration failed, trying fake-initial..."
    python manage.py migrate --fake-initial || echo "Fake-initial also failed, proceeding anyway..."
fi

echo "Starting Gunicorn..."
exec gunicorn config.wsgi:application --bind 0.0.0.0:8000
