# Phoebus OS developer targets.
#
#   make help        list targets
#   make test        run both test suites
#   make check       fmt + clippy + tests
#   make commit MSG="feat(rust): ..."
#   make publish     push to the configured remote

SHELL := /bin/bash

# The release tool produced by `make build`, used by `make release-gates`.
RELEASE ?= installer-rs/target/release/phoebus-release

.PHONY: help context doctor test test-rust test-vm ci opencode-acceptance fmt fmt-check clippy check build static release-gates vm hooks commit publish backup forgejo-init public private protect release clean

help:
	@printf 'Phoebus OS targets:\n'
	@printf '  make context     session briefing (state, roadmap, handoff)\n'
	@printf '  make doctor      check host prerequisites\n'
	@printf '  make test        run the Rust and VM-harness test suites\n'
	@printf '  make test-rust   cargo test --workspace\n'
	@printf '  make test-vm     python3 installer/tests/vm/test_vmsafety.py\n'
	@printf '  make ci          every CI quality gate (fmt, clippy, tests, builds)\n'
	@printf '  make release-gates  run the executable release gates (evidence report)\n'
	@printf '  make opencode-acceptance  live-model acceptance (needs PHOEBUS_OPENCODE_LIVE_*)\n'
	@printf '  make fmt         cargo fmt --all\n'
	@printf '  make check       fmt-check + clippy + tests\n'
	@printf '  make build       release Rust installer\n'
	@printf '  make static      static Rust installer (live environment)\n'
	@printf '  make hooks       enable the committed git hooks\n'
	@printf '  make commit MSG="..."  stage, check and commit\n'
	@printf '  make publish     push to the configured remote\n'
	@printf '  make forgejo-init  store the API token and make the repo public\n'
	@printf '  make public      make the Forgejo repository public\n'
	@printf '  make protect     protect main (block force-push/delete)\n'
	@printf '  make release TAG=v0.2.0  create a Forgejo release\n'
	@printf '  make backup      bundle the whole repo (all refs)\n'

context:
	scripts/context.sh

doctor:
	scripts/doctor.sh

test: test-rust test-vm

test-rust:
	cd installer-rs && cargo test --workspace --locked

test-vm:
	python3 installer/tests/vm/test_vmsafety.py
	python3 installer/tests/vm/test_rollback_acceptance.py

# The complete set of required CI gates. Every target here must actually run;
# the workflow fails the job if a prerequisite is missing rather than skipping.
ci: fmt-check clippy test-rust test-vm build static

# A real-model acceptance run. Fails (BLOCKED) when the live server is not
# configured, so it can never pass by silently not running.
opencode-acceptance:
	@if [ -z "$$PHOEBUS_OPENCODE_LIVE_URL" ] || [ -z "$$PHOEBUS_OPENCODE_LIVE_PROVIDER" ] || [ -z "$$PHOEBUS_OPENCODE_LIVE_MODEL" ]; then \
		echo "BLOCKED: set PHOEBUS_OPENCODE_LIVE_URL, PHOEBUS_OPENCODE_LIVE_PROVIDER and PHOEBUS_OPENCODE_LIVE_MODEL"; \
		exit 2; \
	fi
	cd installer-rs && PHOEBUS_OPENCODE_REQUIRE_LIVE=1 \
		cargo test -p phoebus-executor --test opencode_live --locked -- --nocapture

fmt:
	cd installer-rs && cargo fmt --all

fmt-check:
	cd installer-rs && cargo fmt --all -- --check

clippy:
	cd installer-rs && cargo clippy --workspace --all-targets --locked

check: fmt-check clippy test

build:
	cd installer-rs && cargo build --release -p phoebus-installer --locked

static:
	cd installer-rs && RUSTFLAGS="-C target-feature=+crt-static" \
		cargo build --release -p phoebus-installer --target x86_64-unknown-linux-gnu --locked

# Run the executable release gates and write an evidence-bound report. Host
# gates run here; VM/seat/model gates require their environment and are
# recorded as blocked, never silently passed.
release-gates: build
	$(RELEASE) gates --execute --output gate-report.json

hooks:
	scripts/setup-hooks.sh

commit:
	@if [ -z "$(MSG)" ]; then echo 'usage: make commit MSG="feat(...): ..."'; exit 2; fi
	scripts/agent-commit.sh "$(MSG)"

publish:
	scripts/publish.sh

backup:
	scripts/backup-git.sh

forgejo-init:
	scripts/forgejo.sh init

public:
	scripts/forgejo.sh set-public

private:
	scripts/forgejo.sh set-private

protect:
	scripts/forgejo.sh protect

release:
	@if [ -z "$(TAG)" ]; then echo 'usage: make release TAG=v0.2.0'; exit 2; fi
	scripts/forgejo.sh create-release "$(TAG)" --name "$(TAG)"

clean:
	cd installer-rs && cargo clean
