#!/bin/bash
set -e

echo "[$(date '+%Y-%m-%d %H:%M:%S')] Waiting for database..."
until pg_isready -h ${DB_HOST:-db} -p ${DB_PORT:-5432} -U ${DB_USER:-px360} 2>/dev/null; do
    sleep 1
done
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Database is ready."

echo "[$(date '+%Y-%m-%d %H:%M:%S')] Running migrations..."
python manage.py migrate --noinput

echo "[$(date '+%Y-%m-%d %H:%M:%S')] Collecting static files..."
python manage.py collectstatic --noinput || true

echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting $@..."
exec "$@"
