# pg_isok  A query centered monitoring tool for PostgreSQL
# Copyright (C) 2025 The Meme Factory, Inc., http://www.karlpinc.com/
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# Karl O. Pinc <kop@karlpinc.com>
#

## ##################################################################
##                          Using this Makefile
##
## This Makefile is primarily intended to be used by the developers of
## PG_Isok.  Users of PG_Isok are expected to download a PGXN.org
## "distribution", and work from that.  But users without permissions
## to create extensions should read on, or, even better, read the
## documentation.
##
## If you have downloaded a PGXN.org "distribution", see the
## installation instructions in the doc directory.  The "quick
## start" is:
##
## When you have the necessary permissions to install PostgreSQL
## extensions, it is easiest to install and use the pgxn_client.  To,
## instead, use the Makefile directly:
##
##   make install
##   echo "CREATE EXTENSION pg_isok SCHEMA isok;" | psql ...
##
## When you are in the cloud, or otherwise can't use CREATE EXTENSION,
## you must generate the extension's SQL and execute that yourself:
##
##   # Produce an error if the m4 macro processor is not installed
##   m4 --help > /dev/null
##   # Generate the SQL, to install in the "isok" schema, which must exist
##   make TARGET_SCHEMA=isok sql/pg_isok_cloud--VERSION.sql
##   # Execute the generated SQL
##   psql ... < sql/pg_isok_cloud--VERSION.sql

# Variables
# Generated files needed for extension packaging
PKG_TARGETS := pg_isok.control META.json
EXTENSION := pg_isok
EXTVERSION := $(shell cat VERSION)
# The name of the top-level directory in the distribution archive
# (Also used to name the distribution zip file.)
DIST_DIR := $(EXTENSION)-$(EXTVERSION)
MAKE_FILES := make_files

# Global dependencies.  Rebuild everything when any of these change.
# (This is a somewhat annoying practice, because to make it work
# we must update the timestamps of everything that depends on these
# files.  Otherwise, altering a makefile will always rebuild the
# targets depending on the makefile.  Still, it helps avoid problems
# when developing.)
MAKEDEPENDS := Makefile $(wildcard $(MAKE_FILES)/*.mk)

# Start with the "all" target, so that it is the default.
# The various included files add to this.
all:

# PGXS
# This must come first, so that the initial variable values are those
# of PGXS.
include $(MAKE_FILES)/pgxs.mk

# Turn on inline documentation
include $(MAKE_FILES)/help.mk

# Make the docs
include $(MAKE_FILES)/docs.mk

# Make the SQL
include $(MAKE_FILES)/db.mk

# Make the files needed for extension packaging
$(PKG_TARGETS): % : %.m4 $(MAKEDEPENDS)
	m4 -D isok_version=${EXTVERSION} $< > $@

##
## ##################################################################
# This is here because help is displayed in include file order,
# starting with this file.  And having it appear here is more useful
##                         Deployment targets
##
# These are done by PGXS.

##   install
##       Invoke the PGXS installation process to auto-discover the
##       location where extension files go on your system and put the
##       necessary files where they belong.  (Do not first run `make`
##       without a target!  The "distribution" available from PGXN.org
##       already has everything built.  If you do run `make`, or `make
##       all`, you will then need to install GNU m4.)

##   installcheck
##       Invoke the regression tests. Requires the DISABLE_ROLE and
##       DISABLE_SEARCH_PATH variables (see below) to test when
##       features are disabled.  Running tests requires significant
##       PostgreSQL permissions.  See the installation instructions.
# We _could_ use the cache of disabled features instead of
# requiring the use of variables, but we don't.

##   uninstall
##       Remove the installed files

##
## ##################################################################
##                           Development
##
## Variables:
##     DISABLE_ROLE  Disable the ability to SET ROLE from
##                   ISOK_QUERIES.Role values. Set to any value to
##                   disable SET ROLE. (`make DISABLE_ROLE=Y all`)
##     DISABLE_SEARCH_PATH
##                   Disable the ability to SET search_path... from
##                   ISOK_QUERIES.Search_Path values. Set to any value
##                   to disable SET search_path....
##                   (`make DISABLE_SEARCH_PATH=Y all`)

##
## Targets:
# These targets are added to in the included make files.

##   help
##      Show this text

##   all
##      Build everything
# This is added to in various places
all: $(PKG_TARGETS)

##   clean
##      Delete those files that are generated, but not included
##      in the distribution

##   maintainer-clean
##      Delete all generated files

# Clean up generated packaging files.
.PHONY: maintainer-clean_isok
maintainer-clean_isok:
	rm -rf $(PKG_TARGETS)

maintainer-clean: maintainer-clean_isok

##
## ##################################################################
##                           Distribution
##
## Variables:
##     TARGET_DIR The directory in which to build and store the
##                PGXN distribution file.  This cannot be within
##                the directory which contains this Makefile.
##
## Targets:

##   dist
##       Create a file suitable for uploading to PGXN.org.
##       Uses the TARGET_DIR variable as the directory in which to
##       build and store the distribution file.
# It would be nice if we could use `git archive`, but there's all sorts
# of built files we want to include in the distribution.
.PHONY: dist
dist:
	[ -n "$(TARGET_DIR)" ] \
	  || { printf 'The TARGET_DIR variable must be set\n' >&2 ; \
	       exit 1 ; }
	[ ! -e "$(TARGET_DIR)/$(DIST_DIR)" ] \
	  || { printf '$(TARGET_DIR)/$(DIST_DIR): already exists\n' >&2 ; \
	       exit 1 ; }
	[ ! -e "$(TARGET_DIR)/$(DIST_DIR).zip" ] \
	  || { printf '$(TARGET_DIR)/$(DIST_DIR).zip: already exists\n' >&2 ; \
	       exit 1 ; }
	export GIT_DIR=$$(pwd) ; \
	cd "$(TARGET_DIR)" ; \
	git clone "$${GIT_DIR}" "$(DIST_DIR)" ; \
	cd "$(DIST_DIR)" ; \
	make all ; \
	make clean ; \
	rm -rf .git ; \
	cd .. ; \
	zip -r "$(DIST_DIR).zip" "$(DIST_DIR)" ; \
	rm -rf "$(DIST_DIR)"
