# OxygenFlow on Earth (docs/m0.md §6): build the runner and the guest init,
# install them under /home/claude/flow, re-pin flow-init in flow.toml, and
# restart the flow-runner service.

EARTH ?= claude@earth
SSH ?= ssh -i $(HOME)/.ssh/earth_claude
SCP ?= scp -i $(HOME)/.ssh/earth_claude
FLOW_HOME_ON_EARTH ?= /home/claude/flow
INIT := target/x86_64-unknown-linux-gnu/release/flow-init

.PHONY: build deploy-runner

build:
	cargo build --release -p flow
	RUSTFLAGS="-C target-feature=+crt-static" cargo build --release -p flow-init --target x86_64-unknown-linux-gnu

deploy-runner: build
	$(SCP) target/release/flow $(EARTH):$(FLOW_HOME_ON_EARTH)/bin/flow.new
	$(SCP) $(INIT) $(EARTH):$(FLOW_HOME_ON_EARTH)/bin/flow-init.new
	$(SSH) $(EARTH) 'set -e; cd $(FLOW_HOME_ON_EARTH); \
		cp -p bin/flow bin/flow.prev; cp -p bin/flow-init bin/flow-init.prev; cp -p flow.toml flow.toml.prev; \
		mv bin/flow.new bin/flow; mv bin/flow-init.new bin/flow-init; \
		h=$$(bin/flow hash bin/flow-init | cut -d" " -f1); \
		sed -i "s|^init = .*|init = { path = \"bin/flow-init\", blake3 = \"$$h\" }|" flow.toml; \
		sudo systemctl restart flow-runner; systemctl is-active flow-runner'

# Move the pinned oxygen (deps/oxygen) to its main on Earth; the next commit
# records the new pin. Push oxygen first.
.PHONY: pins
pins:
	cd deps/oxygen && oxy --plain pull origin && oxy --plain checkout
