#!/bin/bash
# The test assumes following environment variables
#PGDir  - directory created by git clone of postgresql git repository

set -e

BuildDir=/tmp/pg_test_extra
mkdir $BuildDir

##################
# test meson     #
##################
XID_MODULE_DIR=$BuildDir/src/test/modules/xid_wraparound

# meson without configuring PG_TEST_EXTRA
cd $PGDir

meson setup $BuildDir --werror --prefix $BuildDir --buildtype=release
cd $BuildDir && ninja && ninja install && cd $PGDir
echo "TEST 10: running xid_wraparound tests, they should be skipped"
meson test -C $BuildDir --suite setup --suite xid_wraparound
echo "TEST 20: setting PG_TEST_EXTRA on commandline, they should not be skipped"
PG_TEST_EXTRA=xid_wraparound meson test -C $BuildDir --suite setup --suite xid_wraparound

rm -rf $BuildDir

#meson configuring PG_TEST_EXTRA
mkdir $BuildDir
cd $PGDir

meson setup $BuildDir --werror --prefix $BuildDir --buildtype=release -DPG_TEST_EXTRA=xid_wraparound
cd $BuildDir && ninja && ninja install && cd $PGDir
echo "TEST 30: tests should not be skipped"
meson test -C $BuildDir --suite setup --suite xid_wraparound
echo "TEST 40: setting PG_TEST_EXTRA on commandline, they should not be skipped"
PG_TEST_EXTRA=xid_wraparound meson test -C $BuildDir --suite setup --suite xid_wraparound
echo "TEST 50: override PG_TEST_EXTRA, they should be skipped"
PG_TEST_EXTRA=xyz meson test -C $BuildDir --suite setup --suite xid_wraparound
echo "TEST 60: overriding PG_TEST_EXTRA, they should be skipped"
PG_TEST_EXTRA="" meson test -C $BuildDir --suite setup --suite xid_wraparound

rm -rf $BuildDir

# overriding meson configured PG_TEST_EXTRA
mkdir $BuildDir
cd $PGDir

meson setup $BuildDir --werror --prefix $BuildDir --buildtype=release -DPG_TEST_EXTRA=wal_consistency_checking
cd $BuildDir && ninja && ninja install && cd $PGDir
echo "TEST 70: overriding PG_TEST_EXTRA, tests should not be skipped"
PG_TEST_EXTRA=xid_wraparound meson test -C $BuildDir --suite setup --suite xid_wraparound

rm -rf $BuildDir

##################
# test make      #
##################
XID_MODULE_DIR=$PGDir/src/test/modules/xid_wraparound

# Without configure time PG_TEST_EXTRA
mkdir $BuildDir
cd $PGDir

./configure --prefix=$BuildDir --enable-tap-tests && make -j4 && make -j4 install
echo "TEST 80: tests should be skipped"
make -C $XID_MODULE_DIR check
echo "TEST 90: from module directory, tests should be skipped"
cd $XID_MODULE_DIR && make check && cd $PGDir
echo "TEST 100: tests should not be skipped"
PG_TEST_EXTRA=xid_wraparound make -C $XID_MODULE_DIR check
echo "TEST 110: from module directory, tests should not be skipped"
cd $XID_MODULE_DIR && PG_TEST_EXTRA=xid_wraparound  make check

rm -rf $BuildDir

# Configure time PG_TEST_EXTRA
mkdir $BuildDir
cd $PGDir

./configure PG_TEST_EXTRA=xid_wraparound --prefix=$BuildDir --enable-tap-tests && make -j4 && make -j4 install
echo "TEST 120: tests should not be skipped"
make -C $XID_MODULE_DIR check
echo "TEST 130: from module directory, tests should not be skipped"
cd $XID_MODULE_DIR && make check && cd $PGDir
echo "TEST 140: tests should not be skipped"
PG_TEST_EXTRA=xid_wraparound make -C $XID_MODULE_DIR check
echo "TEST 150: from module directory, tests should not be skipped"
cd $XID_MODULE_DIR && PG_TEST_EXTRA=xid_wraparound make check && cd $PGDir
echo "TEST 160: overriding PG_TEST_EXTRA, tests should be skipped"
PG_TEST_EXTRA="xyz" make -C $XID_MODULE_DIR check
echo "TEST 170: overriding PG_TEST_EXTRA, from module directory, tests should be skipped"
cd $XID_MODULE_DIR && PG_TEST_EXTRA="" make check && cd $PGDir

rm -rf $BuildDir

# Overriding configure time PG_TEST_EXTRA
mkdir $BuildDir
cd $PGDir

./configure PG_TEST_EXTRA=wal_consistency_checking --prefix=$BuildDir --enable-tap-tests && make -j4 && make -j4 install
echo "TEST 180: overriding PG_TEST_EXTRA, tests should not be skipped"
PG_TEST_EXTRA=xid_wraparound make -C $XID_MODULE_DIR check

# Clean up
make distclean

rm -rf $BuildDir