# 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

 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="/home/anastasia/original_repo/cf_2215"
# branch name - upgrade from
 OLD_VERSION="REL_12_STABLE"
 # branch name - upgrade to
 # the patch will be applied to this branch
 NEW_VERSION="master"

#killall postgres

git checkout $OLD_VERSION
git clean -df
make distclean -s
./configure --prefix=$WORKDIR/postgresql_bin_test_old
make -j4 -s
make install -j4 -s

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 -c "select version();" postgres

# set some non-default ACL on catalog objects
cat $PATCHDIR/test_add_acl_to_catalog_objects.sql
$WORKDIR/postgresql_bin_test_old/bin/psql -f $PATCHDIR/revoke_test.sql postgres
$WORKDIR/postgresql_bin_test_old/bin/psql -f $PATCHDIR/test_add_acl_to_catalog_objects.sql postgres


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

git checkout $NEW_VERSION
git clean -df

patch -p1 < $PATCHDIR/pg_upgrade_ACL_check_v14.patch
# special patch that renames system objects to test new feature
 patch -p1 < $PATCHDIR/test_rename_catalog_objects_v14


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

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 -c "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

# Shouldn't fail without test_rename_catalog_objects.patch (see above)
# Should fail with test_rename_catalog_objects.patch 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

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 -f ./fix_system_objects_ACL.sql
$WORKDIR/postgresql_bin_test_old/bin/pg_ctl -D $WORKDIR/postgresql_data_test_old stop -w

# 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