# morly - the ORLY Nostr relay framework, the server side of musiquay.
#
# The web app lives in ../musiquay; this repo is the native relay that serves
# it (static files, /blossom/ blobs, /proxy/ media and the Nostr WebSocket
# endpoint) and that musiquay's protocols are added to.

MOXIE    ?= $(CURDIR)/../moxie/moxie
# An inherited MOXIEROOT is ignored on purpose: `?=` cannot tell an exported
# stale root from an intended one, and a stale root silently compiles a
# different tree's src/ and pkg/. Pass it on the command line to build against
# another tree: make MOXIEROOT=/path/to/moxie ...
ifeq ($(origin MOXIEROOT),environment)
MOXIEROOT := $(CURDIR)/../moxie
endif
MOXIEROOT ?= $(CURDIR)/../moxie

# Moxie closures do not capture: a closure receives what it needs through its
# parameters. The relay predates that enforcement in places, so the build opts
# out explicitly.
CAPTURES ?= -allow-closure-captures

# === Relay ===
build:
	MOXIEROOT=$(MOXIEROOT) $(MOXIE) build $(CAPTURES) -o morly .

build-test-relay:
	MOXIEROOT=$(MOXIEROOT) $(MOXIE) build $(CAPTURES) -o morly-test .

# Run the relay against a frontend build (defaults to the musiquay web app).
STATIC ?= $(CURDIR)/../musiquay/web/static
DATA   ?= $(CURDIR)/data
PORT   ?= 3334
serve: build
	ORLY_STATIC_DIR=$(STATIC) ORLY_DATA_DIR=$(DATA) \
	ORLY_LISTEN=127.0.0.1 ORLY_PORT=$(PORT) ./morly relay

# === Test ===
# Every package carrying a *_test.mx, through the compiler's own harness.
UNIT_SKIP ?=
test-unit:
	@set -e ; for d in $$( { [ -f main_test.mx ] && echo . ; find pkg -name '*_test.mx' | xargs -n1 dirname ; } | sort -u ) ; do \
		case " $(UNIT_SKIP) " in *" $$d "*) echo "=== unit: $$d (skipped)" ; continue ;; esac ; \
		echo "=== unit: $$d" ; \
		MOXIEROOT=$(MOXIEROOT) $(MOXIE) test $(CAPTURES) ./$$d || exit 1 ; \
	done

test: test-unit

.PHONY: build build-test-relay serve test-unit test
