#!/bin/bash
export CURRENT_PWD=$(pwd)

PGHOME=$CURRENT_PWD/product/17/db_2/
PGDATA=$CURRENT_PWD/pgdata/17/PG1/data
PGWAL=$CURRENT_PWD/pgdata/17/PG1/pg_wal
PGWAL_ARK=$CURRENT_PWD/pgdata/17/PG1/archived_wal

echo "Stop pg"
pg_ctl stop

rm -rf ./pgdata

echo "INIT DB"
#Init DB
mkdir -p $PGDATA
mkdir -p $PGWAL
mkdir -p $PGWAL_ARK

$PGHOME/bin/initdb \
--pgdata=$PGDATA \
--waldir=$PGWAL \
--pwprompt \
--data-checksums


#Control 
export PATH="${PGHOME}/bin:${PATH}"
pg_ctl -D $PGDATA start

psql -c 'select version();' postgres

echo "Installation done."

echo "Creating db and user..."
psql -c 'create database dbtest;' postgres
psql -c "create user mlogtest with encrypted password 'mlogtestpw';" postgres
psql -c 'grant all privileges on database dbtest to mlogtest;' postgres
psql -c 'ALTER DATABASE dbtest OWNER TO mlogtest;' postgres

#for test with extension uncomment those
#psql -c 'create extension pg_stat_statements WITH SCHEMA public;' dbtest
#psql -c 'alter system set shared_preload_libraries = 'pg_stat_statements';' dbtest


echo "Start test..."
time ./test_bug.sh

