#!/bin/sh
if [ "${CKAN_DATASTORE_POSTGRES_DB}" ]; then
    psql -U postgres -c "CREATE USER ${CKAN_DATASTORE_POSTGRES_READ_USER} \
        WITH PASSWORD '${CKAN_DATASTORE_POSTGRES_READ_PWD}' \
        NOSUPERUSER NOCREATEDB NOCREATEROLE;"

    if [ "${CKAN_DATASTORE_POSTGRES_WRITE_USER}" ] && [ "${CKAN_DATASTORE_POSTGRES_WRITE_PWD}"  ]; then
        psql -U postgres -c "CREATE USER ${CKAN_DATASTORE_POSTGRES_WRITE_USER} \
            WITH PASSWORD '${CKAN_DATASTORE_POSTGRES_WRITE_PWD}' \
            NOSUPERUSER NOCREATEDB NOCREATEROLE;"
    fi

    createdb -U postgres -e -O ${CKAN_DATASTORE_POSTGRES_WRITE_USER} ${CKAN_DATASTORE_POSTGRES_DB} -E utf-8
fi
