#!/bin/bash
# This test case reproduce the issue with an extension Makefile build with PGSC when trying to uninstall the extension.

# Variables about the Postgres installation
PG_SHAREDIR=$(pg_config --sharedir)
PG_BINDIR=$(pg_config --bindir)
PG_DOCDIR=$(pg_config --docdir)

# Prepare the environment
CURDIR=$(pwd)
TMPDIR=/tmp/testPGXSuninstall

rm -rf $TMPDIR
mkdir $TMPDIR
cd $TMPDIR

# Build the extension distribution
mkdir sql
touch sql/myext_1.0.0.sql
mkdir doc
touch doc/mydoc1
touch doc/mydoc2
mkdir script
touch script/myscript1
touch script/myscript2

cat >myext.control <<EOF
default_version 	= '1.1.0'
directory 			= 'myext'
EOF

cat >Makefile <<EOF
EXTENSION    = myext
MODULEDIR    = myext
DATA         = \$(wildcard sql/*)
DOCS         = \$(wildcard doc/*)
SCRIPTS      = \$(wildcard script/*)
PG_CONFIG   ?= pg_config
PGXS := \$(shell \$(PG_CONFIG) --pgxs)
include \$(PGXS)
EOF

# Install the extension
echo "------------ Install the extension ------------"
make
sudo make install

# Check
echo "------------ Check ------------"
ls $PG_SHAREDIR/extension/myext.control
ls $PG_SHAREDIR/myext
ls $PG_BINDIR/myscript*
ls $PG_DOCDIR/myext

# Uninstall the extension
echo "------------ Uninstall the extension ------------"
sudo make uninstall

# Check
echo "------------ Check (nothing should be found here) ------------"
ls $PG_SHAREDIR/extension/myext.control
ls $PG_SHAREDIR/extension/myext
ls $PG_BINDIR/myscript*
ls $PG_DOCDIR/myext

# Cleanup the test environment
cd $CURDIR
rm -r $TMPDIR

