#!/bin/bash
set -evux

PG_BIN=$(pg_config --bindir)
PG_ETC=$(pg_config --sharedir | sed 's|/usr/share/postgresql|/etc/postgresql|')
export PGPORT=5432

sudo -u postgres "${PG_BIN}/pg_ctl" \
  start \
  -D ${PG_ETC}/main \
  -o "-h localhost -p $PGPORT"
sleep 10
sudo -u postgres createuser -p "$PGPORT" root -s -w

# 'anastasistalercheck' is used by the exchange, the merchant and
# libeufin-bank that taler-unified-setup.sh brings up for the reducer and
# IBAN tests.  taler-unified-setup.sh runs every one of those dbinits with
# --reset, so the name must NOT be shared with another project's test
# suite: the plain 'talercheck' that the exchange, merchant, sync and donau
# suites all use gets its schema dropped out from under a running exchange
# whenever two of those suites overlap on one host, which shows up here as
# spurious 500s ('relation "denominations" does not exist') from
# taler-exchange-offline.
sudo -u postgres createdb -p "$PGPORT" -O root anastasistalercheck
# The provider database: 'anastasischeck' for the stasis and testing
# suites, 'anastasischeck1' .. 'anastasischeck4' for the four providers
# the reducer/CLI tests run against (see src/cli/test_anastasis_reducer_*.conf).
sudo -u postgres createdb -p "$PGPORT" -O root anastasischeck
for n in 1 2 3 4; do
  sudo -u postgres createdb -p "$PGPORT" -O root "anastasischeck${n}"
done
