# 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
# bash file, and that file holds the variants:
#
#   make cluster up      ->  ctrl/cluster.sh up
#   make newbox destroy  ->  ctrl/newbox.sh destroy
#
# Config layers, weakest first: ctrl/versions.env (pinned toolchain) <
# ctrl/env.d/<profile>.env (cluster shape) < ctrl/.env (local, gitignored) <
# the environment. So `make cluster up PROFILE=client` beats everything.
#
# Start with:  make setup    (then: make cluster up && make docs)

# Identity follows the FOLDER NAME, so this directory can be copied elsewhere,
# renamed, and run as a separate environment with no edits. ctrl/.env overrides
# it when you want a name that differs from the directory.
SLUG      := $(shell echo '$(notdir $(CURDIR))' | tr '[:upper:]' '[:lower:]' | tr -c 'a-z0-9-' '-' | sed 's/^-*//; s/-*$$//')
CLUSTER   := $(or $(shell sed -n 's/^CLUSTER=//p' ctrl/.env 2>/dev/null),$(SLUG))
KCTX      := --context kind-$(CLUSTER)
TILT_PORT := $(shell sed -n 's/^TILT_PORT=//p' ctrl/.env 2>/dev/null)
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. `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

.PHONY: help setup check mem deps deps-image cluster registry addons ports \
        newbox dockerhost docs tilt \
        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

# ── setup ──────────────────────────────────────────────────────────────────

setup:                         ## prepare this machine  [core] [--share-docker] [--cluster]
	bash ctrl/setup.sh $(ARGS)

check:                         ## is this machine ready? reports, never fixes
	bash ctrl/check.sh

mem:                           ## memory, and any cap holding it  [status|backup|restore]
	bash ctrl/mem.sh $(or $(ARGS),status)

deps:                          ## install the toolchain  [core|dev]  (default dev)
	bash ctrl/deps.sh install $(or $(ARGS),dev)

deps-image:                    ## build the installer image  [full]
	docker build -f ctrl/Dockerfile.deps \
	  --target $(if $(filter full,$(ARGS)),deps-full,deps) \
	  -t $(DEPSIMG):$(if $(filter full,$(ARGS)),full,deps) .

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

cluster:                       ## this env + the machine  [up|down|reset|list|free]
	bash ctrl/cluster.sh $(or $(ARGS),up)

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

addons:                        ## profile addons  [install|list]       (default list)
	bash ctrl/addons.sh $(or $(ARGS),list)

ports:                         ## this environment's port block  [show|persist]
	bash ctrl/ports.sh $(or $(ARGS),show)

# ── host ───────────────────────────────────────────────────────────────────

newbox:                        ## throwaway environment  [create|status|shell|destroy]
	bash ctrl/newbox.sh $(or $(ARGS),status)

dockerhost:                    ## share Docker between distros  [status|share|unshare]
	$(if $(filter share unshare,$(ARGS)),sudo ,)bash ctrl/dockerhost.sh $(or $(ARGS),status)

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

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

# --port is only passed when TILT_PORT is actually set. It comes from ctrl/.env,
# which does NOT carry it by default — ports are derived at runtime in
# lib/config.sh unless `make ports persist` has written them. Without the guard
# tilt receives a bare `--port` with no value and fails on the flag rather than
# on anything real. `make ports show` prints the derived block.
tilt:                          ## dev loop  [up|down]                  (default up)
	cd ctrl && tilt $(or $(ARGS),up) $(KCTX) $(if $(filter down,$(ARGS)),,$(if $(TILT_PORT),--port $(TILT_PORT)))

# ── the shape every other project uses ─────────────────────────────────────
# Aliases, not a second implementation: each one calls the same script the
# canonical target does.
#
# The header above argues for `make cluster down` over `make cluster-down`, and
# that still holds *within* this file. But rig is one repo among several on the
# same machine, and every other one answers to kind-up / tilt-up. Muscle memory
# spanning six projects beats internal tidiness in one, so both spellings work.
#
# `cluster list` and `cluster free` have no hyphenated twin on purpose — they
# are rig's own, with nothing to be consistent with.

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, but rig has no Tiltfile — there
# is nothing to run yet, and they fail the same way `make tilt` does.
tilt-up:                       ## alias for `tilt up`   (rig has no Tiltfile yet)
	cd ctrl && tilt up $(KCTX) $(if $(TILT_PORT),--port $(TILT_PORT))

tilt-down:                     ## alias for `tilt down` (rig has no Tiltfile yet)
	cd ctrl && tilt down $(KCTX)
