# Standalone Makefile for rapid Darwin PMDA development
# This allows quick rebuilds without running the full PCP build
#
# IMPORTANT: Requires that configure has been run at least once
# Usage:
#   make check-configured   # Check if PCP was configured
#   make                    # Build darwin PMDA binary and DSO
#   make clean              # Clean build artifacts
#   make test               # Run quick smoke test

TOPDIR = ../..
SRCDIR = $(TOPDIR)/src/pmdas/darwin

# Find the versioned build directory (Makepkgs creates pcp-X.Y.Z)
PCP_BUILD_DIR := $(shell ls -d $(TOPDIR)/pcp-* 2>/dev/null | head -1)
ifeq ($(PCP_BUILD_DIR),)
    $(error Cannot find Makepkgs build directory. Run: cd $(TOPDIR) && ./Makepkgs --verbose)
endif

# Check if PCP has been built (look for platform_defs.h generated by configure in build dir)
ifeq ($(wildcard $(PCP_BUILD_DIR)/src/include/pcp/platform_defs.h),)
    $(error PCP must be fully built and installed first. Run: cd $(TOPDIR) && ./Makepkgs --verbose)
endif

# Compiler and flags
CC = clang
CFLAGS = -Wall -O2 -g
CFLAGS += -I.                           # Current directory for generated domain.h
CFLAGS += -I$(PCP_BUILD_DIR)/src/include/pcp  # Generated headers (platform_defs.h)
CFLAGS += -I$(TOPDIR)/src/include
CFLAGS += -I$(TOPDIR)/src/include/pcp
CFLAGS += -DPCP_VERSION=\"dev\"

# macOS-specific frameworks
FRAMEWORKS = -framework CoreFoundation -framework IOKit

# PCP libraries from Makepkgs build directory
PCP_LIB_DIR = $(PCP_BUILD_DIR)/src/libpcp/src
PCP_PMDA_LIB_DIR = $(PCP_BUILD_DIR)/src/libpcp_pmda/src

LDFLAGS = -L$(PCP_LIB_DIR) -L$(PCP_PMDA_LIB_DIR)
LDLIBS = -lpcp -lpcp_pmda $(FRAMEWORKS)

# Source files
SOURCES = $(SRCDIR)/*.c

HEADERS = $(SRCDIR)/*.h

OBJECTS = $(SOURCES:.c=.o)

# Generated files
DOMAIN_H = domain.h
PMNS_FILE = $(SRCDIR)/pmns

# Build targets
BINARY = pmdadarwin
DYLIB = pmda_darwin.dylib

# Default target
all: $(DOMAIN_H) $(BINARY) $(DYLIB)

# Generate domain.h - minimal version for quick builds
# The Darwin PMDA domain constant is 78
$(DOMAIN_H):
	@echo "Generating domain.h..."
	@echo '/* Auto-generated domain.h for Darwin PMDA - Quick Build */' > $(DOMAIN_H)
	@echo '#ifndef DARWIN' >> $(DOMAIN_H)
	@echo '#define DARWIN 78' >> $(DOMAIN_H)
	@echo '#endif' >> $(DOMAIN_H)
	@echo "✓ Generated $(DOMAIN_H)"

# Build daemon binary (depends on domain.h being generated first)
$(BINARY): $(DOMAIN_H) $(SOURCES) $(HEADERS)
	@echo "Building Darwin PMDA daemon..."
	$(CC) $(CFLAGS) $(SOURCES) $(LDFLAGS) $(LDLIBS) -o $(BINARY)
	@echo "✓ Built $(BINARY)"

# Build DSO library (depends on domain.h being generated first)
$(DYLIB): $(DOMAIN_H) $(SOURCES) $(HEADERS)
	@echo "Building Darwin PMDA DSO..."
	$(CC) $(CFLAGS) -dynamiclib $(SOURCES) $(LDFLAGS) $(LDLIBS) -o $(DYLIB)
	@echo "✓ Built $(DYLIB)"

# Quick smoke test
test: $(BINARY)
	@echo "Running smoke test..."
	@DYLD_LIBRARY_PATH=$(PCP_LIB_DIR):$(PCP_PMDA_LIB_DIR) ./$(BINARY) --help 2>&1 | grep -q "Usage:" && echo "✓ Smoke test passed" || (echo "✗ Smoke test failed"; exit 1)

# Install to local PCP (requires sudo)
install: $(DYLIB)
	@echo "Installing to local PCP..."
	sudo cp $(DYLIB) $(PCP_LIB_DIR)/pcp/pmdas/darwin/
	@echo "Restarting pmcd..."
	sudo pmcd restart || sudo launchctl kickstart -k system/org.pcp.pmcd
	@echo "✓ Installed and restarted pmcd"

# Clean build artifacts
clean:
	rm -f $(BINARY) $(DYLIB) $(OBJECTS) $(DOMAIN_H)
	@echo "✓ Cleaned build artifacts"

# Development helpers
check-configured:
	@if [ -f $(PCP_BUILD_DIR)/src/include/pcp/platform_defs.h ]; then \
		echo "✓ PCP is configured"; \
		exit 0; \
	else \
		echo "✗ PCP has not been configured"; \
		echo ""; \
		echo "To configure PCP:"; \
		echo "  cd $(TOPDIR)"; \
		echo "  ./Makepkgs --verbose"; \
		echo ""; \
		echo "This only needs to be done once."; \
		exit 1; \
	fi

check-deps:
	@echo "Checking dependencies..."
	@which clang > /dev/null && echo "✓ clang found" || echo "✗ clang not found"
	@test -f $(PCP_LIB_DIR)/libpcp.dylib && echo "✓ libpcp found at $(PCP_LIB_DIR)" || echo "✗ libpcp not found at $(PCP_LIB_DIR)"
	@test -f $(PCP_PMDA_LIB_DIR)/libpcp_pmda.dylib && echo "✓ libpcp_pmda found at $(PCP_PMDA_LIB_DIR)" || echo "✗ libpcp_pmda not found at $(PCP_PMDA_LIB_DIR)"
	@test -d $(TOPDIR)/src/include/pcp && echo "✓ PCP headers found" || echo "✗ PCP headers not found"
	@test -d $(PCP_BUILD_DIR) && echo "✓ Makepkgs build directory found at $(PCP_BUILD_DIR)" || echo "✗ Makepkgs build directory not found"

.PHONY: all test install clean check-configured check-deps
