#!/usr/bin/env bash
set -e
echo "Waiting for Postgres..."
until pg_isready -h "${POSTGRES_HOST:-db}" -p "${POSTGRES_PORT:-5432}" -U "${POSTGRES_USER:-illustrations}" >/dev/null 2>&1; do
  sleep 1
done
python manage.py migrate
python manage.py init_users
if [ "${IMPORT_SEED_ON_START}" = "true" ] && [ -f "${SEED_CSV}" ] && [ ! -f /data/imports/.seed_done ]; then
  echo "Seeding from ${SEED_CSV}..."
  python manage.py import_seed "${SEED_CSV}"
  touch /data/imports/.seed_done
fi
python manage.py runserver 0.0.0.0:8000
