.DEFAULT_GOAL := all

PREFIX ?= $(HOME)/.local
BINDIR := $(PREFIX)/bin
LIBDIR := $(PREFIX)/lib
CONFIG_DIR ?= $(HOME)/.config/ollie
DATA_DIR ?= $(HOME)/.local/share/ollie
BUILD_DIR := build
JOBS ?= $(shell nproc 2>/dev/null || echo 2)

.PHONY: all build go tools kde install install-data install-models test clean uninstall help

# Build everything, run tests, then install
all: build test install

# Build all components to $(BUILD_DIR), no install
build: go tools kde

# Go binaries: olliesrv, ollie-9p, toolsrv
go:
	mkdir -p $(BUILD_DIR)/bin $(BUILD_DIR)/tools
	go build -o $(BUILD_DIR)/bin/olliesrv ./cmd/olliesrv/
	go build -o $(BUILD_DIR)/bin/ollie-9p ./cmd/ollie-9p/
	go build -ldflags="-s -w" -o $(BUILD_DIR)/tools/toolsrv ./cmd/toolsrv/

# Compiled tools + script tools
tools: go
	go build -ldflags="-s -w" -o $(BUILD_DIR)/tools/codeintel ./tools/codeintel/
	go build -ldflags="-s -w" -o $(BUILD_DIR)/tools/filetools ./tools/filetools/
	go build -ldflags="-s -w" -o $(BUILD_DIR)/tools/lsptools ./tools/lsp/
	go build -ldflags="-s -w" -o $(BUILD_DIR)/tools/web_fetch ./tools/web/cmd/web_fetch/
	cd $(BUILD_DIR)/tools && for name in code_outline code_query code_rewrite code_symbols code_dependencies codebase_overview; do \
		ln -sf codeintel $$name; \
	done
	cd $(BUILD_DIR)/tools && for name in file_read file_write file_edit file_grep file_glob; do \
		ln -sf filetools $$name; \
	done
	cd $(BUILD_DIR)/tools && for name in lsp_hover lsp_definition lsp_references lsp_completion lsp_diagnostics lsp_symbols lsp_rename; do \
		ln -sf lsptools $$name; \
	done
	cp data/tools/*.meta $(BUILD_DIR)/tools/
	for f in data/tools/*; do [ -f "$$f" ] && [ -x "$$f" ] && cp "$$f" $(BUILD_DIR)/tools/; done; true
	cp -a data/tools/_lib $(BUILD_DIR)/tools/

# KDE components (KF6/Qt6, builds lib9p internally)
kde:
	@if cmake -S kde -B kde/build -DCMAKE_INSTALL_PREFIX=$(PREFIX) 2>/dev/null; then \
		cmake --build kde/build -j$(JOBS) || { echo "KDE build failed" >&2; exit 1; }; \
	else \
		echo "KDE dependencies not available; skipping KDE build"; \
	fi

# Run tests (does not install anything)
test:
	go test ./...

# Install everything from build dir
install: build install-data
	mkdir -p $(BINDIR) $(LIBDIR)
	install -m755 $(BUILD_DIR)/bin/olliesrv $(BINDIR)/olliesrv
	install -m755 $(BUILD_DIR)/bin/ollie-9p $(BINDIR)/ollie-9p
	mkdir -p $(CONFIG_DIR)/tools
	cp -a $(BUILD_DIR)/tools/. $(CONFIG_DIR)/tools/
	@if [ -d kde/build ] && cmake --build kde/build --target help 2>/dev/null | grep -q install; then \
		cmake --install kde/build; \
		install -Dm644 kde/gui/org.ollie.gui.desktop $(PREFIX)/share/applications/org.ollie.gui.desktop; \
		install -Dm644 kde/gui/ollie.svg $(PREFIX)/share/icons/hicolor/scalable/apps/ollie.svg; \
		install -Dm755 kde/dolphin/ollie-actions.desktop $(PREFIX)/share/kio/servicemenus/ollie-actions.desktop; \
		install -Dm755 kde/dolphin/ollie-ask $(BINDIR)/ollie-ask; \
		install -Dm755 kde/dolphin/ollie-session-here $(BINDIR)/ollie-session-here; \
		install -m755 kde/lib9p/libollie9p.so $(LIBDIR)/libollie9p.so; \
	fi

# Install data files (agents, prompts, skills, workflows, scripts, tools, models)
install-data: install-models
	mkdir -p $(CONFIG_DIR)/agents $(CONFIG_DIR)/prompts $(CONFIG_DIR)/skills $(CONFIG_DIR)/workflows $(CONFIG_DIR)/optmem $(CONFIG_DIR)/tools
	@test -f $(CONFIG_DIR)/backends.conf || install -Dm600 data/backends.conf $(CONFIG_DIR)/backends.conf
	install -Dm755 third_party/optmem/memo $(CONFIG_DIR)/optmem/memo
	cp -a data/agents/. $(CONFIG_DIR)/agents/
	cp -a data/prompts/. $(CONFIG_DIR)/prompts/
	cp -a data/skills/. $(CONFIG_DIR)/skills/
	cp data/tools/*.meta $(CONFIG_DIR)/tools/
	for f in data/tools/*; do [ -f "$$f" ] && [ -x "$$f" ] && cp "$$f" $(CONFIG_DIR)/tools/; done; true
	@test -d data/tools/_lib && cp -a data/tools/_lib $(CONFIG_DIR)/tools/ || true
	install -Dm755 data/workflows/* $(CONFIG_DIR)/workflows/
	install -Dm644 cmd/toolsrv/internal/sandbox/sandbox.yaml $(CONFIG_DIR)/sandbox.yaml
	install -Dm755 data/scripts/ollie-remount $(BINDIR)/ollie-remount
	install -Dm755 data/scripts/logseq-cli $(BINDIR)/logseq-cli
	install -Dm755 data/scripts/o $(BINDIR)/o
	install -Dm755 data/scripts/acme/acme-ollie-ensure $(BINDIR)/acme-ollie-ensure
	for f in data/scripts/acme/[A-Z]*; do install -Dm755 "$$f" $(BINDIR)/$$(basename "$$f"); done

# Install embedding model for skill matching
install-models:
	mkdir -p $(DATA_DIR)/models
	@if [ ! -f $(DATA_DIR)/models/model.onnx ]; then \
		echo "Downloading embedding model..."; \
		curl -fsSL -o $(DATA_DIR)/models/model.onnx \
			"https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx"; \
		curl -fsSL -o $(DATA_DIR)/models/tokenizer.json \
			"https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/tokenizer.json"; \
	fi
	@if [ ! -f $(DATA_DIR)/models/libonnxruntime.so ]; then \
		echo "Downloading ONNX runtime..."; \
		curl -fsSL -o /tmp/onnxruntime.tgz \
			"https://github.com/microsoft/onnxruntime/releases/download/v1.29.0/onnxruntime-linux-x64-1.29.0.tgz"; \
		tar -xzf /tmp/onnxruntime.tgz -C /tmp; \
		cp /tmp/onnxruntime-linux-x64-1.29.0/lib/libonnxruntime.so.1.29.0 $(DATA_DIR)/models/libonnxruntime.so; \
		rm -rf /tmp/onnxruntime.tgz /tmp/onnxruntime-linux-x64-1.29.0; \
	fi

# Remove installed files
uninstall:
	rm -f $(BINDIR)/olliesrv $(BINDIR)/ollie-9p
	rm -f $(BINDIR)/ollie-gui $(BINDIR)/ollie-ask $(BINDIR)/ollie-session-here
	rm -f $(BINDIR)/ollie-remount $(BINDIR)/logseq-cli $(BINDIR)/o
	rm -f $(LIBDIR)/libollie9p.so
	rm -f $(PREFIX)/share/applications/org.ollie.gui.desktop
	rm -f $(PREFIX)/share/icons/hicolor/scalable/apps/ollie.svg
	rm -f $(PREFIX)/share/kio/servicemenus/ollie-actions.desktop
	rm -rf $(CONFIG_DIR)/tools
	@echo "Note: $(CONFIG_DIR)/{agents,prompts,skills,workflows,backends.conf} preserved"
	@echo "      Remove manually if desired: rm -rf $(CONFIG_DIR)"

# Remove build artifacts
clean:
	rm -rf $(BUILD_DIR)
	rm -rf kde/build
	rm -f kde/lib9p/libollie9p.so kde/lib9p/libollie9p.h
	go clean ./...

help:
	@echo 'Targets:'
	@echo '  make          Build, test, and install'
	@echo '  make build    Build without installing'
	@echo '  make test     Run tests'
	@echo '  make install  Install (requires build first)'
	@echo '  make uninstall Remove installed files'
	@echo '  make clean    Remove build artifacts'
	@echo ''
	@echo 'Components:'
	@echo '  make go       Build Go binaries (olliesrv, ollie-9p, toolsrv)'
	@echo '  make tools    Build compiled tools'
	@echo '  make kde      Build KDE components (KF6/Qt6)'
