# Thin control Makefile: the subcommand is an argument (`make cluster down`); logic lives in ctrl/ scripts.
#   make check | deps | cluster up | tilt | docs     (`make help` lists all)
# Start with:  make check && make deps && make cluster up
# Notes: docs/notes/Makefile.md

# Identity and ports, asked once of ctrl/ports.sh, read positionally (selftest pins the order):
#   CLUSTER KUBECONTEXT HTTP HTTPS TILT REGISTRY MANIFESTS_DIR
FACTS     := $(shell bash ctrl/ports.sh active 2>/dev/null)
SLUG      := $(shell echo '$(notdir $(CURDIR))' | tr '[:upper:]' '[:lower:]' | tr -c 'a-z0-9-' '-' | sed 's/^-*//; s/-*$$//')
# Fall back to the folder name, not empty, if ports.sh fails on a broken config.
CLUSTER   := $(or $(word 1,$(FACTS)),$(SLUG))
KCTX      := --context $(or $(word 2,$(FACTS)),kind-$(SLUG))
TILT_PORT := $(word 5,$(FACTS))
DEPSIMG   := $(SLUG)-deps

# 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),)
$(eval $(ARGS):;@:)
# ...and as PHONY, because some of those words name real directories (ctrl, docs, ...).
.PHONY: $(ARGS)
endif

.PHONY: help check deps cluster tilt docs selftest standalone \
        kind-up kind-down kind-reset tilt-up tilt-down

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

# ── this machine ───────────────────────────────────────────────────────────

# Everything that looks and never changes anything: host, docker, toolchain,
# config, memory, ports, registry, addons. `check mem` goes deeper on memory —
# how far it really climbs, and the WSL .wslconfig backup/restore.
check:                         ## is this machine ready?  [all] [mem [status|push|all|backup|restore]]
	bash ctrl/check.sh $(ARGS)

# `deps image` is for a machine with nothing but Docker: the installer runs from
# the image instead — see BOOTSTRAP.md. `full` bakes every binary in.
deps:                          ## install the toolchain  [core|dev]  [image [full]]
ifeq ($(word 1,$(ARGS)),image)
	docker build -f ctrl/Dockerfile.deps \
	  --target $(if $(filter full,$(ARGS)),deps-full,deps) \
	  -t $(DEPSIMG):$(if $(filter full,$(ARGS)),full,deps) .
else
	bash ctrl/deps.sh install $(or $(ARGS),dev)
endif

# ── the cluster ────────────────────────────────────────────────────────────

# up also starts the registry and installs the profile's addons, and the ports
# derive from the folder name — there is nothing else to run first.
cluster:                       ## this env + the machine  [up|down|reset|list|free]
	bash ctrl/cluster.sh $(or $(ARGS),up)

# ── dev loop + docs ────────────────────────────────────────────────────────

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

# --port only when TILT_PORT resolved; the Tiltfile asks ports.sh for the rest itself.
tilt:                          ## dev loop  [up|down]                  (default up)
	cd ctrl && tilt $(or $(ARGS),up) $(KCTX) $(if $(filter down,$(ARGS)),,$(if $(TILT_PORT),--port $(TILT_PORT)))

# ── maintaining rig ────────────────────────────────────────────────────────

# The counterpart to check: that one asks about the MACHINE and never fails,
# this one asks about RIG and exits 1. Checks are written as the decisions they
# defend, so a failure names what is being undone.
selftest:                      ## does rig still do what it says? exits 1 if not
	bash ctrl/selftest.sh

# The one-file versions of rig's tools, one folder per profile, for machines the
# full rig is not going to. Generated from rig as it is, never edited by hand;
# `check` is what selftest runs to catch a kit left behind by a change to rig.
standalone:                    ## single-file kits  [write|check|export DIR]  (default write)
	bash ctrl/standalone.sh $(or $(ARGS),write)

# ── the shape every other project uses ─────────────────────────────────────
# Aliases matching other projects' kind-up / tilt-up; each calls the same script.
# Nothing else reads these names: rename, delete or add freely (and update .PHONY).

kind-up:                       ## alias for `cluster up`
	bash ctrl/cluster.sh up

kind-down:                     ## alias for `cluster down`
	bash ctrl/cluster.sh down

kind-reset:                    ## alias for `cluster reset`
	bash ctrl/cluster.sh reset

# These two match the other projects' spelling. rig ships ctrl/Tiltfile, so they
# run — it deploys the examples in k8s/base until you replace them.
tilt-up:                       ## alias for `tilt up`
	cd ctrl && tilt up $(KCTX) $(if $(TILT_PORT),--port $(TILT_PORT))

tilt-down:                     ## alias for `tilt down`
	cd ctrl && tilt down $(KCTX)
