.DEFAULT_GOAL := all

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

.PHONY: all build build-kf5 core gofmt ninep client tools kde kde-kf5 \
        install install-kf5 install-data test test-core test-9p clean help

all: build install

build: core ninep client tools kde
build-kf5: core ninep client kde-kf5

core:
	go build ./...

gofmt:
	gofmt -w $$(find . -name '*.go' -not -path './vendor/*')

ninep:
	mkdir -p $(BINDIR)
	go build -o $(BINDIR)/olliesrv ./cmd/olliesrv/
	go build -o $(BINDIR)/ollie-9p ./cmd/ollie-9p/

client:
	mkdir -p $(LIBDIR) $(INCDIR)
	cd kde/lib9p && go build -buildmode=c-shared -o libollie9p.so
	install -m755 kde/lib9p/libollie9p.so $(LIBDIR)/libollie9p.so
	install -m644 kde/lib9p/libollie9p.h $(INCDIR)/libollie9p.h

tools:
	mkdir -p $(CONFIG_DIR)/tools
	go build -o $(CONFIG_DIR)/tools/toolsrv ./cmd/toolsrv/
	for d in tools/codeintel/cmd/* tools/filetools/cmd/* tools/lsp/cmd/* tools/web/cmd/*; do \
		name=$$(basename $$d); [ -f $$d/main.go ] || continue; \
		go build -o $(CONFIG_DIR)/tools/$$name ./$$d; \
	done
	install -m644 data/tools/*.meta $(CONFIG_DIR)/tools/
	for f in data/tools/*; do [ -f "$$f" ] && [ -x "$$f" ] && install -m755 "$$f" $(CONFIG_DIR)/tools/; done; true
	cp -a data/tools/_lib $(CONFIG_DIR)/tools/

kde:
	cmake -S kde -B kde/build -DCMAKE_INSTALL_PREFIX=$(PREFIX)
	cmake --build kde/build -j$(JOBS)

kde-kf5:
	cmake -S kde -B kde/build-kf5 -DOLLIE_KF5=ON -DCMAKE_INSTALL_PREFIX=$(PREFIX)
	cmake --build kde/build-kf5 -j$(JOBS)

install: build test install-data
	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-kf5: build-kf5 test install-data
	cmake --install kde/build-kf5
	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-kf5.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-data:
	mkdir -p $(CONFIG_DIR)/agents $(CONFIG_DIR)/prompts $(CONFIG_DIR)/skills $(CONFIG_DIR)/tools $(CONFIG_DIR)/workflows
	@test -f $(CONFIG_DIR)/backends.conf || install -Dm600 data/backends.conf $(CONFIG_DIR)/backends.conf
	cp -a data/agents/. $(CONFIG_DIR)/agents/
	cp -a data/prompts/. $(CONFIG_DIR)/prompts/
	cp -a data/skills/. $(CONFIG_DIR)/skills/
	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

# The GUI/KDE build is intentionally delegated to kde/CMakeLists.txt. It
# detects KF6 first and falls back to KF5; kde-kf5 forces the KF5 path.
test: test-core test-9p

test-core:
	go test ./cmd/olliesrv/... ./cmd/toolsrv/... ./log/... ./toolsrv/... ./util/... ./tools/codeintel/...

test-9p:
	go test ./cmd/ollie-9p/... ./kde/lib9p/...

clean:
	go clean ./...
	rm -rf kde/build kde/build-kf5

help:
	@echo 'make             Build and install (automatic KF6/KF5 detection)'
	@echo 'make build       Build without installing'
	@echo 'make kde         Build KDE using automatic detection'
	@echo 'make kde-kf5     Build KDE against KF5/Qt5'
	@echo 'make test        Run tests'
	@echo 'make install     Install the automatic-detection build'
	@echo 'make clean       Remove build artifacts'
