# fw-context — install, update, uninstall
#
# Quick start:
#   git clone git@github.com:turbyho/fw-context-mcp.git ~/.fw-context/src
#   cd ~/.fw-context/src && make install
#
# Update:
#   cd ~/.fw-context/src && make update
#
# Remove:
#   cd ~/.fw-context/src && make uninstall

# Auto-detect source directory (the directory containing this Makefile)
SRC       := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
VENV      ?= $(HOME)/.fw-context/.venv
PYTHON    ?= python3
# Is uv USABLE — not "is there a file named uv on PATH".
#
# pyenv and asdf put a shim on PATH for every tool ever installed under ANY
# interpreter they manage, so `command -v uv` finds one whether or not the
# ACTIVE interpreter has uv.  Measured here: `command -v uv` gave
# ~/.pyenv/shims/uv, and running it printed "pyenv: uv: command not found"
# and exited 127.  The detection saw a file, took the uv branch, and
# `make install` died at pip-install — with a message about pyenv that named
# neither uv nor this Makefile.
#
# So ask the binary to run.  A shim that dispatches correctly passes; a shim
# that cannot does not.
UV := $(shell uv --version >/dev/null 2>&1 && command -v uv)

# Every target below installs into $(VENV) and NEVER through a bare `pip`.
# A bare `pip` is a shim too — it resolves to ~/.pyenv/shims/pip here, which
# installs into whichever interpreter pyenv has active.  $(VENV)/bin/python
# -m pip cannot be redirected that way.

# ---- install ----
install: venv pip-install link-add clean-path
	@# The clang compiler headers of the libclang version: the wheel lacks the ARM ones.
	@# Not fatal — `fw-context doctor` reports it, and `--fix` repairs it later.
	@$(VENV)/bin/fw-context doctor --fix --only clang-resource || true
	@echo ""
	@echo "fw-context installed."
	@echo "  fw-context init"
	@echo ""

# ---- update ----
# Pull, then run the ONE installer.  This target used to carry its own copy
# of the install command and the copy had drifted three ways: it called a
# bare `pip`, it passed `--reinstall`, which is a flag of uv and not of pip,
# and neither branch passed `-e`.  So a successful `make update` replaced the
# editable install with a copy and put back the trap that e62efc5 removed —
# a test could then pass against code that no longer existed.
update:
	@echo "Updating fw-context..."
	@cd $(SRC) && git pull
	@$(MAKE) --no-print-directory pip-install
	@echo "fw-context updated."

# ---- uninstall ----
uninstall:
	@echo "Removing symlinks from ~/.local/bin ..."
	@rm -f $(HOME)/.local/bin/fw-context $(HOME)/.local/bin/fw-context-mcp
	@echo "Removing ~/.fw-context ..."
	@rm -rf $(HOME)/.fw-context
	@echo "fw-context removed."

# ---- venv ----
venv:
ifeq ($(UV),)
	@test -d $(VENV) || $(PYTHON) -m venv $(VENV)
else
	@test -d $(VENV) || $(UV) venv $(VENV) --python $(PYTHON) --seed
endif

# ---- pip install into venv ----
# Editable (-e), and that is not a convenience.  The test and lint targets
# below run out of $(VENV), so a copied install made them measure whatever
# the last `make install` had put there — a source change that was not
# reinstalled was invisible, and a test could pass against code that no
# longer existed.  Editable removes the step and the trap with it.
#
# CI already installs with `pip install -e ".[dev]"` (.github/workflows/
# test.yml), thus this also stops local runs and CI from disagreeing.
#
# The price: `fw-context` on the PATH follows the working tree.  A process
# that already runs keeps the modules it imported, so an edit reaches it on
# the next start, not mid-session.
pip-install: venv
ifeq ($(UV),)
	$(VENV)/bin/python -m pip install -e $(SRC)
else
	$(UV) pip install --python $(VENV)/bin/python -e $(SRC)
endif

# ---- symlink binaries into ~/.local/bin ----
link-add:
	@mkdir -p $(HOME)/.local/bin
	@for bin in $(VENV)/bin/fw-context $(VENV)/bin/fw-context-mcp; do \
		name=$$(basename "$$bin"); \
		link="$(HOME)/.local/bin/$$name"; \
		if [ -L "$$link" ] || [ -e "$$link" ]; then \
			rm -f "$$link"; \
		fi; \
		ln -s "$$bin" "$$link"; \
		echo "  $$link -> $$bin"; \
	done

# ---- remove old PATH entries from shell rc files ----
# No `sed -i`: GNU sed and the BSD sed of macOS read its argument in two
# ways (BSD sed takes the script as the suffix of a backup), a suffix
# (`-i.bak`) would overwrite a backup of the user, and both replace a
# symlinked rc file with a regular file.  sed writes to a temporary file, and
# `cat >` writes the result back into the rc file itself, thus a symlink and
# the permissions stay.  The message comes only when a line went away.
clean-path:
	@for rc in $(HOME)/.zshrc $(HOME)/.bashrc; do \
		if [ -f "$$rc" ] && grep -q '.fw-context/.venv/bin' "$$rc" 2>/dev/null; then \
			tmp=$$(mktemp) || exit 1; \
			if sed '\|export PATH="$$HOME/.fw-context/.venv/bin:$$PATH"|d' "$$rc" > "$$tmp" \
				&& ! cmp -s "$$rc" "$$tmp" && cat "$$tmp" > "$$rc"; then \
				echo "  Removed old PATH entry from $$rc"; \
			fi; \
			rm -f "$$tmp"; \
		fi; \
	done


# ---- dev setup (install all deps including pytest, ruff, mypy, bandit) ----
# Adds the dev tools to the SAME venv every other target uses.  It ran
# `uv sync --extra dev` before, and uv sync manages the PROJECT environment
# — <repo>/.venv — while test, lint and lint-security run out of $(VENV).
# So `make dev` equipped one venv and `make test` used another, which is
# what produced the second venv in the first place.
dev: venv
ifeq ($(UV),)
	$(VENV)/bin/python -m pip install -e "$(SRC)[dev]"
else
	$(UV) pip install --python $(VENV)/bin/python -e "$(SRC)[dev]"
endif
	@echo ""
	@echo "Dev environment ready. Run: make test, make lint, make lint-security"

# ---- security scan ----
lint-security:
	$(VENV)/bin/bandit -r src/ -c pyproject.toml

# ---- test targets ----
# The embedded SDKs of the tests (tests/sdk/versions.toml): Zephyr, the
# Zephyr SDK and the PlatformIO packages, in ~/.fw-context-test-sdk
# (C:\fwctx-sdk on Windows, or $FW_CONTEXT_TEST_SDK).  The tests compile
# their fixtures with these toolchains.  The global PlatformIO directory of the user does not change.
test-sdk:
	$(VENV)/bin/python -m tests.sdk.provision

# The extended set of the test SDKs too (tests/sdk/versions.toml, [extra]):
# the PlatformIO projects of ESP32, Nucleo L476RG and Uno, ESP-IDF, and
# Arduino CLI with its AVR core.  About 6 GB more.
test-sdk-platforms:
	$(VENV)/bin/python -m tests.sdk.provision --extra

# fast subset: leaves out the tests of the markers ollama, libclang, slow,
# sdk_project (a build and an index run of a native project of an SDK),
# extra_platforms (the same over the extended set), and the markers that
# addopts leaves out: system, live (the indexes of this machine) and
# litellm (a LiteLLM proxy).  The -m here replaces the -m of addopts in
# pyproject.toml, thus it names each marker of the two lists.
# The pre-test of the session (tests/_environment.py) stops the run when a
# component that the selected tests need is missing (clang, make, the C
# headers, the test SDKs); it names the repair.
test:
	$(VENV)/bin/pytest tests/ -q -m "not system and not live and not litellm and not ollama and not libclang and not slow and not sdk_project and not extra_platforms"

lint:
	$(VENV)/bin/ruff check src/
	$(VENV)/bin/mypy src/

# all tests except system, slow, live, litellm and extra_platforms (each requires manual invocation)
test-all:
	$(VENV)/bin/pytest tests/ -q

# the native projects of the extended set of the test SDKs (marker
# extra_platforms): a build and an index run of each, minutes each.
test-platforms:
	$(VENV)/bin/pytest tests/ -q -m "extra_platforms"

# slow tests only — full indexing runs, takes 30+ minutes
test-slow:
	$(VENV)/bin/pytest tests/ -q -m "slow"

# functional tests over the indexes that THIS machine holds.  They depend
# on local state, thus they stay out of test-all.  With no indexed project,
# or an index that a tool refuses, they fail.
test-live:
	$(VENV)/bin/pytest tests/ -q -m "live"

.PHONY: install update uninstall venv pip-install link-add clean-path dev test-sdk test-sdk-platforms test lint lint-security test-all test-platforms test-slow test-live
