# torly - a TinyGo Nostr relay.
#
# Build uses TinyGo; the standard Go toolchain is still required by TinyGo for
# its standard library sources (TinyGo shells out to `go`). Both are located
# through PATH; the fallback below provisions this checkout's local toolchain.

TINYGO ?= tinygo
GO     ?= go

# The host shell may export a GOROOT that does not contain a Go toolchain.
# Prefer a working one, else the local install used to bootstrap this tree.
ifeq ($(wildcard $(GOROOT)/bin/go),)
  ifneq ($(wildcard $(HOME)/.local/goroot/bin/go),)
    GOROOT := $(HOME)/.local/goroot
  endif
endif
export GOROOT
export PATH := $(GOROOT)/bin:$(PATH)

BIN     ?= torly
PKG     := ./cmd/torly
PORT    ?= 3334
DATA    ?= ./data
STATIC  ?= ./web/static

.PHONY: all build build-go test test-tinygo e2e check vet fmt run clean size help

all: build

## build: compile the relay with TinyGo
build:
	$(TINYGO) build -o $(BIN) $(PKG)

## build-go: compile with the standard Go toolchain (for debugging/tests)
build-go:
	$(GO) build -o $(BIN)-go $(PKG)

## test: run the unit tests with the standard Go toolchain
test:
	$(GO) test ./...

## test-tinygo: run the unit tests under TinyGo
test-tinygo:
	$(TINYGO) test ./...

## e2e: build and drive the relay with the real nak client
e2e: build
	./scripts/e2e.sh

## check: vet + test
check: vet test

vet:
	$(GO) vet ./...

fmt:
	$(GO) fmt ./...

## run: build and serve on PORT with DATA
run: build
	TORLY_PORT=$(PORT) TORLY_DATA_DIR=$(DATA) TORLY_STATIC_DIR=$(STATIC) ./$(BIN) serve

## size: show the binary size breakdown
size: build
	ls -lh $(BIN)
	@command -v size >/dev/null && size $(BIN) || true

clean:
	rm -f $(BIN) $(BIN)-go

help:
	@grep -E '^## ' $(MAKEFILE_LIST) | sed 's/## /  /'
