EXTENSION = pg_lease
EXTVERSION = 0.1.0

DATA = sql/$(EXTENSION)--$(EXTVERSION).sql

REGRESS = $(EXTENSION)
REGRESS_OPTS = --inputdir=test --outputdir=test

ISOLATION = compete races stale-client rollback-race independent-keys
ISOLATION_OPTS = --inputdir=test --outputdir=test

# check-wait/check-failure must not shadow PGXS's default `all` target.
all:
	@true

# Blocking-acquire wake (release during wait) is not expressible in
# isolationtester — the waiter sleeps on a timer, not a lock — so it runs
# as a two-session script instead.
check-wait: install
	sh test/wait-wake.sh

# Failure-model suite (stage 11): abort, cancellation, connection loss,
# stale ownership, reconnect, restart, crash, downtime lapse. Uses a
# throwaway PostgreSQL instance for restart scenarios.
check-failure: install
	sh test/failure-model.sh

# Source distribution: exactly the release files (docs, extension SQL,
# tests and their expected outputs). Development-only files (test/results,
# host-only tooling, internal audit/agent docs) are excluded. Output:
# dist/pg_lease-$(EXTVERSION).tar.gz
DIST_NAME = $(EXTENSION)-$(EXTVERSION)
DIST_FILES = \
	pg_lease.control \
	Makefile \
	README.md \
	CHANGELOG.md \
	META.json \
	sql/$(EXTENSION)--$(EXTVERSION).sql \
	specs/SEMANTICS.md \
	specs/INVARIANTS.md \
	specs/FAILURE-MODEL.md \
	test/sql/pg_lease.sql \
	$(wildcard test/expected/*.out) \
	test/INVARIANT-MAP.md \
	$(patsubst %,test/specs/%.spec,$(ISOLATION)) \
	test/failure-model.sh \
	test/wait-wake.sh

.PHONY: dist
dist:
	@mkdir -p dist/$(DIST_NAME)
	@for f in $(DIST_FILES); do \
		mkdir -p dist/$(DIST_NAME)/$$(dirname $$f); \
		cp $$f dist/$(DIST_NAME)/$$f || exit 1; \
	done
	cp ../../LICENSE dist/$(DIST_NAME)/LICENSE
	COPYFILE_DISABLE=1 tar -czf dist/$(DIST_NAME).tar.gz -C dist $(DIST_NAME)
	@rm -rf dist/$(DIST_NAME)
	@echo "dist/$(DIST_NAME).tar.gz"

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