# Run this script in a directory that contains postgres git repository with
# old_version and new_version branches
#
# Set WORKDIR as a root path for binaries and pgdata directories

unset PGPORT
unset PGDATABASE
unset PGDATA

 WORKDIR="/tmp/workdir"
 export WORKDIR="/tmp/workdir"

# path to the directory with a patchset:
#    pg_upgrade_ACL_check_v12.patch
#    test_rename_catalog_objects_v12
#    test_add_acl_to_catalog_objects.sql
PATCHDIR="/tmp/x"
# branch name - upgrade from
 OLD_VERSION="REL9_6_STABLE"
 # branch name - upgrade to
 # the patch will be applied to this branch
 NEW_VERSION="master"
 NEW_VERSION="3063eb1"

#killall postgres

if :; then
git clean -df
git reset --hard HEAD
git checkout $OLD_VERSION
make distclean -s
./configure --prefix=$WORKDIR/postgresql_bin_test_old
make -j20 -s
make install -j20 -s
fi

rm -rf $WORKDIR/postgresql_data_test_old
mkdir $WORKDIR/postgresql_data_test_old
$WORKDIR/postgresql_bin_test_old/bin/initdb -D $WORKDIR/postgresql_data_test_old

$WORKDIR/postgresql_bin_test_old/bin/pg_ctl -D $WORKDIR/postgresql_data_test_old start -w
$WORKDIR/postgresql_bin_test_old/bin/psql -Xc "select version();" postgres

# set some non-default ACL on catalog objects
$WORKDIR/postgresql_bin_test_old/bin/psql -Xc 'revoke all on function pg_catalog.timenow() from public;' postgres


$WORKDIR/postgresql_bin_test_old/bin/pg_ctl -D $WORKDIR/postgresql_data_test_old stop -w

if :; then

git clean -df
rm -f ./src/include/dynloader.h ||:
git reset --hard HEAD
git checkout $NEW_VERSION

patch -p1 < $PATCHDIR/pg_upgrade_ACL_check_v14.patch


   make distclean -s
   ./configure --prefix=$WORKDIR/postgresql_bin_test_new --enable-cassert --enable-debug --enable-depend CFLAGS="-O0 -g2"
   make -j20 -s
 make install -j20 -s

fi

rm -rf $WORKDIR/postgresql_data_test_new
mkdir $WORKDIR/postgresql_data_test_new
$WORKDIR/postgresql_bin_test_new/bin/initdb -D $WORKDIR/postgresql_data_test_new

$WORKDIR/postgresql_bin_test_new/bin/pg_ctl -D $WORKDIR/postgresql_data_test_new start -w
$WORKDIR/postgresql_bin_test_new/bin/psql -Xc "select version();" postgres
$WORKDIR/postgresql_bin_test_new/bin/pg_ctl -D $WORKDIR/postgresql_data_test_new stop -w

rm ./fix_system_objects_ACL.sql ||:

# Should fail and generate fix_system_objects_ACL.sql script
$WORKDIR/postgresql_bin_test_new/bin/pg_upgrade \
-k --old-bindir=$WORKDIR/postgresql_bin_test_old/bin \
 --new-bindir=$WORKDIR/postgresql_bin_test_new/bin \
  --old-datadir=$WORKDIR/postgresql_data_test_old \
   --new-datadir=$WORKDIR/postgresql_data_test_new --check ||:

if [ -f ./fix_system_objects_ACL.sql ]; then

cat ./fix_system_objects_ACL.sql


$WORKDIR/postgresql_bin_test_old/bin/pg_ctl -D $WORKDIR/postgresql_data_test_old start -w
$WORKDIR/postgresql_bin_test_old/bin/psql postgres -Xf ./fix_system_objects_ACL.sql
$WORKDIR/postgresql_bin_test_old/bin/pg_ctl -D $WORKDIR/postgresql_data_test_old stop -w

fi

# Must succeed
$WORKDIR/postgresql_bin_test_new/bin/pg_upgrade \
-k --old-bindir=$WORKDIR/postgresql_bin_test_old/bin \
 --new-bindir=$WORKDIR/postgresql_bin_test_new/bin \
  --old-datadir=$WORKDIR/postgresql_data_test_old \
   --new-datadir=$WORKDIR/postgresql_data_test_new --check

# Ideally would not fail, but it does fail
$WORKDIR/postgresql_bin_test_new/bin/pg_upgrade \
-k --old-bindir=$WORKDIR/postgresql_bin_test_old/bin \
 --new-bindir=$WORKDIR/postgresql_bin_test_new/bin \
  --old-datadir=$WORKDIR/postgresql_data_test_old \
   --new-datadir=$WORKDIR/postgresql_data_test_new
