#!/bin/sh

# bindir=$(dirname "$(readlink -fm "$0")")
# source "${bindir}/../.env"


postgres_pod_name=postgresql-primary-0

if [ -z $POSTGRES_PASSWORD ]; then 
    echo "POSTGRES_PASSWORD was missing in env. Are you executing this script via Tilt? (that is the intended method)"
    exit 5
fi

# ## Enable pgcrypto (needed by pg-boss)
# kubectl -n futureporn exec ${postgres_pod_name} -- env PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres --command "\
#     CREATE EXTENSION pgcrypto;"

# ## Create the temporal databases
# kubectl -n futureporn exec ${postgres_pod_name} -- env PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres --command "\
#     CREATE DATABASE temporal_visibility \
#     WITH \
#     OWNER = postgres \
#     ENCODING = 'UTF8' \
#     LOCALE_PROVIDER = 'libc' \
#     CONNECTION LIMIT = -1 \
#     IS_TEMPLATE = False;"


# kubectl -n futureporn exec ${postgres_pod_name} -- env PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres --command "\
#     CREATE DATABASE temporal \
#     WITH \
#     OWNER = postgres \
#     ENCODING = 'UTF8' \
#     LOCALE_PROVIDER = 'libc' \
#     CONNECTION LIMIT = -1 \
#     IS_TEMPLATE = False;"




# 	./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres12 --db $(TEMPORAL_DB) drop -f
# 	./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres12 --db $(TEMPORAL_DB) create
# 	./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres12 --db $(TEMPORAL_DB) setup -v 0.0
# 	./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres12 --db $(TEMPORAL_DB) update-schema -d ./schema/postgresql/v12/temporal/versioned
# 	./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres12 --db $(VISIBILITY_DB) drop -f
# 	./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres12 --db $(VISIBILITY_DB) create
# 	./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres12 --db $(VISIBILITY_DB) setup-schema -v 0.0
# 	./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres12 --db $(VISIBILITY_DB) update-schema -d ./schema/postgresql/v12/visibility/versioned



## Create the futureporn Strapi database
kubectl -n futureporn exec ${postgres_pod_name} -- env PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres --command "\
    CREATE DATABASE futureporn_db \
    WITH \
    OWNER = postgres \
    ENCODING = 'UTF8' \
    LOCALE_PROVIDER = 'libc' \
    CONNECTION LIMIT = -1 \
    IS_TEMPLATE = False;"


## Create the futureporn Postgrest database
## !!! Don't create the database here! Allow @services/migrations to create the database.


# @futureporn/migrations takes care of these tasks now
# ## Create graphile_worker db (for backend tasks)
# kubectl -n futureporn exec ${postgres_pod_name} -- env PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres --command "\
#     CREATE DATABASE graphile_worker \
#     WITH \
#     OWNER = postgres \
#     ENCODING = 'UTF8' \
#     LOCALE_PROVIDER = 'libc' \
#     CONNECTION LIMIT = -1 \
#     IS_TEMPLATE = False;"


# ## create futureporn user
# kubectl -n futureporn exec ${postgres_pod_name} -- env PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres --command "\
#     CREATE ROLE futureporn \
#     WITH \
# 	LOGIN \
# 	NOSUPERUSER \
# 	NOCREATEDB \
# 	NOCREATEROLE \
# 	INHERIT \
# 	NOREPLICATION \
# 	NOBYPASSRLS \
# 	CONNECTION LIMIT -1 \
# 	PASSWORD '$POSTGRES_REALTIME_PASSWORD';"


## grant futureporn user all privs
# kubectl -n futureporn exec ${postgres_pod_name} -- env PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres --command "\
#     GRANT ALL PRIVILEGES ON DATABASE postgrest TO futureporn;"
# kubectl -n futureporn exec ${postgres_pod_name} -- env PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres --command "\
#     GRANT ALL PRIVILEGES ON DATABASE graphile_worker TO futureporn;"



## import schema
## I have a file, schema.psql that I want to import. How do I do that?
# kubectl -n futureporn exec postgresql -- psql -U postgres --command "\ ;"
# kubectl -n futureporn exec postgresql -- psql -U postgres -f - < "${bindir}/postgres-2024-05-09-futureporn_db-schema-only.psql"