#!/bin/bash
set -e

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

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