# Thin control Makefile — one target per ctrl/ script, and the subcommand is an
# argument rather than a second target: `make cluster down`, not `make cluster-down`.
#
# The logic lives in the scripts, never here. Each target maps to exactly one
# file, and that file holds the variants:
#
#   make cluster up      ->  ctrl/cluster.sh up
#   make build amar      ->  ctrl/build.sh amar
#
# Bare words pass straight through. Anything starting with a dash would be
# swallowed by make itself, so pass those via ARGS instead:
#
#   make component ARGS="publish soleprint-ui /tmp/out --dist"
#   make deploy    ARGS="--build"
#
# Every script stays runnable on its own (./ctrl/kind-up.sh still works, and each
# built room keeps its own gen/<room>/ctrl/*.sh) — the standalone rule holds, and
# this only saves typing.
#
# Start with:  make build && make start

# The room to act on when none is named. Rooms live in cfg/ and build into gen/.
ROOM   ?= standalone
PYTHON ?= python3
export PYTHON

# Words after the target become the script's subcommand. Make would otherwise
# treat them as goals of their own, so each gets a no-op rule.
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
ifneq ($(ARGS),)
# Declares each extra word as a target that does nothing: `:` is an empty rule
# body and `@` silences it. Without this, `make build sample` runs the build and
# then fails with "No rule to make target 'sample'", because make reads every
# word on the line as something it has been asked to build.
$(eval $(ARGS):;@:)
# ...and as PHONY, because some of those words name real directories. `cfg`,
# `ctrl`, `docs`, `gen` and `init` all exist at this level, and make considers a
# target that is an existing directory already built — so `make build ctrl` ran
# the build and then printed "make: 'ctrl' is up to date". The empty rule above
# is not enough on its own; only .PHONY stops make consulting the filesystem.
.PHONY: $(ARGS)
endif

.DEFAULT_GOAL := help
.PHONY: help build start stop dist docs cluster deploy component

help:                          ## list targets
	@grep -hE '^[a-z]+:.*?##' $(MAKEFILE_LIST) | sed 's/:.*##/\t/' | expand -t16

# ── rooms ──────────────────────────────────────────────────────────────────

build:                         ## build a room into gen/  [<room>|all|models]
	bash ctrl/build.sh $(or $(ARGS),$(ROOM))

start:                         ## run a built room  [<room>] [-d] [--build]
	bash ctrl/start.sh $(or $(ARGS),$(ROOM))

stop:                          ## stop a running room  [<room>]
	bash ctrl/stop.sh $(or $(ARGS),$(ROOM))

dist:                          ## compile the plexus UIs to single files  [<room>]
	bash ctrl/dist.sh $(or $(ARGS),$(ROOM))

# ── docs ───────────────────────────────────────────────────────────────────

docs:                          ## documentation  [serve [port]|graphs [theme]]  (default serve)
	bash ctrl/docs.sh $(or $(ARGS),serve)

# ── cluster ────────────────────────────────────────────────────────────────

cluster:                       ## shared kind cluster  [up|down|status]  (default status)
	bash ctrl/cluster.sh $(or $(ARGS),status)

# ── distribution ───────────────────────────────────────────────────────────

component:                     ## publish components  [list|sync|watch|publish|diff]
	$(PYTHON) ctrl/spr.py $(or $(ARGS),list)

deploy:                        ## push standalone to the server  [--build|--sync-only]
	bash ctrl/deploy.sh $(ARGS)
