#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" --port "${PGPORT:-5432}" <<-EOSQL
    CREATE DATABASE hub;
    GRANT ALL PRIVILEGES ON DATABASE hub TO "user";
    CREATE USER hub;
    GRANT ALL PRIVILEGES ON DATABASE hub TO "hub";
EOSQL

if test -f "/docker-entrypoint-initdb.d/dumps/hub.sql"; then
    psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname hub --port "${PGPORT:-5432}" < /docker-entrypoint-initdb.d/dumps/hub.sql
else
    echo "can not find hub db dump to load"
fi
