# Mattermore — Mattermost AI Chat Agent
# Build targets:
#   make dist       — Build all platform binaries and package plugin
#   make build      — Build linux-amd64 binary only
#   make check      — Lint + test
#   make clean      — Remove build artifacts

PLUGIN_ID := com.forkless.mattermore
PLUGIN_VERSION := 0.1.17
DIST_DIR := dist
SERVER_DIR := server
BUNDLE_NAME := $(PLUGIN_ID)-$(PLUGIN_VERSION).tar.gz
BUNDLE_PATH := $(DIST_DIR)/$(BUNDLE_NAME)

# Platforms to build for
LINUX_AMD64 := linux-amd64
LINUX_ARM64 := linux-arm64
DARWIN_AMD64 := darwin-amd64
DARWIN_ARM64 := darwin-arm64

.PHONY: all build dist check clean

all: dist

# Build linux-amd64 only (fastest for local testing)
build:
	@mkdir -p $(SERVER_DIR)/dist
	GOOS=linux GOARCH=amd64 CGO_ENABLED=0 \
		go build -o $(SERVER_DIR)/dist/plugin-$(LINUX_AMD64) ./$(SERVER_DIR)

# Build all platform binaries
dist: build-all-platforms bundle

build-all-platforms:
	@mkdir -p $(SERVER_DIR)/dist
	@echo "Building for $(LINUX_AMD64)..."
	@GOOS=linux GOARCH=amd64 CGO_ENABLED=0 \
		go build -o $(SERVER_DIR)/dist/plugin-$(LINUX_AMD64) ./$(SERVER_DIR)
	@echo "Building for $(LINUX_ARM64)..."
	@GOOS=linux GOARCH=arm64 CGO_ENABLED=0 \
		go build -o $(SERVER_DIR)/dist/plugin-$(LINUX_ARM64) ./$(SERVER_DIR)
	@echo "Building for $(DARWIN_AMD64)..."
	@GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 \
		go build -o $(SERVER_DIR)/dist/plugin-$(DARWIN_AMD64) ./$(SERVER_DIR)
	@echo "Building for $(DARWIN_ARM64)..."
	@GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 \
		go build -o $(SERVER_DIR)/dist/plugin-$(DARWIN_ARM64) ./$(SERVER_DIR)

bundle:
	@mkdir -p $(DIST_DIR)
	@echo "Creating bundle..."
	@mkdir -p /tmp/mm-bundle/$(PLUGIN_ID)-$(PLUGIN_VERSION)
	@cp plugin.json /tmp/mm-bundle/$(PLUGIN_ID)-$(PLUGIN_VERSION)/
	@mkdir -p /tmp/mm-bundle/$(PLUGIN_ID)-$(PLUGIN_VERSION)/server/dist
	@cp $(SERVER_DIR)/dist/* /tmp/mm-bundle/$(PLUGIN_ID)-$(PLUGIN_VERSION)/server/dist/
	@cd /tmp/mm-bundle && tar czf $(CURDIR)/$(BUNDLE_PATH) $(PLUGIN_ID)-$(PLUGIN_VERSION)
	@rm -rf /tmp/mm-bundle
	@echo "Bundle created: $(BUNDLE_PATH)"

check:
	@echo "Running go vet..."
	@go vet ./$(SERVER_DIR)
	@echo "Running go fmt..."
	@go fmt ./$(SERVER_DIR)
	@echo "Done"

clean:
	@rm -rf $(DIST_DIR)
	@rm -rf $(SERVER_DIR)/dist
	@echo "Cleaned"
