# Terion — stereo analyzer VST3/AU
# A thin wrapper around CMake, with macOS signing / un-quarantining helpers.

BUILD_DIR      ?= build
LOCAL_JUCE     ?= third_party/JUCE
HOME_JUCE      := $(HOME)/JUCE
XDG_JUCE_LOWER := $(HOME)/.local/git/juce
XDG_JUCE_UPPER := $(HOME)/.local/git/JUCE
CMAKE          ?= cmake
CONFIG         ?= Release
JUCE_REPO      ?= https://github.com/juce-framework/JUCE.git
JOBS           ?= $(shell sysctl -n hw.ncpu 2>/dev/null || nproc 2>/dev/null || echo 4)
# Codesign identity: leave empty for ad-hoc ("-") signing.
CODESIGN_ID    ?=

VST3_DIR       := $(HOME)/Library/Audio/Plug-Ins/VST3/Terion.vst3
AU_DIR         := $(HOME)/Library/Audio/Plug-Ins/Components/Terion.component

# Prefer a local JUCE checkout (~/JUCE, then ~/.local/git/{juce,JUCE}) if
# present, otherwise use the vendored copy in the repo.
ifeq ($(strip $(JUCE_DIR)),)
  ifneq ($(wildcard $(HOME_JUCE)/CMakeLists.txt),)
    JUCE_DIR := $(HOME_JUCE)
  else ifneq ($(wildcard $(XDG_JUCE_LOWER)/CMakeLists.txt),)
    JUCE_DIR := $(XDG_JUCE_LOWER)
  else ifneq ($(wildcard $(XDG_JUCE_UPPER)/CMakeLists.txt),)
    JUCE_DIR := $(XDG_JUCE_UPPER)
  else
    JUCE_DIR := $(LOCAL_JUCE)
  endif
endif

.PHONY: all juce configure build install sign unquarantine uninstall clean deepclean help

all: configure
	$(CMAKE) --build $(BUILD_DIR) --config $(CONFIG) -j$(JOBS)

# Ensure a JUCE source tree is available: use a local checkout
# (~/JUCE or ~/.local/git/{juce,JUCE}), else clone into third_party/.
juce:
	@if [ ! -f "$(JUCE_DIR)/CMakeLists.txt" ]; then \
		if [ ! -d "$(dir $(JUCE_DIR))" ]; then mkdir -p "$(dir $(JUCE_DIR))"; fi; \
		echo "JUCE not found at $(JUCE_DIR) — cloning into it..."; \
		git clone --depth 1 "$(JUCE_REPO)" "$(JUCE_DIR)"; \
	else \
		echo "JUCE already present at $(JUCE_DIR)."; \
	fi

# The build target auto-installs plugins via JUCE's COPY_PLUGIN_AFTER_BUILD.
configure: juce
	$(CMAKE) -S . -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=$(CONFIG) -DTERION_JUCE_DIR=$(JUCE_DIR)

build:
	$(CMAKE) --build $(BUILD_DIR) --config $(CONFIG) -j$(JOBS)
	@echo "Plugins built into $(BUILD_DIR)/Terion_artefacts/$(CONFIG)."

# Ad-hoc (or identity) codesign both plugin bundles, recursively.
sign:
	@if [ -d "$(VST3_DIR)" ]; then \
		echo "Signing VST3 ..."; \
		codesign --force --deep --sign "$(if $(CODESIGN_ID),$(CODESIGN_ID),-)" "$(VST3_DIR)"; \
	else \
		echo "VST3 bundle not found at $(VST3_DIR) — run 'make build' first."; \
	fi
	@if [ -d "$(AU_DIR)" ]; then \
		echo "Signing AU ..."; \
		codesign --force --deep --sign "$(if $(CODESIGN_ID),$(CODESIGN_ID),-)" "$(AU_DIR)"; \
	else \
		echo "AU bundle not found at $(AU_DIR) — run 'make build' first."; \
	fi

# Strip Gatekeeper quarantine / provenance attributes from the installed bundles.
unquarantine:
	@if [ -d "$(VST3_DIR)" ]; then \
		echo "Removing quarantine from VST3 ..."; \
		xattr -dr com.apple.quarantine "$(VST3_DIR)" 2>/dev/null || true; \
		xattr -dr com.apple.provenance "$(VST3_DIR)" 2>/dev/null || true; \
	fi
	@if [ -d "$(AU_DIR)" ]; then \
		echo "Removing quarantine from AU ..."; \
		xattr -dr com.apple.quarantine "$(AU_DIR)" 2>/dev/null || true; \
		xattr -dr com.apple.provenance "$(AU_DIR)" 2>/dev/null || true; \
	fi

install: build sign unquarantine
	@echo "Terion VST3/AU installed, signed, and un-quarantined."
	@echo "  VST3: $(VST3_DIR)"
	@echo "  AU:   $(AU_DIR)"

uninstall:
	rm -rf "$(VST3_DIR)"
	rm -rf "$(AU_DIR)"
	@echo "Uninstalled Terion from the user Audio Plug-Ins directories."

clean:
	rm -rf $(BUILD_DIR)

# Fully remove generated artefacts, including the vendored JUCE copy.
deepclean: clean
	rm -rf build Terion_artefacts third_party

help:
	@echo "Targets:"
	@echo "  all            configure + build"
	@echo "  juce           clone JUCE into third_party/JUCE if missing (else use ~/JUCE or ~/.local/git/{juce,JUCE})"
	@echo "  configure      run CMake configure against $(BUILD_DIR)"
	@echo "  build          compile the configured tree"
	@echo "  sign           codesign both installed plugin bundles (ad-hoc by default)"
	@echo "  unquarantine   strip Gatekeeper quarantine/provenance xattrs"
	@echo "  install        build + sign + unquarantine into ~/Library/Audio/Plug-Ins"
	@echo "  uninstall      remove installed plugins"
	@echo "  clean          remove $(BUILD_DIR)"
	@echo "  deepclean      remove build dir, artefacts, and third_party/"
	@echo ""
	@echo "Variables:  BUILD_DIR=  JUCE_DIR=  CONFIG=  JOBS=  CMAKE=  JUCE_REPO="
	@echo "            CODESIGN_ID=  (e.g. 'Developer ID Application: X' — empty = ad-hoc)"