# ============================================================================
#  Ambivalence - build + install (macOS / Linux)
#
#  Usage:
#    make               clean Release build (VST3 + standalone) into build/<platform>
#    make install       fresh build, remove stale installs, install cleanly
#    make uninstall     remove installed artifacts
#    make clean         delete the build directory
#    make version       print the current git branch + commit
#    make info          print the resolved configuration
#
#  Notes:
#    - `make install` always performs a clean rebuild first, so it is fully
#      self-sufficient. `make && make install` also works (it builds twice).
#    - JUCE is auto-fetched into ./build/JUCE on first build (shallow clone of
#      the JUCE_VERSION tag). Override with: make JUCE_PATH=/path/to/JUCE
#    - Install targets (user-level, no sudo required):
#        macOS:  VST3:       ~/Library/Audio/Plug-Ins/VST3/Ambivalence1.1.vst3
#                Standalone: ~/Applications/Ambivalence1.1.app
#        Linux:  VST3:       ~/.local/lib/vst3/Ambivalence1.1.vst3
#                Standalone: ~/.local/bin/Ambivalence1.1
# ============================================================================

PRODUCT := Ambivalence1.1

UNAME_S := $(shell uname -s)

# ============================================================================
# JUCE location
#   An explicit JUCE_PATH wins. Otherwise JUCE is shallow-cloned into
#   ./build/JUCE (pinned to JUCE_VERSION) so the build is self-contained.
# ============================================================================
JUCE_REPO    ?= https://github.com/juce-framework/JUCE.git
JUCE_VERSION ?= 8.0.15
JUCE_SRC     ?= $(CURDIR)/build/JUCE
JUCE_PATH    ?= $(JUCE_SRC)

# ============================================================================
# Platform-specific settings
# ============================================================================
ifeq ($(UNAME_S),Linux)
    BUILD_DIR ?= build/linux
    CONFIG    ?= Release
    JOBS      := $(shell nproc 2>/dev/null || echo 1)
    PREFIX    ?= $(HOME)/.local
    VST3_DIR  ?= $(HOME)/.local/lib/vst3
    APP_DIR   := $(PREFIX)/bin
else
    BUILD_DIR ?= build/macOS
    CONFIG    ?= Release
    ARCH      ?= $(shell uname -m)
    ifeq ($(ARCH),universal)
        OSX_ARCH := "arm64;x86_64"
    else
        OSX_ARCH := $(ARCH)
    endif
    JOBS      := $(shell sysctl -n hw.ncpu)
    PREFIX    ?= $(HOME)
    VST3_DIR  ?= $(HOME)/Library/Audio/Plug-Ins/VST3
    APP_DIR   := $(PREFIX)/Applications
endif

CMAKE := cmake

VST3_BUNDLE := $(VST3_DIR)/$(PRODUCT).vst3
STANDALONE  := $(APP_DIR)/$(PRODUCT)$(if $(filter $(UNAME_S),Linux),,.app)

ifdef OSX_ARCH
    OSX_FLAG := -DCMAKE_OSX_ARCHITECTURES="$(OSX_ARCH)"
endif

.PHONY: all build install uninstall clean version info

all: build

build:
	@command -v $(CMAKE) >/dev/null 2>&1 || { echo "error: cmake not found"; exit 1; }
	@if [ -d "$(JUCE_PATH)/modules/juce_core" ]; then \
	    echo "==> JUCE: $(JUCE_PATH)"; \
	elif [ "$(JUCE_PATH)" = "$(JUCE_SRC)" ]; then \
	    echo "==> JUCE: fetching $(JUCE_VERSION) into $(JUCE_SRC)"; \
	    mkdir -p "$(dir $(JUCE_SRC))"; \
	    rm -rf "$(JUCE_SRC)"; \
	    git clone --depth 1 --branch "$(JUCE_VERSION)" "$(JUCE_REPO)" "$(JUCE_SRC)" \
	        || { rm -rf "$(JUCE_SRC)"; echo "error: failed to fetch JUCE"; exit 1; }; \
	else \
	    echo "error: JUCE not found at '$(JUCE_PATH)'"; \
	    echo "       run: make JUCE_PATH=/path/to/JUCE"; \
	    exit 1; \
	fi
	@echo "==> Ambivalence: clean build ($(CONFIG), $(UNAME_S))"
	rm -rf "$(BUILD_DIR)"
	$(CMAKE) -S . -B "$(BUILD_DIR)" \
	    -DJUCE_PATH="$(JUCE_PATH)" \
	    -DCMAKE_BUILD_TYPE="$(CONFIG)" \
	    $(OSX_FLAG) \
	    -DCMAKE_INSTALL_PREFIX="$(PREFIX)" \
	    -DJUCE_VST3_DIR="$(VST3_DIR)"
	$(CMAKE) --build "$(BUILD_DIR)" -j$(JOBS)

install: build
	@echo "==> Ambivalence: removing stale installs"
	rm -rf "$(VST3_BUNDLE)" "$(STANDALONE)"
	@echo "==> Ambivalence: installing"
	$(CMAKE) --install "$(BUILD_DIR)"
	@echo "==> Done."
	@if [ -d "$(VST3_BUNDLE)" ]; then \
	    echo "    VST3:       $(VST3_BUNDLE)"; \
	else \
	    echo "    WARNING: VST3 bundle not found at $(VST3_BUNDLE)"; \
	fi
	@if [ -e "$(STANDALONE)" ]; then \
	    echo "    Standalone: $(STANDALONE)"; \
	else \
	    echo "    (standalone not installed - checked $(STANDALONE))"; \
	fi
	@echo "    Rescan plugins in your DAW to pick up the new build."

uninstall:
	@echo "==> Ambivalence: uninstalling"
	rm -rf "$(VST3_BUNDLE)" "$(STANDALONE)"
	@echo "==> Done."

clean:
	rm -rf "$(BUILD_DIR)"

version:
	@echo "branch: $$(git rev-parse --abbrev-ref HEAD)"
	@echo "commit: $$(git rev-parse --short HEAD)"

info:
	@echo "platform    = $(UNAME_S)"
	@echo "JUCE_PATH   = $(JUCE_PATH)"
	@echo "JUCE_REPO   = $(JUCE_REPO)"
	@echo "JUCE_VER    = $(JUCE_VERSION)"
	@echo "BUILD_DIR   = $(BUILD_DIR)"
	@echo "CONFIG      = $(CONFIG)"
	@echo "JOBS        = $(JOBS)"
	@echo "PREFIX      = $(PREFIX)"
	@echo "VST3        = $(VST3_BUNDLE)"
	@echo "Standalone  = $(STANDALONE)"
