# Horizont plugin - build and install (macOS)
#
#   make            fetch JUCE, configure and build (Release)
#   make install    build and install into ~/Library/Audio/Plug-Ins
#   make uninstall  remove the installed copies
#   make clean      delete the build directory
#   make help       list available targets

JUCE_DIR      := JUCE
JUCE_VERSION  := 8.0.15
JUCE_REPO     := https://github.com/juce-framework/JUCE.git

BUILD_DIR     := build
ARTIFACTS     := $(BUILD_DIR)/Horizont_artefacts
CONFIG        := Release

VST3          := $(ARTIFACTS)/$(CONFIG)/VST3/Horizont.vst3
AU            := $(ARTIFACTS)/$(CONFIG)/AU/Horizont.component

PLUGINS_HOME  := $(HOME)/Library/Audio/Plug-Ins
VST3_INSTALL  := $(PLUGINS_HOME)/VST3/Horizont.vst3
AU_INSTALL    := $(PLUGINS_HOME)/Components/Horizont.component

CMAKE         := cmake
JUCE_ROOT     := $(abspath $(CURDIR)/$(JUCE_DIR))

.PHONY: all help deps configure build install uninstall clean

all: build

help:
	@echo "Horizont plugin targets:"
	@echo "  make            build (fetch JUCE into ./$(JUCE_DIR) if missing)"
	@echo "  make install    build + install into $(PLUGINS_HOME)"
	@echo "  make uninstall  remove installed plugin"
	@echo "  make clean      delete $(BUILD_DIR)"

deps:
	@if [ -f "$(JUCE_DIR)/CMakeLists.txt" ]; then \
		echo "==> JUCE already present in ./$(JUCE_DIR)"; \
	else \
		echo "==> Fetching JUCE $(JUCE_VERSION) into ./$(JUCE_DIR) ..."; \
		git clone --depth 1 --branch $(JUCE_VERSION) $(JUCE_REPO) $(JUCE_DIR) || { \
			echo "!! Could not fetch JUCE. Check network access."; exit 1; }; \
	fi

configure: deps
	@mkdir -p $(BUILD_DIR)
	$(CMAKE) -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=$(CONFIG) -DJUCE_ROOT="$(JUCE_ROOT)"

build: configure
	$(CMAKE) --build $(BUILD_DIR) --target Horizont_All --config $(CONFIG) -j

$(VST3) $(AU): build

install: build
	@echo "==> Installing Horizont into $(PLUGINS_HOME)"
	@mkdir -p "$(PLUGINS_HOME)/VST3" "$(PLUGINS_HOME)/Components"
	@rm -rf "$(VST3_INSTALL)" "$(AU_INSTALL)"
	@cp -R "$(VST3)" "$(VST3_INSTALL)"
	@cp -R "$(AU)" "$(AU_INSTALL)"
	@codesign --force --deep --sign - "$(VST3_INSTALL)" >/dev/null 2>&1 || true
	@codesign --force --deep --sign - "$(AU_INSTALL)" >/dev/null 2>&1 || true
	@echo "==> Installed:"
	@echo "    $(VST3_INSTALL)"
	@echo "    $(AU_INSTALL)"

uninstall:
	@rm -rf "$(VST3_INSTALL)" "$(AU_INSTALL)"
	@echo "==> Removed installed plugin."

clean:
	@rm -rf $(BUILD_DIR)
	@echo "==> Cleaned $(BUILD_DIR)."
