.PHONY: install uninstall status

OPENCODE_DIR := $(HOME)/.config/opencode
REPO_DIR := $(shell pwd)

# Items to symlink
ITEMS := skills tools agents

# LLM services to manage
LLM_SERVICES := atlas forge swift

install:
	@echo "Installing OpenCode config symlinks..."
	@mkdir -p $(OPENCODE_DIR)
	@for item in $(ITEMS); do \
		if [ -e "$(REPO_DIR)/.opencode/$$item" ]; then \
			if [ -L "$(OPENCODE_DIR)/$$item" ]; then \
				echo "  $$item: already symlinked"; \
			else \
				ln -s "$(REPO_DIR)/.opencode/$$item" "$(OPENCODE_DIR)/$$item"; \
				echo "  $$item: symlinked"; \
			fi \
		else \
			echo "  $$item: skipped (not found)"; \
		fi \
	done
	@echo "Done!"

uninstall:
	@echo "Removing OpenCode config symlinks..."
	@for item in $(ITEMS); do \
		if [ -L "$(OPENCODE_DIR)/$$item" ]; then \
			rm "$(OPENCODE_DIR)/$$item"; \
			echo "  $$item: removed symlink"; \
		fi \
	done
	@echo "Done!"

status:
	@echo "OpenCode config status:"
	@for item in $(ITEMS); do \
		if [ -L "$(OPENCODE_DIR)/$$item" ]; then \
			target=$$(readlink "$(OPENCODE_DIR)/$$item"); \
			echo "  $$item: symlink -> $$target"; \
		elif [ -e "$(OPENCODE_DIR)/$$item" ]; then \
			echo "  $$item: exists (not symlinked)"; \
		else \
			echo "  $$item: not found"; \
		fi \
	done

restart-llm-%:
	@echo "Restarting com.vllm-mlx-$*..."
	@launchctl bootout gui/$$(id -u) ~/Library/LaunchAgents/com.vllm-mlx.$*.plist || true
	@launchctl bootstrap gui/$$(id -u) ~/Library/LaunchAgents/com.vllm-mlx.$*.plist

restart-llm: $(patsubst %,restart-llm-%,$(LLM_SERVICES))