# pg_someone — PGXS build
MODULE_big = pg_someone
OBJS = \
	src/pg_someone.o \
	src/hooks.o \
	src/voice.o \
	src/memory.o \
	src/phantom.o

EXTENSION = pg_someone
DATA = $(wildcard sql/pg_someone--*.sql)
PGFILEDESC = "pg_someone - there is someone else in your database"

# pg_regress starts its own throwaway instance with the module preloaded.
REGRESS = $(patsubst test/sql/%.sql,%,$(sort $(wildcard test/sql/*.sql)))
REGRESS_OPTS = --inputdir=test --outputdir=test \
	--temp-instance=tmp_regress \
	--temp-config=test/regress.conf \
	--temp-config=test/stage.conf \
	--encoding=UTF8 --no-locale

PG_CONFIG ?= pg_config

# PostgreSQL 19.0 made post_parse_analyze_hook's JumbleState const; early
# 19devel snapshots did not. Same PG_VERSION_NUM, so ask the header.
ifneq ($(shell grep -sc 'const JumbleState' $(shell $(PG_CONFIG) --includedir-server)/parser/analyze.h),0)
PG_CPPFLAGS += -DSOMEONE_CONST_JSTATE
endif

PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)

$(OBJS): src/pg_someone.h src/compat.h

# Install into ./stage (no root needed) and write test/stage.conf, which points
# a server at it.
# extension_control_path only exists since PostgreSQL 18; before that, run
# `make install` first and stage.conf stays empty.
STAGE = $(CURDIR)/stage
ifeq ($(shell test "$(MAJORVERSION)" -ge 18 2>/dev/null && echo y),y)
stage: all
	$(MAKE) install DESTDIR=$(STAGE)
	echo "dynamic_library_path = '$(STAGE)$(pkglibdir):\$$libdir'" > test/stage.conf
	echo "extension_control_path = '$(STAGE)$(datadir):\$$system'" >> test/stage.conf
else
stage: all
	@echo "PostgreSQL $(MAJORVERSION): no extension_control_path, testing the installed copy (make install first)"
	: > test/stage.conf
endif

# `make check-local`: regression suite against a throwaway instance.
check-local: stage
	$(MAKE) installcheck

# `make check-upgrade`: pg_upgrade between two throwaway clusters.
check-upgrade: stage
	test/upgrade.sh '$(PG_CONFIG)' test/stage.conf

.PHONY: stage check-local check-upgrade

EXTRA_CLEAN = $(EXTENSION)-*.zip stage test/stage.conf test/results test/log test/regression.diffs test/regression.out tmp_regress

# Release archive for PGXN and GitHub: make dist -> pg_someone-X.Y.Z.zip
# Built from the committed tree (git archive), so commit before running it.
DIST_VERSION = $(shell sed -n 's/^   "version": "\(.*\)",/\1/p' META.json)
dist:
	git archive --format zip --prefix=$(EXTENSION)-$(DIST_VERSION)/ \
		-o $(EXTENSION)-$(DIST_VERSION).zip HEAD
.PHONY: dist

# The version lives in three places; keep them in sync.
check-version:
	@test "$(DIST_VERSION)" = "$$(sed -n "s/^default_version = '\(.*\)'/\1/p" $(EXTENSION).control)" || \
		{ echo "META.json and $(EXTENSION).control disagree on the version"; exit 1; }
	@test -f sql/$(EXTENSION)--$(DIST_VERSION).sql || \
		{ echo "sql/$(EXTENSION)--$(DIST_VERSION).sql is missing"; exit 1; }
	@grep -q '\.version = "$(DIST_VERSION)"' src/pg_someone.c || \
		{ echo "PG_MODULE_MAGIC_EXT version in src/pg_someone.c is not $(DIST_VERSION)"; exit 1; }
	@echo "version $(DIST_VERSION) is consistent"
.PHONY: check-version
