PREFIX  ?= $(HOME)/.local/bin
TARGET  := $(PREFIX)/CodeMapper
SIG     := CODEMAPPER-SIGNATURE-izackp

.PHONY: build install uninstall _check_existing

build:
	swift build -c release

_check_existing:
	@if [ -f "$(TARGET)" ]; then \
		GOT=$$("$(TARGET)" --signature 2>/dev/null || true); \
		if [ "$$GOT" != "$(SIG)" ]; then \
			if [ "$(FORCE)" != "1" ]; then \
				echo "ERROR: $(TARGET) exists but is not this CodeMapper build."; \
				echo "       Pass FORCE=1 to override."; \
				exit 1; \
			fi; \
		fi \
	fi

install: build _check_existing
	install -d $(PREFIX)
	install .build/release/CodeMapper $(TARGET)

uninstall: _check_existing
	rm -f $(TARGET)
