# brain: a confined Claude Code session with the box's channel, as a systemd user unit.
# Tools come from the flake: nix develop --command make test
#   make install   # workspace, channel, key, unit; then attach once to accept the prompts
#   make attach    # look in (detach: C-b d)

FLAKE = ..
WORK = $(HOME)/pdtv26-brain
CONF = $(HOME)/.config/pdtv26-brain
# The box's ask capability listens here: an interface name, or an address.
HOST = tailscale0
PORT = 8025
# The box, by name, and its keys for ir and look (made on the box; copy them here).
BOX = box
IR_URL = ws://$(BOX):8026/
LOOK_URL = ws://$(BOX):8028/
# Problem reports: issues on REPO, assigned to ASSIGNEE. The token is fine-grained: that repo, Issues read/write, nothing else.
REPO = owner/repo
ASSIGNEE = owner
UNIT = $(HOME)/.config/systemd/user/pdtv26-brain.service
SOCK = /run/user/$(shell id -u)/pdtv26-brain/tmux.sock
TMUX = $(WORK)/app/bin/tmux

.PHONY: help test app install attach status

## list targets
help:
	@awk -f ../make-help.awk $(MAKEFILE_LIST)

## the unit tests, in Nix's build sandbox
test:
	nix build -L --no-link ..#brain-js

## the channel and its unit, as a GC root in the workspace
app:
	mkdir -p $(WORK)
	nix build --out-link $(WORK)/app '$(FLAKE)#brain'

$(WORK)/CLAUDE.md: app
	install -m 644 $(WORK)/app/share/pdtv26-brain/CLAUDE.md $@

# Who's who: an example, once; then it's the household's to edit.
$(WORK)/household.md: | app
	install -m 644 $(WORK)/app/share/pdtv26-brain/household.md $@

# The channel listens on HOST only, and holds the box's ir and look capabilities and the report token.
$(WORK)/.mcp.json: Makefile
	printf '{"mcpServers":{"box":{"command":"%s","args":["$(HOST)","$(PORT)","%s","$(IR_URL)","%s","$(REPO)","%s","$(ASSIGNEE)","$(LOOK_URL)","%s"]}}}\n' \
	  $(WORK)/app/bin/brain-channel $(CONF)/ask-key $(CONF)/ir-key $(CONF)/report-token $(CONF)/look-key > $@

# Pre-approve the project's one MCP server and its tools; the trust and dev-channel prompts still want a person.
$(WORK)/.claude/settings.local.json: Makefile
	install -d $(dir $@)
	echo '{"enabledMcpjsonServers":["box"],"permissions":{"allow":["mcp__box__reply","mcp__box__say","mcp__box__ir_listen","mcp__box__ir_send","mcp__box__report_problem","mcp__box__look","Bash(python3 -I *)"]}}' > $@

## the box's key to ask: 160 random bits; the box needs a copy, in /etc/pdtv26/ask-key
$(CONF)/ask-key:
	install -d -m 700 $(dir $@)
	umask 077; head -c 20 /dev/urandom | base32 | tr A-Z a-z | tr -d = > $@

$(CONF)/ir-key $(CONF)/look-key:
	@echo "copy the box's /etc/pdtv26/$(notdir $@) to $@ (mode 600)" >&2; exit 1

$(CONF)/report-token:
	@echo "make a fine-grained token for $(REPO), Issues read/write only, and save it as $@ (mode 600)" >&2; exit 1

$(UNIT): app
	install -D -m 644 $(WORK)/app/share/pdtv26-brain/pdtv26-brain.service $@

## set up the workspace, channel, keys, and unit, and (re)start it
install: $(CONF)/report-token $(CONF)/ir-key $(CONF)/look-key $(CONF)/ask-key app $(WORK)/CLAUDE.md $(WORK)/household.md $(WORK)/.mcp.json $(WORK)/.claude/settings.local.json $(UNIT)
	install -d -m 700 $(WORK)/.config-claude $(WORK)/.cache
	systemctl --user daemon-reload
	systemctl --user enable pdtv26-brain
	systemctl --user restart pdtv26-brain
	@echo "first time: make attach; /login, trust the folder, approve the box server"

## look in on the session (detach: C-b d)
attach:
	$(TMUX) -S $(SOCK) attach

## what the session shows now
status:
	systemctl --user --no-pager status pdtv26-brain | head -5
	$(TMUX) -S $(SOCK) capture-pane -p | grep -v '^\s*$$' | tail -25
